Reputation: 787
I have this in a cron job to remove databases older than 30 days:
find /my/backup/path/* -mtime +30 -exec rm {} \;
How can I modify this to only delete the files if the backup was not taken on the first of the month?
E.G. I want have a daily backup of databases (for one month only) PLUS a backup for each month:
Any ideas how I can do this?
Upvotes: 0
Views: 522
Reputation: 531400
-exec
is a filter, too: the exit status of the executed command is the result of the primary. I'll adapt Alfe's answer to demonstrate:
date_is_not_the_first () {
[ "$(stat -c %y "$1" | cut -c9-10)" != 01 ]
}
find /my/backup/path/* -mtime +30 -exec bash -c 'date_is_not_the_first "$1"' _ {} \; -delete
The -delete
option only applies if -mtime
is true and the -exec
command has a zero exit status.
Upvotes: 0
Reputation: 59446
rm_if_not_on_1st() {
[ "$(stat -c %y "$1" | cut -c9-10)" = "01" ] || rm "$1"
}
export -f rm_if_not_on_1st
find /my/backup/path/* -mtime +30 -exec bash -c 'rm_if_not_on_1st "$1"' _ {} \;
Upvotes: 1
Reputation: 129
I can't think of a way to modify your "find" command/cron job but you could:
create another cron job that moves the monthly backup file (taken on 1st of month) to another directory called say /my/backup/monthly_backups. Then when your find / remove cron job runs it won't find the one taken on 1st of the month
another option is to modify the backup script to name monthly backups differently so that they then don't match the "find" criteria in the cron job and so won't be deleted.
Upvotes: 0