0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Schedule Backup with Cron Job

Create a Bash script with backup commands, then schedule it using crontab -e by adding a line like 0 2 * * * /path/to/backup.sh to run daily at 2 AM.
📋

Examples

InputBackup /home/user/documents to /backup/documents daily at 2 AM
OutputCron entry: 0 2 * * * /home/user/backup.sh Backup script copies files from /home/user/documents to /backup/documents
InputBackup /var/www to /mnt/backup weekly on Sunday at 3 AM
OutputCron entry: 0 3 * * 0 /home/user/backup.sh Backup script archives /var/www and saves to /mnt/backup
InputBackup /etc config files every hour
OutputCron entry: 0 * * * * /home/user/backup.sh Backup script copies /etc files to backup location
🧠

How to Think About It

First, write a Bash script that copies or archives the files you want to back up. Then, use the cron scheduler by editing the crontab file with crontab -e to run your script automatically at the times you choose.
📐

Algorithm

1
Write a Bash script that performs the backup task (copy or archive files).
2
Make the script executable with <code>chmod +x /path/to/backup.sh</code>.
3
Open the cron table editor using <code>crontab -e</code>.
4
Add a cron job line specifying the schedule and the script path.
5
Save and exit the editor to activate the scheduled job.
💻

Code

bash
#!/bin/bash
# backup.sh - simple backup script

SOURCE_DIR="$HOME/documents"
BACKUP_DIR="$HOME/backup/documents"

mkdir -p "$BACKUP_DIR"
cp -r "$SOURCE_DIR"/* "$BACKUP_DIR"
echo "Backup completed at $(date)"
Output
Backup completed at Sat Apr 27 14:00:00 UTC 2024
🔍

Dry Run

Let's trace backing up /home/user/documents to /home/user/backup/documents

1

Create backup directory

mkdir -p /home/user/backup/documents creates the folder if missing

2

Copy files

cp -r /home/user/documents/* /home/user/backup/documents copies all files

3

Print confirmation

echo prints 'Backup completed at [current date]'

StepCommandEffect
1mkdir -p /home/user/backup/documentsCreates backup folder if not exists
2cp -r /home/user/documents/* /home/user/backup/documentsCopies all files from source to backup
3echo Backup completed at ...Prints completion message with timestamp
💡

Why This Works

Step 1: Backup script copies files

The script uses cp -r to copy all files recursively from the source to the backup folder.

Step 2: Scheduling with cron

Cron runs the script automatically at the scheduled time using the crontab entry.

Step 3: Automated backups

This setup ensures backups happen regularly without manual intervention.

🔄

Alternative Approaches

Using tar to archive backup
bash
#!/bin/bash
SOURCE_DIR="$HOME/documents"
BACKUP_FILE="$HOME/backup/documents_$(date +%Y%m%d).tar.gz"
mkdir -p "$HOME/backup"
tar -czf "$BACKUP_FILE" -C "$SOURCE_DIR" .
echo "Backup archive created at $BACKUP_FILE"
Archives files into a compressed tarball, saving space and keeping backups organized by date.
Using rsync for incremental backup
bash
#!/bin/bash
SOURCE_DIR="$HOME/documents/"
BACKUP_DIR="$HOME/backup/documents/"
mkdir -p "$BACKUP_DIR"
rsync -av --delete "$SOURCE_DIR" "$BACKUP_DIR"
echo "Incremental backup completed at $(date)"
Uses rsync to copy only changed files, making backups faster and efficient.

Complexity: O(n) time, O(n) space

Time Complexity

The backup time depends on the number of files and their sizes (n). Copying or archiving all files takes linear time.

Space Complexity

Backup requires additional space equal to the size of the files being copied or archived.

Which Approach is Fastest?

Using rsync is faster for repeated backups because it copies only changed files, reducing time and space usage.

ApproachTimeSpaceBest For
Simple cp scriptO(n)O(n)Small backups, simplicity
Tar archiveO(n)O(n)Compressed backups, archiving
Rsync incrementalO(k) where k ≤ nO(k)Frequent backups, efficiency
💡
Always test your backup script manually before scheduling it with cron to ensure it works correctly.
⚠️
Forgetting to make the backup script executable or using incorrect paths in the cron job causes the backup to fail silently.