1

I have a backup script which creates a backup (in tar.gz) of my /var/www-folder in my CIFS-mount-folder.

To delete old backups which just waste memory I have created a cronjob that checks every day if a archive is older than 20 days and if necessary it deletes the specific tar.gz-files.

Here is my cron:

0 1 * * * find /my/cifs/mnt/path/*.tar.gz -mtime +20 -exec rm {} \;

Is that right? Not that there is any error in it, which deletes e.g. all files on the hard drive which are older than 20 days and not just the files in the backup folder.

1

Usually you would use the -name option:

 0 1 * * * find /my/cifs/mnt/path/ -name '*.tar.gz' -mtime +20 -exec rm {} \;

Also, ensure that you are finding files:

 0 1 * * * find /my/cifs/mnt/path/ -name '*.tar.gz' -type f -mtime +20 -exec rm {} \;

By splitting off the -name option, you will find files in the given directory as well as in subdirectories. Also, if there were no archives, then find would report an error (rather than simply moving on without action).

Finally, you could handle filenames with unusual characters (such as spaces) by changing the ending of the command:

 0 1 * * * find /my/cifs/mnt/path/ -name '*.tar.gz' -type f -mtime +20 -exec rm {} +
  • Okay, I see. but my version works also, or? Is that just a "elegant" way do this? – fkin Mar 6 '16 at 14:57
  • partly: if you have no archives, then there's a problem. – Thomas Dickey Mar 6 '16 at 15:00
  • Regarding to your last edit: Are "." and "-" unusual characters? I create my backup in the following syntax: (d).(month).(year)-(hour).(minute).tar.gz (e.g. 06.03.16-16.00.tar.gz) – fkin Mar 6 '16 at 15:14
  • no - those are okay. I added the last for completeness. – Thomas Dickey Mar 6 '16 at 15:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.