Overview
Cron is the standard Linux scheduler. By adding a cron job you can run a backup script unattended — for example, every night at 02:30 — so you never rely on remembering to do it manually. This article shows how to wrap an rsync backup in a script and schedule it.
Before you begin
- A working manual backup command (see [Backing up your server with rsync]).
- Shell access with sudo, and a text editor.
- An ALYPOD Storage Box or S3 bucket as the destination.
Steps
- Create a script at
/usr/local/bin/alypod-backup.shand make it executable:
```bash
!/usr/bin/env bash
set -euo pipefail
LOG=/var/log/alypod-backup.log DEST="[email protected]:/backup/server1/" DATE="$(date +%F)"
echo "=== Backup started $DATE ===" >> "$LOG" rsync -avz --delete \ --exclude '/cache/' --exclude '*.sock' \ /var/www /etc \ "$DEST" >> "$LOG" 2>&1 echo "=== Backup finished $(date +%F' '%T) ===" >> "$LOG" ```
bash
sudo chmod +x /usr/local/bin/alypod-backup.sh
- Test it manually before scheduling:
bash
sudo /usr/local/bin/alypod-backup.sh
tail -n 20 /var/log/alypod-backup.log
- Open the crontab for root:
bash
sudo crontab -e
- Add a schedule. This runs daily at 02:30 and emails output if
MAILTOis set:
cron
30 2 * * * /usr/local/bin/alypod-backup.sh >/dev/null 2>&1
- Save and exit. Confirm the entry was stored:
bash
sudo crontab -l
- Check that the cron daemon is running and enabled:
bash
systemctl status crond 2>/dev/null || systemctl status cron
Troubleshooting
- Problem: Job never runs. Solution: Ensure the cron service is active and the
script path is absolute.
journalctl -u cronshows execution errors. - Problem: "Permission denied" placing the SSH key. Solution: Cron runs with a
minimal environment; specify the key explicitly with
-e "ssh -i /root/.ssh/alypod_backup"inside the rsync command. - Problem: Overlapping runs on large datasets. Solution: Use
flock:flock -n /tmp/backup.lock /usr/local/bin/alypod-backup.sh. - Problem: No output/log. Solution: Redirect stdout and stderr inside the script, as shown, because cron discards them by default.
Related articles
- [Backing up your server with rsync]
- [Restoring data from a backup]
- [Backup best practices (the 3-2-1 rule)]
