optimusproweb

Registered
Jul 23, 2019
2
0
1
Europe
cPanel Access Level
Website Owner
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!
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,295
1,255
313
Houston
Have you tried using -delete instead? If you run that command manually do you not get an error?

Code:
find /home/$user/backupdir/ -name "*.sql" -type f -mtime +4 -delete
I tested this on my personal server and can confirm it works.
 
May 14, 2018
12
0
1
India
cPanel Access Level
Reseller Owner
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