Remove backups older than 7 days except first of every month
Solution 1
This works for me under Debian/Ubuntu (and should work under most) :
For example if you have file(s) dated in the following format:
mybackup_12-01-2015.tar.gz
$ find ./* -name 'mybackup_??-01-*' -prune -o -mtime +7 -exec ls {} \;
Command:
- Find all files starting from current folder
- Prune (disregarding/excluding) files with a '01' date match
- and only including files of ≥7 days (mtime)
- Execute
ls
to show what's left.
If you want to delete them after testing command, just replace
ls
withrm
.
When you are happy with the command, it also works well as a cron task/job. I have it execute my script once every couple months. (my script uses a mtime of +90 days to cleanup mysql backups)
Solution 2
Deleting files older than 7 days except first of month.
find . ! -name '*01.tar.gz' -mtime +7 -exec rm {} \;
This does:
find .
: Execute find in the current directory. Replace.
with any other directory.!
(equivalent to-not
): Reverse the next operation.-name '*01'
: Finds files that ends with01.tar.gz
. In my case files ended with date inYYYYMMDD
format, so this filters out first file of every month. Quotes are required so bash doesn't expand the asterisk. This filter is negated thanks to the previous!
.-mtime +7
: Filter files that are older than 7 days.-exec rm {} \;
: Executesrm
on every single selected file. Remove this option when you're testing to get the list of selected files.
Deleting directories older than 7 days except first of month.
In my case my mongodb backups were stored in separated directories. To purge the old files I executed this:
find . -maxdepth 1 ! -name 'db_backup_*01' -mtime +7 -exec rm -r {} \;
The main changes:
-maxdepth 1
: This limits the directory depth search to 1, meaining the search will be limited to the current directory.-exec rm -r {} \;
: To delete directories,-r
parameter is required torm
.
Related videos on Youtube
guest
Updated on September 18, 2022Comments
-
guest almost 2 years
I want to delete backups older than 7 days but keep the first of every month.
The name of the files is:
name_$(date +\%d\%m\%Y).tar.gz
I want to keep:
name_$(date +\01\%m\%Y).tar.gz
I want something like this:
find /path -mtime +8 -exec rm {} \;
Where can I fit in
name_$(date +\01\%m\%Y).tar.gz
?Maybe using
--exclude name_$(date +\01\%m\%Y).tar.gz
I'm looking for a one line command.
Don't want subdirectories.
Tried it with echo it doesn't work fine but it shows somw of the 01.
the
--exclude
just doesn't work:command not found
-
GambleNerd over 8 yearsHow about scripting out a move of the "keeper files" that have the "01" characters in the file name to a sub-folder first just beneath where all the other that are to be non-recursively purged after a certain age like
/01Monthly
,/BackupsRetained
, etc. and then have your other logic do the rest without the exclusion or--exclude
switch? This way your01
files are always moving first per the same script that purges and those are always in this other "standard" sub-folder within that same root folder than you purge the 7 day old files from within only that folder and not recursively. -
Hastur over 8 yearsIt doesn't work
--exclude name_01*.tar.gz
? Try withecho
instead ofrm
before... :) -
Paul over 8 yearsI think what you want is something like -prune -o -name 'name_01*.tar.gz' stackoverflow.com/questions/5659516/…
-