This guide will show you how to set up automated daily, weekly, and monthly backups on Ubuntu/Debian using a script. We'll achieve this by utilizing the tar, find, and rsync commands, along with cron for scheduling.
The tar command below will generate a compressed archive of the /var/www/html directory and store it in the /mnt/backup/daily/ folder. Meanwhile, the find command will delete any daily backups older than seven days.
tar -zcf /mnt/backup/daily/backup-$(date +%Y%m%d).tar.gz -C /var/www/ html
find /mnt/backup/daily/* -mtime +7 -delete
Once you've adjusted the commands to fit your requirements, save them in a script named backup-daily.sh.
The tar command below will generate a compressed archive of the /var/www/html directory and store it in the /mnt/backup/weekly/ folder. Meanwhile, the find command will delete any daily backups older than 31 days or 1 month.
tar -zcf /mnt/backup/weekly/backup-$(date +%Y%m%d).tar.gz -C /var/www/ html
find /mnt/backup/weekly/* -mtime +31 -delete
Once you've adjusted the commands to fit your requirements, save them in a script named backup-weekly.sh.
The tar command below will generate a compressed archive of the /var/www/html directory and store it in the /mnt/backup/monthly/ folder. Meanwhile, the find command will delete any daily backups older than 365 days or 1 year.
tar -zcf /mnt/backup/monthly/backup-$(date +%Y%m%d).tar.gz -C /var/www/ html
find /mnt/backup/monthly/* -mtime +365 -delete
Once you've adjusted the commands to fit your requirements, save them in a script named backup-monthly.sh.
To automate the execution of your daily, weekly, and monthly backup scripts, you can create a cron job. Launch the cron editor using the crontab -e command and add the following lines, ensuring you specify the complete path to your script files.
15 0 * * * sh /mnt/backup-daily.sh
30 0 * * 1 sh /mnt/backup-weekly.sh
45 0 1 * * sh /mnt/backup-monthly.sh
In case you’re not familiar with cron syntax, this is what we’re doing:
It's wise to copy your local backup files to another system as an extra layer of protection. This ensures you still have access to your data if the current system is compromised or becomes inaccessible.
You can use the rsync command to create a mirror copy of your local backup directory, including all its contents.
rsync -a --delete /mnt/backup/ [email protected]:/path/to/remote/backups/
Take note of the --delete flag in the command. This ensures that backups removed locally using the find command are also deleted on the remote server, keeping /mnt/backup/ on your local system identical to /path/to/remote/backups/ on the remote system.
To run the rsync command without entering a password, you'll need to set up public key authentication on the remote server. If you're unfamiliar with this process, you can refer to a tutorial or video for guidance.
Finally, set up a cron job to automate the execution of the rsync command.
0 2 * * * rsync -a --delete /mnt/backup/ [email protected]:/path/to/remote/backups/
The rsync command will run daily, scheduled to execute after all backups have been created.
Da Samuel Agustjik Mahardhika | March 24, 2025
Se cercate di più: offriamo potenti root server, game server, web hosting, server TeamSpeak e opzioni di rivendita.
Per saperne di più sui nostri prodotti