Table of Contents
-
- Types of Backups
- Storage Options
- Frequency & Retention
-
- Assessing Critical Data
- Cleaning Up Unnecessary Files
- Checking Storage Availability
-
- rsync: The Swiss Army Knife
- tar: Archiving Essentials
- Timeshift: System Snapshots
- BorgBackup: Encrypted Deduplication
- restic: Secure & Fast Backups
-
- Basic rsync Backup
- Compressed Backups with tar
- System Snapshots with Timeshift
- Encrypted Remote Backups with BorgBackup
-
- Restoring Files with rsync
- Extracting from tar Archives
- Rolling Back with Timeshift
- Recovering from BorgBackup
-
- Scheduling Backups with cron
- Versioning & Retention Policies
- Encryption Best Practices
- Offsite & Cloud Backups
1. Understanding Backup Basics
Before diving into tools and commands, let’s clarify key concepts to build a solid foundation.
Types of Backups
Not all backups are created equal. Choose the right type based on your needs:
-
Full Backup: Copies all selected data.
- Pros: Simple to restore, no dependencies.
- Cons: Time-consuming, uses more storage.
-
Incremental Backup: Copies only data changed since the last backup (full or incremental).
- Pros: Fast, storage-efficient.
- Cons: Restores require the full backup + all incrementals (risk of chain breakage).
-
Differential Backup: Copies data changed since the last full backup.
- Pros: Faster than full backups, easier to restore than incremental (only full + latest differential).
Storage Options
Where you store backups matters for security and accessibility:
- Local Storage: External HDDs, SSDs, or USB drives (fast, low cost, but vulnerable to theft/fire).
- Network Storage: NAS (Network-Attached Storage) or NFS/SMB shares (centralized, accessible over LAN).
- Cloud Storage: AWS S3, Backblaze, or Nextcloud (offsite, scalable, but may have bandwidth costs).
Frequency & Retention
- Frequency: Back up critical data (e.g., work files) daily; system configs weekly; full system monthly.
- Retention: Keep multiple backups (e.g., daily for 7 days, weekly for 1 month, monthly for 6 months) to avoid data loss from corrupted backups.
2. Preparing for Backup
A little prep work ensures smoother backups and smaller, faster transfers.
Assess Critical Data
Identify what needs protection:
- User Files:
/home/<username>(documents, photos, downloads). - System Configs:
/etc,/var,/usr/local(critical for OS functionality). - Hidden Files:
.bashrc,.ssh,.config(often contain personal settings). - Databases: MySQL, PostgreSQL, or MongoDB data (requires special handling—see Common Pitfalls).
Clean Up Unnecessary Files
Reduce backup size by deleting:
- Temporary files:
/tmp,~/.cache. - Large logs:
/var/log(archive old logs first withlogrotate). - Duplicates: Use tools like
fdupesto find and remove duplicates.
Check Storage Availability
Ensure your backup destination has enough space:
# Check free space on the backup drive (replace /mnt/backup with your path)
df -h /mnt/backup
3. Choosing Backup Tools
Linux offers a wealth of backup tools. Here are the most reliable options:
rsync: The Swiss Army Knife
- Use Case: Local/remote file synchronization, incremental backups.
- Pros: Fast, efficient (only transfers changed data), supports SSH for remote backups.
- Cons: No built-in compression/encryption (add with
tarorgpg).
tar: Archiving Essentials
- Use Case: Creating compressed archives of files/directories.
- Pros: Preinstalled on all Linux systems, supports compression (gzip, bzip2).
- Cons: Not incremental by default (combine with
rsyncfor efficiency).
Timeshift: System Snapshots
- Use Case: Rolling back the OS to a previous state (like Windows System Restore).
- Pros: Easy to use, GUI/CLI options, preserves system files (not user data by default).
- Cons: Less flexible for user files; use alongside rsync/tar for complete protection.
BorgBackup (borg)
- Use Case: Encrypted, deduplicated backups to local/remote storage.
- Pros: Saves space via deduplication, built-in encryption, supports versioning.
- Cons: Steeper learning curve than rsync/tar.
restic
- Use Case: Secure, fast backups with encryption and deduplication.
- Pros: Similar to borg but simpler, cloud-friendly (S3, Azure, GCS support).
4. Step-by-Step Backup Process
Let’s walk through practical backup workflows using the tools above.
Basic rsync Backup
rsync is ideal for syncing files to an external drive or remote server.
Example: Backup /home to an external USB drive
-
Mount the USB drive (replace
/dev/sdb1with your drive’s path):sudo mount /dev/sdb1 /mnt/backup -
Run rsync to sync
/hometo the drive:rsync -av --delete /home/ /mnt/backup/home_backup/-a: Archive mode (preserves permissions, ownership, timestamps).-v: Verbose (shows progress).--delete: Removes files in the backup that no longer exist in/home(mirrors source).
Compressed Backups with tar
Combine tar and rsync for compressed, incremental backups.
Example: Create a compressed archive of /etc
-
Use
tarto create a gzip-compressed archive:tar -czf /mnt/backup/etc_backup_$(date +%Y%m%d).tar.gz /etc-c: Create archive.-z: Compress with gzip.-f: Specify output file (name includes date for versioning).
-
For incremental backups, use
--listed-incremental:tar -czf /mnt/backup/etc_incr_$(date +%Y%m%d).tar.gz --listed-incremental=/mnt/backup/etc.snar /etc--listed-incremental=/etc.snar: Stores metadata to track changes (reuse for future increments).
System Snapshots with Timeshift
Timeshift is perfect for protecting against OS updates gone wrong.
Step 1: Install Timeshift
- Ubuntu/Debian:
sudo apt install timeshift - Fedora:
sudo dnf install timeshift
Step 2: Configure Timeshift
- Launch the GUI:
sudo timeshift-launcher(or use CLI:timeshift --configure). - Choose a backup location (e.g., external drive).
- Select snapshot type: RSYNC (supports external drives) or BTRFS (for BTRFS filesystems).
- Set schedule (e.g., daily snapshots, keep 7 daily backups).
Step 3: Create a Snapshot
# CLI: Create an on-demand snapshot
sudo timeshift --create --comments "Before updating packages"
Encrypted Remote Backups with BorgBackup
Borg ensures secure, space-efficient backups to a remote server (e.g., VPS).
Step 1: Install Borg
# Ubuntu/Debian
sudo apt install borgbackup
# Fedora
sudo dnf install borgbackup
Step 2: Initialize a Borg Repository
A repository is where borg stores backups. Initialize one on a remote server via SSH:
borg init --encryption=repokey ssh://[email protected]:/path/to/borg_repo
--encryption=repokey: Encrypts the repo with a passphrase (store this securely!).
Step 3: Create a Backup
Backup /home to the remote repo:
borg create --progress ssh://[email protected]:/path/to/borg_repo::"backup-{now:%Y-%m-%d}" /home
::"backup-{now:%Y-%m-%d}": Adds a timestamped label to the backup.
5. Step-by-Step Recovery Process
Backups are useless if you can’t restore them. Here’s how to recover data with each tool.
Restoring with rsync
To restore files from an rsync backup:
Example: Restore /home from external drive
# Mount the backup drive
sudo mount /dev/sdb1 /mnt/backup
# Restore files (add --dry-run first to test!)
rsync -av --dry-run /mnt/backup/home_backup/ /home/
# Remove --dry-run to perform the restore
rsync -av /mnt/backup/home_backup/ /home/
Extracting from tar Archives
Recover files from a tar archive:
Example: Extract /etc backup
# List contents first (optional)
tar -tzf /mnt/backup/etc_backup_20240520.tar.gz
# Extract all files
tar -xzf /mnt/backup/etc_backup_20240520.tar.gz -C /
# Extract a single file (e.g., /etc/fstab)
tar -xzf /mnt/backup/etc_backup_20240520.tar.gz etc/fstab -C /tmp
Rolling Back with Timeshift
Restore a Timeshift snapshot if the OS fails to boot:
Step 1: Boot into Timeshift
- If the system boots: Launch Timeshift GUI → select snapshot → “Restore”.
- If not: Boot from a Linux live USB, install Timeshift, and restore from the live environment.
Step 2: Restore via CLI
# List available snapshots
sudo timeshift --list
# Restore a snapshot (replace <snapshot-name> with the ID from --list)
sudo timeshift --restore --snapshot <snapshot-name>
Recovering from BorgBackup
Restore files from a borg repo:
Step 1: List Backups in the Repo
borg list ssh://[email protected]:/path/to/borg_repo
Step 2: Restore a Specific Backup
# Restore /home from the 2024-05-20 backup to /tmp/restore
borg extract --progress ssh://[email protected]:/path/to/borg_repo::backup-2024-05-20 home/ --destination /tmp/restore
6. Advanced Backup Strategies
Take your backups to the next level with these pro tips.
Scheduling Backups with cron
Automate backups using cron (task scheduler).
Example: Daily rsync Backup at 2 AM
-
Open the crontab editor:
crontab -e -
Add this line (replace paths with your backup drive):
0 2 * * * rsync -av --delete /home/ /mnt/backup/home_backup/ >> /var/log/rsync_backup.log 2>&10 2 * * *: Runs daily at 2:00 AM.>> /var/log/rsync_backup.log 2>&1: Logs output/errors.
Versioning & Retention
Keep multiple backups but avoid clutter:
- Use timestamps in filenames (e.g.,
backup_20240520.tar.gz). - With borg/restic: Use
borg pruneto delete old backups:# Keep 7 daily, 4 weekly, 6 monthly backups borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 ssh://[email protected]:/path/to/borg_repo
Encryption Best Practices
-
tar + gpg: Encrypt tar archives with GPG:
tar -czf - /home | gpg -c > /mnt/backup/home_encrypted_$(date +%Y%m%d).tar.gz.gpg(Enter a passphrase when prompted.)
-
borg/restic: Always use encryption for remote backups—borg’s
repokeyor restic’s--password-file.
Offsite Backups
Store backups offsite to protect against theft/fire:
- Cloud Storage: Use
rcloneto sync backups to AWS S3, Google Drive, or Backblaze B2. - Remote Server: Use rsync over SSH or borg to a VPS (e.g., DigitalOcean, Linode).
Testing Backups
Always verify backups work!
- For rsync: Check file counts/sizes with
diff -r /home /mnt/backup/home_backup. - For tar: Test extraction with
tar -tzf backup.tar.gz | grep "critical_file". - For borg: Use
borg checkto validate repo integrity:borg check ssh://[email protected]:/path/to/borg_repo
7. Common Pitfalls to Avoid
- Forgetting Hidden Files:
rsync /homemisses.bashrc,.ssh, etc. Use/home/(with trailing slash) to include all subdirectories. - Ignoring Permissions: Backups may lose permissions if run without
sudo(use-ain rsync/tar). - Not Testing Restores: A backup is only good if you can restore from it—test monthly.
- Database Backups: Databases (MySQL, PostgreSQL) need live dumps (e.g.,
mysqldump) before file-level backups. - Running Out of Space: Monitor backup drive usage with
df -h; prune old backups regularly.
8. Conclusion
Backup and recovery are non-negotiable for Linux users. By combining tools like rsync, tar, Timeshift, and borg, you can create a robust backup strategy that protects both system files and user data. Remember: The best backup is one you test regularly.
Don’t wait for data loss to strike—set up your first backup today, schedule it, and rest easy knowing your system is protected.