How to Schedule Backups Using `cron` on Debian 12 Bookworm
Categories:
5 minute read
Keeping regular backups is one of the most important tasks for any system administrator or user who values their data. Whether you’re managing a personal project, a development server, or a production machine, backups ensure that your data can be restored in case of hardware failures, accidental deletions, or system corruption.
In this article, we’ll guide you through scheduling automated backups using cron
, the classic job scheduling tool built into almost every Unix-like system, including Debian 12 Bookworm.
1. Understanding cron
cron
is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.
On Debian 12, the cron
daemon is typically installed and running by default. To check its status, run:
sudo systemctl status cron
If it’s not running, you can start and enable it using:
sudo systemctl start cron
sudo systemctl enable cron
The main configuration file for system-wide cron jobs is /etc/crontab
, but most user-specific jobs are handled through their own crontab entries, editable via:
crontab -e
2. Why Automate Backups with cron
Automating your backups with cron
offers several advantages:
- Consistency: Ensures backups happen at the same time every day/week.
- No Manual Intervention: Once set up, it runs quietly in the background.
- Customizable: You can easily configure it for different backup schedules and directories.
- Low Overhead: Cron is lightweight and doesn’t consume much system resources.
3. Backup Strategy Basics
Before diving into scripts and scheduling, it’s important to define a backup strategy:
What to back up?
Choose critical data:/etc
,/home
, databases, web server content, etc.Where to store backups?
External drive, remote server, cloud storage, or even another directory on the same disk (though not ideal).How often?
Daily for volatile data (e.g., logs, web content), weekly or monthly for system configs.Retention policy?
Decide how many old backups to keep before deleting them.
4. Creating Backup Scripts
We’ll create a simple bash script to back up the /home
directory and compress it into a tar archive. You can adjust it as needed.
Example Backup Script: backup-home.sh
#!/bin/bash
# Variables
BACKUP_SRC="/home"
BACKUP_DEST="/backup"
DATE=$(date +%F)
HOSTNAME=$(hostname)
FILENAME="$HOSTNAME-home-$DATE.tar.gz"
LOGFILE="/var/log/backup.log"
# Create destination directory if it doesn't exist
mkdir -p "$BACKUP_DEST"
# Create the backup
tar -czf "$BACKUP_DEST/$FILENAME" "$BACKUP_SRC" >> "$LOGFILE" 2>&1
# Log completion
echo "[$(date)] Backup of $BACKUP_SRC completed and saved to $FILENAME" >> "$LOGFILE"
Make It Executable
chmod +x /usr/local/bin/backup-home.sh
You can place this script anywhere, but /usr/local/bin/
is a good standard location for custom scripts.
5. Using cron
to Schedule Backups
Once your script is ready and working, it’s time to schedule it using cron
.
Open Crontab
For the current user:
crontab -e
Or to edit the root user’s crontab (for system-wide backups):
sudo crontab -e
Cron Syntax
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of week (0 - 7) (Sunday = 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Example: Daily at 2:30 AM
To run your backup script daily at 2:30 AM:
30 2 * * * /usr/local/bin/backup-home.sh
This will compress the /home
directory and store the archive in /backup
.
6. Verifying and Managing cron
Jobs
List Existing Cron Jobs
crontab -l
For root:
sudo crontab -l
Remove a Cron Job
To remove a job, edit your crontab again:
crontab -e
Then simply delete the line or comment it out with #
.
Check Cron Logs
Debian 12 logs cron activity in /var/log/syslog
. You can filter logs like this:
grep CRON /var/log/syslog
Or to see entries related to your backup script:
grep backup-home.sh /var/log/syslog
7. Backup Examples
Here are a few more practical backup examples to consider:
7.1 Weekly Full System Backup
0 3 * * 0 /usr/local/bin/full-backup.sh
This runs every Sunday at 3:00 AM.
7.2 Backup MySQL Databases
Script: backup-mysql.sh
#!/bin/bash
DATE=$(date +%F)
BACKUP_DIR="/backup/mysql"
mkdir -p "$BACKUP_DIR"
mysqldump -u root -pYourPassword --all-databases | gzip > "$BACKUP_DIR/mysql-$DATE.sql.gz"
Schedule it with cron:
0 4 * * * /usr/local/bin/backup-mysql.sh
7.3 Rotate Old Backups
Add this line in your backup script to delete files older than 7 days:
find "$BACKUP_DEST" -type f -name "*.tar.gz" -mtime +7 -exec rm {} \;
8. Security and Best Practices
Scheduling backups is powerful, but it must be done responsibly:
- Use absolute paths in scripts (cron doesn’t load user profiles).
- Protect backup scripts and logs using proper permissions.
- Avoid hardcoding sensitive credentials. Instead, use environment variables or configuration files with limited access.
- Test your backup and restore process regularly.
- Use encryption if backups are stored on external or remote systems.
- Monitor available disk space to prevent backup failures.
9. Conclusion
Automating backups using cron
on Debian 12 Bookworm is a practical and reliable way to protect your data. By combining shell scripting with cron scheduling, you can tailor a backup system to meet your needs — whether for daily home directory backups, weekly system snapshots, or regular database dumps.
With a good backup strategy, proper automation, and some attention to security, you’ll ensure that your valuable data is safe, restorable, and managed with minimal effort.
Remember: the best backup strategy is the one you actually implement and verify regularly.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.