LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2012, 05:39 PM   #1
Qube
LQ Newbie
 
Registered: Mar 2011
Location: Cobble Hill, BC Canada
Distribution: CentOS 5.5
Posts: 2

Rep: Reputation: 0
How to remove 'dead' folders from backup device...


I have an external HDD, which I connect via USB, and run my backup script, which is just:
cp -r -u -v /ThisFolder /media/disk/DriveQ/backup

All is working great, and I get the new/modified files copied fine...however:

Someone has helpfully been renaming some of the source folders:

'Joe's Woodworking' has been renamed to 'Joe's Woodworking Inc' etc etc.

Since the new name does not exist, it gets copied, so now we have Joe's data duplicated.

I need to write a script that will troll through the folder I point it to on the backup device, and remove each folder it does NOT find in the 'ThisFolder' source.


Any assistance or direction would be greatly appreciated.

Cheers,
Q.
 
Old 10-23-2012, 03:00 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi,
do you also want to remove files that do not exist in the source directory or just the directories?
If you want to remove both files and directories, I would recommend using rsync with the --del switch.

If you want to only remove directories, you'll have to filter the files out first. Something like this might work:

Code:
#!/bin/bash

src_dir="/ThisFolder"
dst_dir="./media/disk/DriveQ/backup"

IFS='
'

# get the list of files/directories to be deleted
del_list=($(rsync -avirun --del "$src_dir/" "$dst_dir"|grep "^\*deleting"|cut -c13- ))

# remove the directories
for i in "${del_list[@]}"; do
	if [[ -d "${dst_dir}/$i" ]]; then echo rm -r "$i"; fi
done
Anyway, be very careful with this, allways run with the echo first to see what is going to be deleted before you actually do it.
also, with rsync, pay attention to the trailing slash in the source directory. It does matter.

Hope this helps
 
Old 10-23-2012, 06:45 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
What about a local rsync between the two disks? Most likely it’s installed already. There you can perform a dry-run (-n) first and get a list what would be done if used in combination with -i:
Code:
$ rsync -navi /ThisFolder /media/disk/DriveQ/backup
This command (without -ni) could also be put in a cron job for a daily copy.

Last edited by Reuti; 10-24-2012 at 05:17 AM. Reason: Typo
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to search for and remove dead symbolic links? HGeneAnthony Linux - General 5 01-19-2007 02:09 AM
Using rsync to backup data. Best way to backup folders? Micro420 Linux - General 2 11-23-2006 01:13 AM
How to remove folders Gins Linux - General 7 08-15-2006 01:31 PM
Bookmark mgr to remove dead links? hyllplan Linux - Software 0 08-31-2005 06:35 AM
Forcibly Remove Dead Smb Mounts tunasashimi Linux - Networking 3 06-13-2005 11:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration