Backup files cleanup

Hello,

I've been able to keep a certain number of backup files with the find -mtime command, but is there a way to add the last 4 Sunday's or any other day?

I checked the man page and forums, but couldn't find anything.

Any help would be appreciated.

Thanks

How are your files organised? If there is a difference in the filename to clearly mark a Sunday file, then it should be easy but you would need to explain a little more what files you have first.

Can you show us the output from an ls -l of the files in question that you want to work with?

Thanks,
Robin

Here's the file list:

Sep 20 00:50 database_backup_201609192130.zip
Sep 19 00:51 database_backup_201609182130.zip
Sep 18 00:49 database_backup_201609172130.zip
Sep 17 00:51 database_backup_201609162130.zip
Sep 16 00:51 database_backup_201609152130.zip

The file name doesn't "clearly" mark the day, just the date.

I think rbatte1 was after a clearer explanation of the intended target files' names. Me, I couldn't tell what you're after when reading both your posts.

I agree with RudiC, your input and list of files do not match and not clear .

Did you mean to list the files ?

man find ( search for -printf )
%Tk    File's last modification time in the format specified  by
                     k, which is the same as for %A.
a      locale's abbreviated weekday name (Sun..Sat)

Here is the example, i tried to simulate. Hope it helps.

Create the files:

touch f1 -t 201609180945
touch f2 -t 201609111045
touch f3 -t 201608281230

List files:

ls -l
-rw-rw-r-- 1 user1 user1    0 Sep 18 09:45 f1
-rw-rw-r-- 1 user1 user1    0 Sep 11 10:45 f2
-rw-rw-r-- 1 user1 user1    0 Aug 28 12:30 f3
-rw-rw-r-- 1 user1 user1    0 Sep 20 22:33 f4

This prints last 2 sunday files

find . -type f -printf '%Ta\t%p\n' | grep Sun | tail -2

output:

Sun    ./f2
Sun    ./f1

Change the weekday name and number as per your need.

If this is not what you want, please provide input files , expected output whether you want to list last four sunday based on date information present in filename or 'file modification time' etc.

Ok just to make it clearer.

The backup happens every night.
The file name shows the date.
I have a script that keeps the last 5 files.
I want it to also keep the last 4 Sundays on top of the last 5 files.

Thanks

There's a rich abundance of backup schemes on the net, and even in these forums you might find some that you could adapt to suit your needs.
Looks like you want to save sort of 5 daily backups and another 4 weekly ones?
How about saving the Sundays' backups in a separate directory and keep the latest 4, resp. delete those more than 4 weeks old?

In an earlier OS, using a backup package to create the file I used cron to step it through the directory queue:
backup file created at 3:01 AM
at 4:01 I cleared the backup4 directory
at 4:15 I moved backup3 contents to backup4
at 5:15 I moved backup2 contents to backup3
at 6:15 I moved backup1 contents to backup2
at 7:15 I moved the current backup to backup1

for the weekly it would have been a matter of copying the Sunday backup to a similar queue.

It helped that I put the entire backup system on it's own file system on it's own hard drive and that the daily backup was burned to CD first. Over 15 years and as many as 6 machines I only have had 1 issue where this failed me and I had to reconstruct from CD.

I was able to get my client to agree to have a seperate folder for the weekly backups.

Changed crontab to copy every sunday both in daily and a weekly.

Thanks