Dan Craciun
asked on
Shell script to delete old backups
Hi,
I need a script to delete old backups created by Cobian Backup on a NAS (for some reason the program does not delete the old backups, gave up trying to find out why).
The NAS is running Synology 5, with ssh access enabled. The built-in shell is ash.
The backups are basically one full, followed by 0 or more incremental backups, then the next full one etc.
The backups are stored, for each user, in /volume1/homes/username/. See the structure below.
What I need is to delete, for each backed up folder, all the backups apart from the most recent full backup and its incremental backups. After the script runs, I should have, for each user and each folder, a single full backup and 0 or more incremental backups with a more recent date than the full backup.
How can I do that?
Thank you.
The file structure is as follows:
I need a script to delete old backups created by Cobian Backup on a NAS (for some reason the program does not delete the old backups, gave up trying to find out why).
The NAS is running Synology 5, with ssh access enabled. The built-in shell is ash.
The backups are basically one full, followed by 0 or more incremental backups, then the next full one etc.
The backups are stored, for each user, in /volume1/homes/username/. See the structure below.
What I need is to delete, for each backed up folder, all the backups apart from the most recent full backup and its incremental backups. After the script runs, I should have, for each user and each folder, a single full backup and 0 or more incremental backups with a more recent date than the full backup.
How can I do that?
Thank you.
The file structure is as follows:
volume1/homes/user1/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder1 2014-xx-xx xx;xx;xx (Incremental)
volume1/homes/user1/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder2 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder2 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder2 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder2 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder2 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user1/folder2 2014-xx-xx xx;xx;xx (Incremental)
volume1/homes/user1/folder2 2014-12-34 56;78;90 (Full)
volume1/homes/user1/folder2 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user2/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user2/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user2/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user2/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user2/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
volume1/homes/user2/folder1 2014-xx-xx xx;xx;xx (Incremental)
volume1/homes/user2/folder1 2014-12-34 56;78;90 (Full)
volume1/homes/user2/folder1 2014-xx-xx xx;xx;xx (Incremental)
...
How you distinguish between full and incremental backups and are these file names or folder names?
ASKER
The full backups end in (Full), the incremental ones in (Incremental). For ex, see below a real listing:
ServerBackup> ls -l
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-01 09;30;17 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-09 09;30;28 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-10 09;30;24 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-13 09;30;19 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-14 09;30;28 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-15 09;30;16 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-16 09;30;24 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-17 09;30;30 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-20 09;30;28 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-21 09;30;24 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-22 09;30;27 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-23 09;30;19 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-24 09;30;24 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-27 09;30;26 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-28 09;30;33 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-29 09;30;28 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-30 09;30;30 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-10-31 09;30;20 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-03 09;30;23 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-04 09;30;27 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-05 09;30;17 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-06 09;30;24 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-07 09;30;27 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-11 09;30;20 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-12 09;30;30 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-13 09;30;24 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-14 09;30;20 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-17 09;30;26 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-18 09;30;26 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-19 09;30;17 (Incremental)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-11-20 09;30;17 (Full)
drwxrwxrwx 3 user1 users 4096 Nov 25 2009 Profiles 2014-12-19 09;30;27 (Incremental)
should
volume1/homes/user1/folder 1 2014-xx-xx xx;xx;xx (Incremental)
be deleted because it is older than
volume1/homes/user1/folder 2 2014-12-34 56;78;90 (Full)
or should it be kept because it is more recent than
volume1/homes/user1/folder 1 2014-12-34 56;78;90 (Full)
?
i.e. is
volume1/homes/user1/folder 2 2014-12-34 56;78;90 (Full)
part of the same backup set as
volume1/homes/user1/folder 1 2014-12-34 56;78;90 (Full)
or is it part of a separate backup set that should be pruned separately?
volume1/homes/user1/folder
be deleted because it is older than
volume1/homes/user1/folder
or should it be kept because it is more recent than
volume1/homes/user1/folder
?
i.e. is
volume1/homes/user1/folder
part of the same backup set as
volume1/homes/user1/folder
or is it part of a separate backup set that should be pruned separately?
ASKER
It's a separate backup. Each user has 1 or more folders that they chose to backup, i.e. Desktop, Documents, etc, so for ex folder1 is Desktop, folder 2 is Documents, folder 3 is Pictures etc. And each can have it's own schedule.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
At the end of the day, what you should prune more often will be the full backups, since they will be the biggest hogs.
So why not just do a find to delete fulls older than x days (in this example older than 30 days):
For incrementals, you could do similar:
It's not exactly what you asked, but it uses KISS methodology (Keep It Short and Simple).
So why not just do a find to delete fulls older than x days (in this example older than 30 days):
find /volume1/homes/user1 -name *Full* -mtime +30 | xargs rm
For incrementals, you could do similar:
find /volume1/homes/user1 -name *Incremental* -mtime +30 | xargs rm
It's not exactly what you asked, but it uses KISS methodology (Keep It Short and Simple).
ASKER
@Carlos: if you notice, all the folder have the same date: Nov 25 2009. That's probably the reason why Cobian does not delete old backups (it's searching for timestamps, not folder names).
@ozo: thank you. I'll try to understand the script first, will post back with what I could not figure out by myself. I'm more of a powershell guy, so Perl is a little greek to me.
@ozo: thank you. I'll try to understand the script first, will post back with what I could not figure out by myself. I'm more of a powershell guy, so Perl is a little greek to me.
ASKER
Here's what I got so far:
I tested the script (replacing system "rm", "-f" with system "echo") and it seams to work. But I can't still figure out why :)
Can you enlighten me, ozo, or shall I post this as a new question?
perl -e 'for( @ARGV ){ # create a short script (-e).
#Run the block for each of the arguments array (basically for each folder in /volume1/homes/).
#The current folder name (/volume1/homes/user1) will be stored in $_
my %b; # declare a hash b. Does it have an initial value?
m#(.*?)\s\d{4}-\d\d-\d\d\s\d\d;\d\d;\d\d\s\((Full|Incremental)\)# && unshift @{$b{$1}},$_ for grep -d, <"$_"/*>;
#then it becomes fuzzy: for each file in the current path ($_/*) search for names like folder2 2014-12-34 56;78;90 (Full)
#and then prepend the current path (/volume1/homes/user1 for ex) to what array??? $b{$1} contains what?
# How is the hash constructed?
for( values %b ){ #for each value of the hash b
my($f,$i); #declare 2 local variables, $f and $i. Are they the (key,value) pair for b?
system "rm","-f",$_ for grep {$i|=$f,$f|=/\(Full\)$/,$i}@$_;
#again, fuzzy: remove (run rm -f) for each file that matches the grep. But I cannot figure out the grep. It reverses the
#file order and preserves only those after the first folder with "(Full)" in the name?
}
}' /volume1/homes/*
I tested the script (replacing system "rm", "-f" with system "echo") and it seams to work. But I can't still figure out why :)
Can you enlighten me, ozo, or shall I post this as a new question?
# declare a hash b. Does it have an initial value?
It is initially empty
# $b{$1} contains what?
for names like folder2 2014-12-34 56;78;90 (Full)
$1 would contains "folder2"
@{$b{$1}} would contain a list associated with folder2
# $f and $i. Are they the (key,value) pair for b?
$_ is the value for b
@{$_} is the list of names in that value
$f is set the first [or last in date order] time we see a (full) backup
$i is set on the next name we see
It is initially empty
# $b{$1} contains what?
for names like folder2 2014-12-34 56;78;90 (Full)
$1 would contains "folder2"
@{$b{$1}} would contain a list associated with folder2
# $f and $i. Are they the (key,value) pair for b?
$_ is the value for b
@{$_} is the list of names in that value
$f is set the first [or last in date order] time we see a (full) backup
$i is set on the next name we see
ASKER
Thank you Ozo. I tested the script and it works, with minor tweaks (it's /volume1 and "-rf").
The delete part takes a long time, so I would like to print the name of the folder being deleted as a "proof of life", so I know the script is still running.
Would this work?
The delete part takes a long time, so I would like to print the name of the folder being deleted as a "proof of life", so I know the script is still running.
Would this work?
perl -e 'for( @ARGV ){
my %b;
m#(.*?)\s\d{4}-\d\d-\d\d\s\d\d;\d\d;\d\d\s\((Full|Incremental)\)# && unshift @{$b{$1}},$_ for grep -d, <"$_"/*>;
for( values %b ){
my($f,$i);
for (grep {$i|=$f,$f|=/\(Full\)$/,$i}@$_) {
system "echo", "Deleting $_";
system "rm","-rf",$_ ;
}
}
}' /volume1/homes/*
Yes, although
print "Deleting $_\n";
may be easier.
print "Deleting $_\n";
may be easier.
It probably takes a while deleting since it does multiple "rm file" commands.
It would be a lot faster if you issue a "xargs | rm" command.
It would be a lot faster if you issue a "xargs | rm" command.
ASKER
Thank you.