Table of Contents
- Introduction
- Understanding Your Backup Needs
- 2.1 What to Backup
- 2.2 Backup Frequency
- 2.3 Storage Location
- 2.4 Retention Policy
- Types of Linux Backups
- 3.1 Full Backups
- 3.2 Incremental Backups
- 3.3 Differential Backups
- 3.4 Choosing the Right Type
- Popular Linux Backup Tools Overview
- Step-by-Step Backup Setup with Key Tools
- Automating Linux Backups
- Verifying Your Backups
- Best Practices for Linux Backups
- References
2. Understanding Your Backup Needs
Before diving into tools, define your backup goals. Ask:
2.1 What to Backup
Not all data is equal. Prioritize:
- User Data:
/home(documents, photos, downloads),/var/www(websites),/etc(system configurations). - System Files: For full system recovery (e.g.,
/boot,/root,/usr). - Avoid: Temporary files (
/tmp), cache (~/.cache), or large redundant folders (e.g.,node_modules).
2.2 Backup Frequency
- Critical Data (e.g., work documents): Daily or hourly.
- Non-Critical Data (e.g., old photos): Weekly or monthly.
- System Snapshots: Weekly (since system files change less often).
2.3 Storage Location
Choose where to store backups:
- Local: External HDD/SSD (fast, but vulnerable to theft/fire).
- Remote: Another computer (via SSH), cloud storage (AWS S3, Backblaze), or a friend’s server (offsite protection).
- Hybrid: Combine local (for quick restores) and remote (for disaster recovery).
2.4 Retention Policy
Decide how long to keep backups:
- Daily snapshots: Keep 7–14 days.
- Weekly snapshots: Keep 4–8 weeks.
- Monthly snapshots: Keep 6–12 months.
- Annual snapshots: Keep 1–3 years (for historical data).
3. Types of Linux Backups
Backups come in three primary flavors. Choose based on speed, storage, and recovery needs.
3.1 Full Backups
What: Copies all selected data every time.
Pros: Simple to restore (one file set).
Cons: Slow, uses maximum storage.
Use Case: Monthly base backups or small datasets.
3.2 Incremental Backups
What: Copies only data changed since the last backup (full or incremental).
Pros: Fast, storage-efficient.
Cons: Restores require the full backup + all incrementals (complex).
Use Case: Daily backups (e.g., after a weekly full backup).
3.3 Differential Backups
What: Copies data changed since the last full backup.
Pros: Faster than full backups, simpler restores than incremental (full + latest differential).
Cons: Larger than incrementals over time.
Use Case: Balancing speed and restore simplicity (e.g., daily differentials after a weekly full).
3.4 Choosing the Right Type
- Home Users: Full (monthly) + incremental (daily) = balance of speed and storage.
- Power Users: Full (weekly) + differential (daily) = easier restores.
- Servers: Incremental (hourly) + full (nightly) = minimal downtime.
4. Popular Linux Backup Tools Overview
Linux offers robust tools for every use case. Here are the most reliable:
4.1 rsync: The Swiss Army Knife
- Type: CLI, file-level, incremental.
- Pros: Preinstalled on most Linux distros, supports remote backups (SSH), highly customizable.
- Cons: No built-in encryption (use with SSH or tools like
rclone), no deduplication. - Best For: Simple file backups, syncing data to external drives/remote servers.
4.2 Timeshift: System Snapshots Made Easy
- Type: GUI/CLI, system-level, incremental (RSYNC) or snapshot-based (BTRFS).
- Pros: User-friendly, restores entire systems to a previous state, supports BTRFS/LVM.
- Cons: Focused on system files (not user data), limited cloud support.
- Best For: Recovering from OS misconfigurations or failed updates.
4.3 BorgBackup: Deduplication & Encryption
- Type: CLI, deduplication, incremental, encrypted.
- Pros: Saves space via deduplication, encrypts backups, supports compression, prunes old backups.
- Cons: Steeper learning curve, no GUI (use
Vortafor GUI). - Best For: Secure, space-efficient backups to local/cloud storage.
4.4 Honorable Mentions
- Duplicity: Integrates with cloud storage (S3, GDrive), encrypts backups, uses
rsyncunder the hood. - Amanda/Bacula: Enterprise-grade, centralized backup for networks (overkill for home users).
- rclone: Syncs to cloud storage (S3, Dropbox), supports encryption via
cryptbackend.
5. Step-by-Step Backup Setup with Key Tools
5.1 Using rsync for File-Level Backups
Prerequisites: rsync is preinstalled on most Linux systems. Verify with rsync --version.
Basic rsync Command
Sync /home/user to an external drive mounted at /mnt/backup:
rsync -av --progress /home/user /mnt/backup/
-a: Archive mode (preserves permissions, timestamps).-v: Verbose (shows progress).--progress: Displays transfer progress.
Advanced rsync: Exclude Files, Dry Runs
Exclude cache folders and test before running:
# Dry run (test what will be synced)
rsync -av --exclude={".cache", "node_modules"} --dry-run /home/user /mnt/backup/
# Actual backup (remove --dry-run to execute)
rsync -av --exclude={".cache", "node_modules"} /home/user /mnt/backup/
Remote Backup via SSH
Sync to a remote server (replace user@server and /remote/path):
rsync -av -e ssh /home/user user@server:/remote/backup/path/
5.2 Timeshift for System Snapshots
Prerequisites: Ubuntu/Debian: sudo apt install timeshift; Fedora: sudo dnf install timeshift.
GUI Setup (Recommended for Beginners)
- Launch Timeshift:
sudo timeshift-launcher. - Select Snapshot Type:
RSYNC: Works on any filesystem (slower, uses more space).BTRFS: For BTRFS filesystems (fast snapshots, requires BTRFS subvolumes).
- Select Storage: Choose an external drive or partition (not the system drive!).
- Schedule: Set daily/weekly snapshots (e.g., 2 daily, 4 weekly, 2 monthly).
- Save: Click “Create” to take an immediate snapshot.
CLI Setup
Create a manual RSYNC snapshot to /mnt/backup/timeshift:
sudo timeshift --create --snapshot-device /dev/sdb1 --snapshot-type RSYNC --comments "Before update"
List snapshots:
sudo timeshift --list
Restore a snapshot (boot from a live USB for system restores):
sudo timeshift --restore --snapshot '2024-05-20_12-00-00'
5.3 BorgBackup for Encrypted, Space-Efficient Backups
Prerequisites: Install Borg: sudo apt install borgbackup (Ubuntu/Debian) or sudo dnf install borgbackup (Fedora).
Step 1: Initialize a Backup Repository
Create an encrypted repo on an external drive (/mnt/backup/borg-repo). Use a strong passphrase!
borg init --encryption=repokey /mnt/backup/borg-repo
repokey: Encryption key stored in the repo (usekeyfileto store it separately).
Step 2: Create a Backup
Backup /home/user to the repo (name the backup 2024-05-20-home):
borg create --progress /mnt/backup/borg-repo::2024-05-20-home /home/user
- Add
--exclude .cacheto skip folders,--compression zstdfor compression.
Step 3: Restore Files
Restore a single file (e.g., document.txt) to /tmp/restore:
borg extract /mnt/backup/borg-repo::2024-05-20-home home/user/documents/document.txt --destination /tmp/restore
Step 4: Prune Old Backups
Keep only 7 daily, 4 weekly, and 6 monthly backups:
borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 /mnt/backup/borg-repo
6. Automating Linux Backups
Manual backups are error-prone—automate with cron or systemd timers.
6.1 Cron Jobs: Scheduling Backups
Cron runs scripts at specified intervals. Edit the crontab with crontab -e (use nano as the editor if prompted).
Example: Daily rsync Backup at 2 AM
Add this line to run a backup script (/home/user/backup-script.sh) daily at 2:00 AM:
0 2 * * * /home/user/backup-script.sh >> /var/log/backup.log 2>&1
0 2 * * *: Minute (0), hour (2), day (), month (), weekday (*) → daily at 2 AM.>> /var/log/backup.log 2>&1: Logs output/errors to a file.
6.2 Systemd Timers (Advanced Alternative)
For more control (e.g., dependencies, retry logic), use systemd timers. Create a .service file and a .timer file in /etc/systemd/system/.
6.3 Sample Backup Script
Create /home/user/backup-script.sh (replace /mnt/backup with your drive path):
#!/bin/bash
BACKUP_SOURCE="/home/user"
BACKUP_DEST="/mnt/backup"
LOG_FILE="/var/log/backup.log"
# Check if backup drive is mounted
if ! mountpoint -q "$BACKUP_DEST"; then
echo "[$(date)] Backup drive not mounted. Exiting." >> "$LOG_FILE"
exit 1
fi
# Run rsync backup
echo "[$(date)] Starting backup..." >> "$LOG_FILE"
rsync -av --exclude=".cache" "$BACKUP_SOURCE" "$BACKUP_DEST" >> "$LOG_FILE" 2>&1
echo "[$(date)] Backup completed. Check $LOG_FILE for details." >> "$LOG_FILE"
Make it executable:
chmod +x /home/user/backup-script.sh
7. Verifying Your Backups
A backup is useless if it can’t be restored. Always verify!
7.1 Check Backup Integrity
- rsync: Use
--checksumto compare source and destination (ignores timestamps):rsync -av --checksum --dry-run /home/user /mnt/backup/ - BorgBackup: Validate the repo:
borg check /mnt/backup/borg-repo
7.2 Test Restores Regularly
- rsync: Restore a file to a temporary directory:
rsync -av /mnt/backup/home/user/documents/file.txt /tmp/restore-test/ - Timeshift: Restore a snapshot to a test directory (avoid overwriting your system!):
sudo timeshift --restore --snapshot '2024-05-20_12-00-00' --target /tmp/timeshift-test
8. Best Practices for Linux Backups
- Encrypt Everything: Use Borg, SSH, or
rclone cryptfor cloud backups. - Offsite Storage: Store backups in a different physical location (e.g., cloud, friend’s server) to protect against fire/theft.
- Test Restores Monthly: Practice restoring files to ensure backups work.
- Separate Backups: Keep system (Timeshift) and user data (rsync/Borg) backups separate.
- Update Your Plan: Review backup goals quarterly (e.g., new data folders, changed storage needs).
9. References
- rsync Official Documentation
- Timeshift GitHub
- BorgBackup Documentation
- Linux Backup Guide (DigitalOcean)
- Cron Job Basics (Linuxize)
By following this guide, you’ll have a resilient backup system to protect your Linux data. Remember: the best backup is the one you test regularly! Let us know in the comments if you have questions or favorite backup tools.