How to Set Up a Cron Job for Automatic Backups in Debian 12 Bookworm
Categories:
5 minute read
Backing up your data regularly is one of the simplest and smartest things you can do to protect against data loss. On a Debian 12 Bookworm system, you can automate this process using cron
—a built-in Linux tool that schedules scripts or commands to run at specific times. In this guide, we’ll walk through the steps to set up a cron job that automatically backs up important files or directories on your Debian system.
This tutorial is aimed at users with a basic understanding of Linux and shell commands. Whether you’re backing up files to a local folder, external drive, or remote server, the same general steps apply.
Why Automate Backups?
Manual backups are easy to forget. Automation ensures that backups happen consistently, without human intervention. This is especially important for servers, personal projects, and any critical system that needs regular snapshots.
Benefits of using cron for backups:
- Consistent schedules (e.g., daily, weekly)
- Hands-off operation
- Simple to configure
- Reliable and native to Linux
Prerequisites
Before setting up the cron job, make sure you have the following:
- A system running Debian 12 Bookworm
- A user account with sudo privileges
- Basic knowledge of shell scripting
- A directory or set of files you want to back up
Step 1: Decide What to Back Up
First, identify what needs to be backed up. This could be:
- System configuration files (
/etc/
) - User files (
/home/username/
) - Web server data (
/var/www/
) - Databases (like MySQL or PostgreSQL dumps)
Let’s assume we want to back up the /home/username/documents
folder.
Step 2: Create a Backup Script
We’ll use a simple shell script to handle the backup. This script will:
- Create a compressed archive of the target directory
- Save it to a designated backup folder
- Include a timestamp in the filename for versioning
Create a file called backup.sh
:
sudo nano /usr/local/bin/backup.sh
Add the following content:
#!/bin/bash
# Variables
BACKUP_SOURCE="/home/username/documents"
BACKUP_DEST="/home/username/backups"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DEST/documents_backup_$TIMESTAMP.tar.gz"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DEST"
# Create the backup
tar -czf "$BACKUP_FILE" "$BACKUP_SOURCE"
# Optional: Delete backups older than 7 days
find "$BACKUP_DEST" -type f -name "*.tar.gz" -mtime +7 -exec rm {} \;
# Log message (optional)
echo "Backup completed: $BACKUP_FILE"
Make the script executable:
sudo chmod +x /usr/local/bin/backup.sh
Make sure you replace /home/username/documents
and /home/username/backups
with your actual paths.
Step 3: Test the Script
Before automating, test the script manually:
/usr/local/bin/backup.sh
After it runs, check the backup directory to ensure the .tar.gz
file was created. You can also extract it to confirm the contents:
tar -xzf documents_backup_<timestamp>.tar.gz
If everything looks good, you’re ready to schedule it.
Step 4: Add a Cron Job
Cron uses a special file called a crontab to schedule tasks.
Edit the crontab for the user who owns the files:
crontab -e
Add the following line to run the backup script daily at 2 AM:
0 2 * * * /usr/local/bin/backup.sh >> /home/username/backup.log 2>&1
Breakdown of the cron syntax:
0 2 * * *
– At 2:00 AM daily/usr/local/bin/backup.sh
– The script to run>> /home/username/backup.log 2>&1
– Append output and errors to a log file
You can customize the schedule. Here are some examples:
- Every hour:
0 * * * *
- Every Sunday at 3 AM:
0 3 * * 0
- Twice a day (2 AM and 2 PM):
0 2,14 * * *
To list existing cron jobs:
crontab -l
Step 5: Verify Cron is Working
Cron should already be active on Debian 12. To confirm, check the service:
sudo systemctl status cron
If it’s inactive, start and enable it:
sudo systemctl start cron
sudo systemctl enable cron
You can check your log file after the scheduled time to see if the backup ran:
cat /home/username/backup.log
If it’s empty or missing, check for errors in system logs:
journalctl -u cron
Optional: Backing Up to External or Remote Locations
1. External Drive
If you’re using an external drive, mount it first and update the BACKUP_DEST
path in your script.
To list devices:
lsblk
To mount:
sudo mount /dev/sdX1 /mnt/backupdrive
2. Remote Server via rsync
or scp
You can add a line to your script to copy the archive to a remote server:
scp "$BACKUP_FILE" user@remote-server:/remote/backup/path/
Or with rsync
for more control:
rsync -avz "$BACKUP_FILE" user@remote-server:/remote/backup/path/
You’ll want to set up SSH keys for passwordless access.
Optional: Database Backups
If you’re running services like MySQL or PostgreSQL, add commands to the script to export databases before creating the archive.
MySQL:
mysqldump -u root -p[yourpassword] yourdatabase > /tmp/db_backup.sql
PostgreSQL:
pg_dump yourdatabase > /tmp/db_backup.sql
Then include /tmp/db_backup.sql
in your archive and optionally clean it afterward:
rm /tmp/db_backup.sql
Tips for Better Backups
- Encrypt sensitive backups using
gpg
oropenssl
- Monitor disk space to avoid failed backups
- Store in multiple locations (local + remote/cloud)
- Test recovery procedures periodically
- Use versioning to avoid overwriting critical data
Wrapping Up
Setting up a cron job for automatic backups on Debian 12 Bookworm is straightforward and highly customizable. Once configured, it quietly runs in the background, protecting your data with regular, scheduled backups.
To recap the steps:
- Decide what to back up
- Create and test a backup script
- Add it to cron with your preferred schedule
- Monitor logs and test recovery
This approach can be scaled and adapted to suit desktops, servers, or even Raspberry Pi setups. Backups aren’t glamorous, but when disaster strikes, they’re priceless. Set it and forget it—until you need it.
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.