Linux Server Administration
Automating Server Backups Using Cron Jobs
A complete step-by-step guide to scheduling automatic daily backups on your Linux server — so your data is always protected, without any manual effort.
Why does server backup matter?
Anything can go wrong on a production server — an accidental file deletion, a security breach, or hardware failure. Without a backup, everything is gone for good. By setting up a cron job, your server data will be automatically saved every day without you having to do anything manually.
The first step is connecting to your remote Linux server. This is done using SSH (Secure Shell) — an encrypted protocol that lets you control a remote machine securely from your local terminal.
ssh root@your-server-ip
Once logged in, your terminal will show the server’s command prompt. You’re now ready to proceed.
Cron is a Linux time-based job scheduler that runs commands or scripts automatically at defined intervals. Most servers come with it pre-installed, but it’s worth checking before you proceed.
Check Cron Service:
Ubuntu / Debian:
- systemctl status cron
CentOS / RHEL:
- systemctl status crond
If you see active (running) in the output, cron is already running and you can skip to Step 3. If not, install it using the commands below.
Ubuntu / Debian:
- apt update
- apt install cron -y
- systemctl enable cron
- systemctl start cron
CentOS / RHEL:
- yum install cronie -y
- systemctl enable crond
- systemctl start crond
Next, create a dedicated folder to store all your backup archives. The permission 700 ensures only the root user can read or write to this directory — a critical security measure for protecting your backups.
- mkdir -p /backup/server
- chmod 700 /backup/server
Now we’ll create a shell script that does the actual backup work. This script compresses your web directory into a .tar.gz archive and automatically deletes backups older than 7 days — keeping your disk usage in check.
nano /root/backup.sh
The editor will open. Paste the following script:
- #!/bin/bash
- SOURCE=”/var /www “
- BACKUP=”/backup/server”
- DATE=$(date +%Y-%m-%d-%H-%M)
- tar -czf $BACKUP/backup-$DATE.tar.gz $SOURCE
- find $BACKUP -type f -mtime +7 -delete
- Save and exit: Press CTRL + X → Y → Enter
Here’s what each part of the script does:
- SOURCE — the directory you want to back up
- BACKUP — where the backup archives will be stored
- DATE — a timestamp used to create unique filenames for each backup
- tar -czf — compresses the source directory into a .tar.gz archive
- find … -delete — removes any backup files older than 7 days automatically
By default, newly created files on Linux are not executable. You need to explicitly grant execute permission before the script can be run — either manually or by cron.
chmod +x /root/backup.sh
Before setting up any automation, always test your script manually. This confirms the script works correctly and catches any issues before they silently fail in production.
/bin/bash /root/backup.sh
Now check whether the backup file was created:
- ls -l /backup/server
- If successful, you’ll see something like this:
- -rw-r–r– 1 root root 45231 2025-07-15 10:32 backup-2025-07-15-10-32.tar.gz
✅ File created successfully — your script is working perfectly!
Now it’s time to automate everything. Cron uses a special file called a crontab to store scheduled tasks. Open it for editing with:
crontab -e
If this is your first time, you’ll be prompted to choose a text editor — select nano (option 1) as it’s the easiest for beginners.
Add the following line at the bottom of the file:
0 2 * * * /bin/bash /root/backup.sh
Save and exit: CTRL + X → Y → Enter
Understanding Cron Syntax — there are 5 fields:
* * * * *
| | | | |
| | | | └── Day of week
| | | └──── Month
| | └────── Day of month
| └──────── Hour
└────────── Minute
The final step — confirm that your cron job has been registered correctly. This command lists all scheduled jobs for the current user.
crontab -l
You should see your job listed like this:
0 2 * * * /bin/bash /root/backup.sh
🎉 Congratulations! Your server backups are now fully automated.

