How to Set Up Automatic Backups in Linux
Introduction
Automatic backups are essential to protect your data from accidental loss or server failure. This guide shows you how to set up scheduled backups in Linux using cron and simple scripts.
Step-by-step Guide
- Create a backup script
Create a file namedbackup.shwith the following content:n- #!/bin/bash
- tar -czf /home/backup/www-$(date +\%F).tar.gz /var/wwwn
- Make it executable:n
- chmod +x backup.sh
- Make it executable:n
- Test your script manually
Run the script to verify it works: n- ./backup.shn
- Check the backup file:n
- ls -lh /home/backup/
- Check the backup file:n
- ./backup.shn
- Edit your crontab to schedule the backup
Open the crontab editor:n- crontab -en
- Add this line to run the backup every day at 2:00 AM:n
- 0 2 * * * /ruta/completa/a/backup.sh
- Add this line to run the backup every day at 2:00 AM:n
- crontab -en
- (Optional) Automate backup cleanup
Add this line to your script to delete backups older than 7 days:n- find /home/backup -type f -mtime +7 -name "*.tar.gz" -delete
- (Optional) Send backups to a remote server
Add this line to your script to copy the backup to another server:n- scp /home/backup/www-$(date +\%F).tar.gz user@remote_ip:/remote/backup/path/
Recommendations
- Store backups on a separate disk or remote server for extra safety.
- Regularly test your backups by restoring files.
- Monitor disk space to avoid failed backups.
Call To Action
eed reliable storage for your backups? Check out our VPS plans! https://xernode.com/#pricing