I got a directory structure like this:
/backups/
/backups/db/
/backups/db/01.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
/backups/db/02.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
/backups/db/03.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
/backups/db/04.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
Now Im running this command:
find /backups/db/ -mindepth 1 -mtime +2 -delete && rsync ...
When I execute this command directly in the shell, it works and the result is:
/backups/
/backups/db/
/backups/db/03.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
/backups/db/04.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
If I run this command by a cronjob, the directories wont be deleted and the result is:
/backups/
/backups/db/
/backups/db/01.01.2020/
/backups/db/02.01.2020/
/backups/db/03.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
/backups/db/04.01.2020/domain_a.sql.gz, domain_b.sql.gz, domain_c.sql.gz
This happens cause find
first delete the files within these (2) folders and this action change the date of these folders, so that they dont match anymore the -mtime +2
.
Is there any solution? I mean I could go with something like this, but I usually dont like to work with rm if its not necessary.
find /backups/db/ -mindepth 1 -mtime +6 -type d -exec rm -r {} +
find (GNU findutils) 4.7.0-git