Auto delete backup cron job
Hello,
I've created two directories, one for database backup and one for public_html backup.
Current cron jobs that works fine, are:
For daily database backup:
mysqldump --opt -Q -u myuser -p'mypassword' --all-databases > /home/myuser/backupdb/databases_$(date +"\%Y_\%m_\%d_\%T").sql
For daily public_html backup:
tar -cvpzf /home/myuser/backupdir/files_$(date +"\%Y_\%m_\%d_\%T").tar.gz /home/myuser/public_html
So, I need cron job commant for auto delete those backups older than 4 days after a new one is generated.
I tried these commands for auto delete the backup of the database and public_html older than 4 days but doesn't work:
find /home/myuser/backupdb/ -name "*.sql" -type f -mtime +4 -exec rm -f
find /home/myuser/backupdb/files* -type f -mtime +4 | xargs rm
Best Regards!
-
Have you tried using -delete instead? If you run that command manually do you not get an error? find /home/$user/backupdir/ -name "*.sql" -type f -mtime +4 -delete
I tested this on my personal server and can confirm it works.0 -
Have you tried using -delete instead? If you run that command manually do you not get an error?
find /home/$user/backupdir/ -name "*.sql" -type f -mtime +4 -delete
I tested this on my personal server and can confirm it works.
I'll try with this code and I'll let you know. Thank you!0 -
Hi We have a script, but need some modifications ---------------------------- 0 4 * * * find /home/user_name/public_html -maxdepth 1 -type f -mtime +7 -name 'backup-*.tar.gz' -delete ---------------------------- We want this to be used - 1. It should look only for backups as - .tar.gz only within the home directory 2. It should run daily and will delete all tar.gz backups 0 days old automatically using Cron Thanks 0
Please sign in to leave a comment.
Comments
3 comments