Automated Backup System
Overview
This automated backup system, written in Bash, creates efficient incremental backups of important directories such as /etc, /var/log, and /home. It uses rsync with hard-linking to save space, rotates old backups, cleans logs, and ensures data is safely written to disk using sync.
The backup runs automatically through systemd service and timer units, scheduled to execute daily at 15:00 (3:00 PM).
Components: backup.sh (script), backup.service and backup.timer (systemd unit files for automation)
- backup.sh
Main backup script located at:
/usr/local/bin/backup/backup.sh
Responsibilities: - Creates incremental backups using rsync --link-dest. - Excludes its own backup folder to avoid recursion. - Logs operations and timestamps into /usr/local/bin/backup/logs/. - Cleans up backups and logs older than 7 days. - Flushes filesystem buffers at the end with sync.
Safe practices included: - set -euo pipefail to exit on errors and catch undefined variables. - trap to log and identify the exact line that failed. - Symbolic links like *_latest for easy reference to the newest backup.
- backup.service
Systemd service unit controlling script execution.
Located at:
/etc/systemd/system/backup.service
Contents:
[Unit]
Description=Automated backup service
Wants=backup.timer
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup/backup.sh
User=root
Group=root
WorkingDirectory=/usr/local/bin/backup
[Install]
WantedBy=multi-user.target
Explanation: - Type=oneshot: Runs the script once and exits. - ExecStart: Defines the script path. - WorkingDirectory: Ensures relative paths resolve correctly. - User=root: Backups typically require root privileges. - WantedBy: Allows enabling service manually if desired.
- backup.timer
Systemd timer unit for scheduling.
Located at:
/etc/systemd/system/backup.timer
Contents:
[Unit]
Description=Run backup script every day at 15:00 / 3:00PM with persistence
[Timer]
OnCalendar=*-*-* 15:00:00
Persistent=true
[Install]
WantedBy=timers.target
Explanation: - OnCalendar=--* 15:00:00: Runs daily at 3 PM. - Persistent=true: If the system was off during the scheduled time, it runs immediately on boot. - WantedBy=timers.target: Makes it auto-startable on system boot.
Setup Instructions
1- Copy Script and Create Directory
sudo mkdir -p /usr/local/bin/backup
sudo cp ~/Desktop/backup/backup.sh /usr/local/bin/backup/
sudo chmod +x /usr/local/bin/backup/backup.sh
2- Create Unit Files
sudo nano /etc/systemd/system/backup.service
sudo nano /etc/systemd/system/backup.timer
Paste the unit contents provided above.
3- Reload Systemd
sudo systemctl daemon-reload
4- Start and Enable the Timer
sudo systemctl start backup.timer
sudo systemctl enable backup.timer
5- Verify
systemctl list-timers --all
You should see an entry similar to:
NEXT LEFT LAST PASSED UNIT ACTIVATES
Wed 2025-10-15 15:00:00 EEST 2h left Tue 2025-10-14 15:00:00 EEST 22h ago backup.timer backup.service
To view logs:
journalctl -u backup.service --since today
Log and Backup Rotation
- Old backups older than 7 days are automatically deleted.
- Logs older than 7 days are cleaned as well.
- Latest log is always symlinked to: /usr/local/bin/backup/logs/new_backup.log_latest
Notes for Administrators
-
Manual Run: You can run it anytime using:
sudo systemctl start backup.service -
Testing: Test safely with small dummy directories before backing up /home or /etc.
-
Data Safety: The sync command ensures that all backup data is flushed from RAM to disk before completion.
-
Modifying Schedule: Adjust OnCalendar in backup.timer to fit your preferred frequency.
Author
Sami Hajji Computer Engineer | System Administrator | Cloud & DevOps Enthusiast