Linux Backup and Restore - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux Backup and Restore Guide
Complete beginner-friendly guide to backing up and restoring your Linux system, covering Arch Linux, CachyOS, and other distributions including file backups, system backups, automated backups, and restore procedures.
Table of Contents
- Understanding Backups
- What to Backup
- File-Level Backups
- System Configuration Backups
- Full System Backups
- Automated Backups
- Restoring from Backups
- Backup Tools
- Best Practices
Understanding Backups
What is a Backup?
Backup is a copy of your data stored separately from the original.
Why backups matter:
- Data loss protection: Recover from accidental deletion
- System recovery: Restore system after failure
- Configuration recovery: Restore settings after changes
- Peace of mind: Know your data is safe
Types of Backups
File-level backups:
- What: Backs up individual files and folders
- Use for: Documents, photos, personal files
- Tools: rsync, tar, GUI tools
System configuration backups:
- What: Backs up system settings and configuration
- Use for: Restoring system configuration
- Tools: Configuration file copying, system settings export
Full system backups:
- What: Backs up entire system
- Use for: Complete system recovery
- Tools: Timeshift, Clonezilla, dd, Btrfs snapshots
Backup Storage Locations
Where to store backups:
- External drive: USB drive, external hard drive
- Network storage: NAS, network drive
- Cloud storage: Online backup services
- Separate partition: Different partition on same drive
Best practices:
- Multiple locations: Don't rely on one backup
- Off-site backup: Keep copy away from computer
- Regular backups: Backup frequently
- Test restores: Verify backups work
What to Backup
Essential Files to Backup
Personal files:
- Documents, photos, videos
- Downloads folder
- Desktop files
- Music, books, etc.
Configuration files:
~/.config/: Application configurations~/.bashrc,~/.zshrc: Shell configurations~/.ssh/: SSH keys and configs~/.local/: User-specific data
System configuration:
/etc/: System configuration files- Package lists:
pacman -Q > package-list.txt - Bootloader configuration
File-Level Backups
rsync
Install rsync:
# Arch/CachyOS
sudo pacman -S rsync
# Debian/Ubuntu
sudo apt install rsync
# Fedora
sudo dnf install rsync
Backup home directory:
# Backup home
rsync -av --exclude='.cache' ~/ /backup/home/
# Restore
rsync -av /backup/home/ ~/
Options:
-a: Archive mode (preserves permissions, timestamps)-v: Verbose output--exclude: Exclude directories
tar
Create archive:
# Backup home
tar -czf backup-home-$(date +%Y%m%d).tar.gz ~/
# Backup system (exclude system directories)
sudo tar -czf backup-system.tar.gz --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/tmp /
Restore:
# Extract
tar -xzf backup-home-20240115.tar.gz
# To specific location
tar -xzf backup-home-20240115.tar.gz -C /restore/path
System Configuration Backups
Package Lists
Backup installed packages:
# Arch/CachyOS
pacman -Q > package-list.txt
# Debian/Ubuntu
dpkg --get-selections > package-list.txt
# Fedora
dnf list installed > package-list.txt
Restore packages:
# Arch/CachyOS
pacman -S $(cat package-list.txt | cut -d' ' -f1)
# Debian/Ubuntu
dpkg --set-selections < package-list.txt
apt-get dselect-upgrade
Configuration Files
Backup config:
# Backup /etc
sudo tar -czf etc-backup.tar.gz /etc
# Backup user config
tar -czf config-backup.tar.gz ~/.config
Full System Backups
Timeshift
Install Timeshift:
# Arch/CachyOS
yay -S timeshift
# Debian/Ubuntu
sudo apt install timeshift
# Fedora
sudo dnf install timeshift
Create snapshot:
# Create snapshot
sudo timeshift --create
# List snapshots
sudo timeshift --list
Restore:
# Restore from snapshot
sudo timeshift --restore
Clonezilla
Use Clonezilla:
# Download ISO from clonezilla.org
# Boot from ISO
# Follow wizard to create disk image
dd (Disk Image)
Create disk image:
# Create image
sudo dd if=/dev/sda of=/backup/system.img bs=4M status=progress
# Restore image
sudo dd if=/backup/system.img of=/dev/sda bs=4M status=progress
Warning: dd can destroy data if used incorrectly. Use with caution.
Btrfs Snapshots
If using Btrfs:
# Create snapshot
sudo btrfs subvolume snapshot / /mnt/snapshots/snapshot-$(date +%Y%m%d)
# List snapshots
sudo btrfs subvolume list /
Automated Backups
Cron Job
Create backup script:
# Create script
vim ~/backup.sh
Script example:
#!/bin/bash
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d)
# Backup home
rsync -av --exclude='.cache' ~/ "$BACKUP_DIR/home-$DATE/"
# Backup package list
pacman -Q > "$BACKUP_DIR/package-list-$DATE.txt"
Make executable:
chmod +x ~/backup.sh
Add to crontab:
# Edit crontab
crontab -e
# Daily backup at 2 AM
0 2 * * * /home/user/backup.sh
systemd Timer
Create service:
# Create service
sudo vim /etc/systemd/system/backup.service
Service file:
[Unit]
Description=System Backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Create timer:
# Create timer
sudo vim /etc/systemd/system/backup.timer
Timer file:
[Unit]
Description=Daily Backup
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Enable timer:
sudo systemctl enable backup.timer
sudo systemctl start backup.timer
Restoring from Backups
Restore Files
From rsync:
rsync -av /backup/home/ ~/
From tar:
tar -xzf backup-home-20240115.tar.gz
Restore System
From Timeshift:
# Boot from live USB
# Launch Timeshift
# Select snapshot
# Restore
From Btrfs snapshot:
# Boot from live USB
# Mount root
# Restore snapshot
sudo btrfs subvolume snapshot /mnt/snapshots/snapshot-20240115 /mnt/restored
Backup Tools
GUI Tools
Timeshift:
# Launch GUI
sudo timeshift-gtk
Deja Dup (Debian/Ubuntu):
sudo apt install deja-dup
BackInTime:
# Arch/CachyOS
yay -S backintime
# Debian/Ubuntu
sudo apt install backintime-qt4
Best Practices
Backup Strategy
3-2-1 Rule:
- 3 copies: Original + 2 backups
- 2 different media: Different storage types
- 1 off-site: Keep one backup off-site
Regular Backups
Schedule:
- Daily: Important files
- Weekly: Full system
- Monthly: Archive old backups
Test Restores
Verify backups:
# Test restore to different location
# Verify files are correct
# Ensure system can boot
Summary
This guide covered backup and restore for Arch Linux, CachyOS, and other distributions, including file backups, system backups, automated backups, and restore procedures.
Next Steps
- System Recovery - System recovery
- Btrfs Guide - Btrfs snapshots
- Snapshots - System snapshots
- ArchWiki Backup: https://wiki.archlinux.org/title/Rsync
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.