Automated File Backups with Linux Bash Scripting: A Simple Step-by-Step Guide
How to protect your data automatically using tar, rsync, and cron.
Losing important files to a crashed drive, a bad update, or a simple rm -rf mistake is one of the most painful experiences in system administration. The good news is that you don't need a paid backup product to protect your data on Linux. A short bash script combined with cron can back up your files automatically, every day, without you ever touching the terminal again.
This guide walks through building a working backup script from scratch, scheduling it, and making it reliable enough to trust with real data.
Why Bash Scripting Beats GUI Backup Tools
One-click backup apps are convenient, but they hide what’s actually happening to your files. A bash script gives you full transparency and control: you decide exactly what gets backed up, where it goes, how long it’s kept, and what happens if something fails. It also runs on any Linux server, a VPS, a home server, or a Raspberry Pi, with zero extra software beyond tools that are already installed.
The two core utilities used in almost every Linux backup script are:
- tar: bundles and compresses files into a single archive, ideal for point-in-time snapshots of directories like websites or configuration files.
- rsync: copies only the files that changed since the last run, which makes it fast and efficient for large datasets or remote destinations.
Many production setups combine both: rsync mirrors files to a staging folder, then tar compresses that folder into a dated archive.
Building the Backup Script
Here’s a simple, practical bash script that archives a directory, timestamps the backup, logs the result, and removes old backups automatically.
Create a file:
nano ~/backup.shPaste the script and edit SOURCE_DIR/BACKUP_DIR to your real paths:
#!/bin/bash
# ---- Configuration ----
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/home/user/backups"
RETENTION_DAYS=7
DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_NAME="backup_${DATE}.tar.gz"
LOG_FILE="${BACKUP_DIR}/backup.log"
# ---- Prepare ----
mkdir -p "$BACKUP_DIR"
# ---- Create the backup ----
echo "$(date): Starting backup of $SOURCE_DIR" >> "$LOG_FILE"
tar -czf "${BACKUP_DIR}/${BACKUP_NAME}" -C "$SOURCE_DIR" .
if [ $? -eq 0 ]; then
echo "$(date): Backup completed successfully -> ${BACKUP_NAME}" >> "$LOG_FILE"
else
echo "$(date): Backup FAILED" >> "$LOG_FILE"
exit 1
fi
# ---- Clean up old backups ----
find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +"$RETENTION_DAYS" -delete
echo "$(date): Old backups older than $RETENTION_DAYS days removed" >> "$LOG_FILE"Save and exit.
This structure, configuration variables, an archive step, a success/failure check, and a cleanup step, mirror the pattern used in most reliable backup scripts, including retention-based cleanup with find -mtime.
Making It Executable
Grant required permission to the file to run:
bashchmod +x ~/backup.shTest it manually before trusting it to run unattended:
bash./backup.sh
cat ~/backups/backup.logIf the log shows “Backup completed successfully,” the script is ready to be scheduled.
Adding Remote Backups with Rsync
Local backups protect you from accidental deletion, but not from hardware failure. Sending a copy off server closes that gap. Add a rsync step to push the archive or the whole backup folder to a remote host over SSH:
rsync -avz -e "ssh -i /home/user/.ssh/id_rsa" \
"${BACKUP_DIR}/${BACKUP_NAME}" \
backupuser@remote-server:/backups/For syncing entire directories incrementally instead of shipping single archives, rsync’s --delete flag keeps the remote copy an exact mirror of the source, and --link-dest can be used to create space-efficient, hard-linked snapshots so only changed files consume extra storage.
Scheduling the Script with Cron
A backup script is only “automated” once it runs on its own. cron is Linux's built-in job scheduler for exactly this purpose.
Get Emilyharbord’s stories in your inbox
Join Medium for free to get updates from this writer.
Open the crontab editor:
crontab -eAdd a line to run the script daily at 2:00 AM:
0 2 * * * /home/user/backup.sh >> /home/user/backups/cron.log 2>&1Common scheduling patterns worth knowing:
0 2 * * *— every day at 2:00 AM30 3 * * 0— every Sunday at 3:30 AM (good for a weekly full backup)0 4 1 * *— the 1st of every month at 4:00 AM (monthly archive)
Many admins layer these together: daily incremental rsync runs, a weekly full tar archive, and a monthly cleanup job, all managed from the same crontab.
After adding the job, confirm it’s registered and that the cron service is active:
crontab -l
systemctl status cronBest Practices for Reliable Backups
A backup script is useful only if it works when you need it. Follow these simple tips:
- Keep three copies of important data: the original, a local backup, and one backup stored somewhere else.
- Test restoring files regularly. A backup is not reliable until you confirm that you can restore it.
- Encrypt private or sensitive backup files before sending them to another server or cloud storage.
- Set up an email or notification if a backup fails, so you do not find out too late.
- Exclude unnecessary files such as caches, logs, and temporary files to save backup time and storage space.
Tip: For a more advanced setup with archive integrity checks and SHA-256 checksums, check this guide on Bash Automated Backup Integrity Script.
Conclusion
You do not need expensive software to automate Linux backups. A simple Bash script with tar, rsync, and cron can back up important files automatically for free.
Make sure your script saves logs, checks for errors, removes old backups, and is tested by restoring files. Once set up, your server can handle backups automatically without daily manual work.








