Script to delete old backup file?(Acronis, OO DiskImage, etc)

Status
Not open for further replies.

titan_rw

Guru
Joined
Sep 1, 2012
Messages
586
Hi.

Maybe I'm not seeing something, but I'm wondering about the syntax of this command:

Code:
ls -ltd1 /mnt/tank/Backups/Desktop/* | grep ".*\.omg" | tail +3 > /tmp/list && xargs rm < /tmp/list


What I'm wondering is, 1: why use grep when you can simply include the wildcard in the original 'ls' command. And 2: Why use a temporary file at all?

I use Acronis, so I've used a directory of ".tib" files as an example.

I would do this:

Code:
ls -td1 /mnt/backup/somewhere/*.tib | tail +3


(note -l is redundant when using -1)

This lists the files to be deleted, one per line.

All we need to do is delete these files, right?

Code:
rm -f `ls -td1 /mnt/backup/somewhere/*.tib | tail +3`


This will run "rm -f " on every file returned by the "ls | tail" pipe.

Not actually tested on my files, as Acronis rotates old backups for me. But I tested this one:

Code:
ls -l `ls -td1 /mnt/backup/somewhere/*.tib | tail +3`


Which properly "ls'd" the files to be deleted.

Anyway, that's how I would do it. The beauty of *nix (linux, unix, bsd, etc). There's more than one way to do it.
 

HolyK

Ninja Turtle
Moderator
Joined
May 26, 2011
Messages
654
You are right in all points i guess. If you will read this thread from start, you will see much more complicated commands, they were modified several times which would probably also work. Then i posted shorter way how to achieve the needed results, then it was again slightly modified. Now you come with even shorter and easier way. It is exactly how you said ... The beauty of *nix (linux, unix, bsd, etc). There's more than one way to do it. :]

By the way ... probably this is not that case (i tested your command and don't see any gaps), but i don't like wildcards in the middle of one command, i had a baaaaad issue with this in past (maybe because i am more like a Windows person).

And cyberjock can rewrite his cron job in Notepad++ one more time ;) ;)
 

titan_rw

Guru
Joined
Sep 1, 2012
Messages
586
I don't see a problem with having a wildcard in the middle. In fact the original command has a wildcard in exactly the same spot. It's just a wildcard for every file, then text processing to limit files after. Also be aware of what your regular expression will match that you might not want it to match. grep ".*\.omg" will match a filename "IndexOf.omgs.txt". It will also match ".omg files are cool.jpg". If you want to use a regular expression there, I'd use "\.omg$". This is assuming the extensions will always be lower case. If not, then using "\.[oO][mM][gG]$" will make it case insensitive. There may be another way to case desensitize this, but I know that will work. Long story short: I prefer the wildcard.

In a script, I would probably use variables so you can keep the variable definition at the top, and more clear.

I'd probably also use 'find' instead of 'ls' for getting a list of files. Easier to be more 'exact' on what you want. find with "-iname ", "-type f", "-maxdepth 1" and "-mindepth 1" would work. However I don't see an easy way for find to sort by date. Using ls works, but it's going to include any directories that match the wildcard too. That probably won't be a problem, but be aware of your file structure. Because we're not using a recursive rm, matched directories won't be deleted. However, if the directories are sorted as 'new' it might 'keep' the directories and delete backups that you want. Example directory that will be matched with ls combined with the grep above: "keep these .omg backups/"

A different approach would be to use find to make sure there's 2 files that are 2 days old or newer. If so, use find again to delete files older than 2 days. If not, email someone that backups may not have run properly. This would also handle the case of manually run backups better. There could be 3 backups in 2 days if one was kicked off manually. Deleting files older than 2 days only if there's at least 2 files 2 days old or newer would keep the 3 backups. (whew, say that 10 times fast).

Most of this is probably moot, but could be one of those things where it works fine for 2 years, then it silently starts deleting all your backups because there's extra files it's matching, or extra directories it's matching. Then 3 years later when you go to restore from a backup you 'know' you have, you find you're fubar'd.
 

titan_rw

Guru
Joined
Sep 1, 2012
Messages
586
Thinking about this again, and I suppose if you wanted to avoid using command substitution, you could still use xargs, without the temporary file.

This should work:

Code:
ls -td1 /mnt/backup/somewhere/*.tib | tail +3 | xargs rm -f


Caveats: Will only match backups with lowercase extensions. Will match directories that END in ".tib".
 

HolyK

Ninja Turtle
Moderator
Joined
May 26, 2011
Messages
654
titan_rw: Thanks, plenty of good things in your post. And you are right about the <whatever>.omg<something>, did not checked this situation. :o
 
Status
Not open for further replies.
Top