Bash Script to Automate Backup with Simple Commands
tar -czf backup-$(date +%Y%m%d%H%M%S).tar.gz /path/to/source inside a script to automate backups by compressing files with a timestamped name.Examples
How to Think About It
Algorithm
Code
#!/bin/bash SOURCE="$1" BACKUP_DIR="./backups" mkdir -p "$BACKUP_DIR" TIMESTAMP=$(date +%Y%m%d%H%M%S) ARCHIVE="$BACKUP_DIR/backup-$TIMESTAMP.tar.gz" tar -czf "$ARCHIVE" "$SOURCE" echo "Created backup archive $ARCHIVE containing $SOURCE"
Dry Run
Let's trace backing up /home/user/documents through the code
Set SOURCE
SOURCE="/home/user/documents"
Create backup directory
mkdir -p ./backups (creates if not exists)
Generate timestamp
TIMESTAMP=20240601123145
Set archive path
ARCHIVE=./backups/backup-20240601123145.tar.gz
Create archive
tar -czf ./backups/backup-20240601123145.tar.gz /home/user/documents
Print confirmation
Created backup archive ./backups/backup-20240601123145.tar.gz containing /home/user/documents
| Step | Action | Value |
|---|---|---|
| 1 | SOURCE | /home/user/documents |
| 2 | Backup directory | ./backups |
| 3 | TIMESTAMP | 20240601123145 |
| 4 | ARCHIVE | ./backups/backup-20240601123145.tar.gz |
| 5 | tar command | tar -czf ./backups/backup-20240601123145.tar.gz /home/user/documents |
| 6 | Output | Created backup archive ./backups/backup-20240601123145.tar.gz containing /home/user/documents |
Why This Works
Step 1: Using <code>tar</code> with gzip
The tar -czf command creates a compressed archive file, combining files into one and compressing them to save space.
Step 2: Timestamp in filename
Adding $(date +%Y%m%d%H%M%S) ensures each backup file has a unique name based on the current date and time, preventing overwrites.
Step 3: Backup directory creation
The script creates a backups folder if it doesn't exist, keeping backup files organized in one place.
Alternative Approaches
#!/bin/bash SOURCE="$1" DEST="./backups/" mkdir -p "$DEST" rsync -av --delete "$SOURCE" "$DEST" echo "Synced $SOURCE to $DEST"
#!/bin/bash SOURCE="$1" BACKUP_DIR="./backups" mkdir -p "$BACKUP_DIR" TIMESTAMP=$(date +%Y%m%d%H%M%S) ARCHIVE="$BACKUP_DIR/backup-$TIMESTAMP.zip" zip -r "$ARCHIVE" "$SOURCE" echo "Created zip backup $ARCHIVE containing $SOURCE"
Complexity: O(n) time, O(n) space
Time Complexity
The script's time depends on the size and number of files in the source directory, as it processes each file once during compression.
Space Complexity
The archive file requires space proportional to the total size of the source files; no extra memory is used beyond temporary buffers.
Which Approach is Fastest?
Using rsync is faster for repeated backups because it copies only changed files, while tar compresses everything each time.
| Approach | Time | Space | Best For |
|---|---|---|---|
| tar gzip archive | O(n) | O(n) | Full compressed backups |
| rsync incremental | O(changed files) | O(changed files) | Fast incremental syncs |
| zip archive | O(n) | O(n) | Cross-platform compressed backups |
"$SOURCE" can cause errors with paths containing spaces.