Backup & Recovery
Data backup is one of the most important aspects of system administration. This article introduces various backup and recovery solutions in Ubuntu 26.04 to help you choose the right tool for your needs.
rsync - Flexible Synchronization Tool
rsync is a fast, flexible file synchronization tool that supports incremental transfers and is one of the most commonly used backup tools on Linux.
Basic Usage
# Local directory sync
rsync -av /source/directory/ /backup/directory/
# Note: the trailing slash on the source path matters
# /source/dir/ -> syncs the contents of the directory
# /source/dir -> syncs the directory itself (including the directory name)Common Options
# -a archive mode (preserves permissions, timestamps, symlinks, etc.)
# -v verbose output
# -z compress during transfer
# -h human-readable output
# --progress show progress
# --delete delete files in the target that don't exist in the source
# Complete local backup command
rsync -avh --progress /home/user/ /backup/home-user/
# Backup with exclusion rules
rsync -avh --progress \
--exclude='.cache' \
--exclude='node_modules' \
--exclude='.local/share/Trash' \
/home/user/ /backup/home-user/
# Using an exclude file
rsync -avh --exclude-from='/home/user/rsync-excludes.txt' \
/home/user/ /backup/home-user/Remote Backup
# Backup to a remote server via SSH
rsync -avz -e ssh /home/user/ user@remote:/backup/home-user/
# Specify SSH port
rsync -avz -e "ssh -p 2222" /home/user/ user@remote:/backup/home-user/
# Restore from a remote server
rsync -avz user@remote:/backup/home-user/ /home/user/
# Use --delete to keep both sides in sync
rsync -avz --delete /home/user/ user@remote:/backup/home-user/Automated rsync Backup Script
Create a backup script:
sudo nano /usr/local/bin/daily-backup.sh#!/bin/bash
# Configuration
SOURCE="/home/"
DEST="/backup/daily"
LOG="/var/log/backup.log"
DATE=$(date +%Y-%m-%d_%H%M%S)
# Create log entry
echo "===== Backup started: $DATE =====" >> "$LOG"
# Execute backup
rsync -avh --delete \
--exclude='.cache' \
--exclude='node_modules' \
--exclude='.local/share/Trash' \
"$SOURCE" "$DEST" >> "$LOG" 2>&1
# Record result
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $(date)" >> "$LOG"
else
echo "Backup failed: $(date)" >> "$LOG"
fisudo chmod +x /usr/local/bin/daily-backup.shSchedule it with cron:
# Run backup every day at 2 AM
sudo crontab -e
# Add: 0 2 * * * /usr/local/bin/daily-backup.shTimeshift - System Snapshots
Timeshift focuses on system file snapshots and recovery, similar to Windows System Restore.
Installation and Configuration
# Install Timeshift
sudo apt install timeshift
# Launch the graphical interface
sudo timeshift-gtk
# Create a snapshot via command line
sudo timeshift --create --comments "Before installing NVIDIA driver"
# List all snapshots
sudo timeshift --list
# Restore to a specific snapshot
sudo timeshift --restore --snapshot '2026-03-24_10-00-00'
# Delete a snapshot
sudo timeshift --delete --snapshot '2026-03-24_10-00-00'Command-Line Configuration
# View current configuration
sudo timeshift --config
# Set snapshot type (RSYNC or BTRFS)
sudo timeshift --snapshot-device /dev/sda2Timeshift Configuration File
Edit /etc/timeshift/timeshift.json:
{
"backup_device_uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"parent_device_uuid": "",
"do_first_run": false,
"btrfs_mode": false,
"include_btrfs_home": false,
"schedule_monthly": false,
"schedule_weekly": true,
"schedule_daily": true,
"schedule_hourly": false,
"schedule_boot": false,
"count_monthly": 2,
"count_weekly": 3,
"count_daily": 5,
"count_hourly": 0,
"count_boot": 0,
"exclude": [
"/home/**",
"/root/**"
]
}Btrfs Snapshot Mode
If the system uses the Btrfs filesystem, Timeshift can leverage native Btrfs snapshots, which are faster and use less space:
# Verify filesystem type
df -T /
# If it's Btrfs, select BTRFS mode in Timeshift
sudo timeshift --btrfsDeja Dup - Desktop Backup Tool
Deja Dup (GNOME Backups) is an easy-to-use backup tool for the Ubuntu desktop environment, suitable for personal users.
Installation and Usage
# Install (usually pre-installed on Ubuntu Desktop)
sudo apt install deja-dupDeja Dup provides a graphical interface, accessible through “Settings” or by searching for “Backups” in the app menu.
Key features:
- Supports backup to local disks, external drives, and network storage
- Supports backup to Google Drive and other cloud storage
- Automatic encryption of backups
- Incremental backup support
- Scheduled automatic backups
Command-Line Operations
Deja Dup uses duplicity as its backend:
# Manually run a backup
deja-dup --backup
# Restore files
deja-dup --restoreDisk Image Backup
Creating Disk Images with dd
# Create a full disk image
sudo dd if=/dev/sda of=/backup/sda.img bs=4M status=progress
# Compressed image
sudo dd if=/dev/sda bs=4M status=progress | gzip > /backup/sda.img.gz
# Restore from an image
sudo dd if=/backup/sda.img of=/dev/sda bs=4M status=progress
# Restore from a compressed image
gunzip -c /backup/sda.img.gz | sudo dd of=/dev/sda bs=4M status=progressUsing Clonezilla
Clonezilla is a powerful disk cloning and imaging tool:
# Install Clonezilla
sudo apt install clonezilla
# It is generally recommended to boot from a Clonezilla Live USB
# Download: https://clonezilla.org/downloads.phpSystem Backup with tar
# Back up the entire system (excluding unnecessary directories)
sudo tar czpf /backup/system-$(date +%Y%m%d).tar.gz \
--exclude=/backup \
--exclude=/proc \
--exclude=/sys \
--exclude=/dev \
--exclude=/run \
--exclude=/tmp \
--exclude=/mnt \
--exclude=/media \
--exclude=/lost+found \
/
# Restore the system
sudo tar xzpf /backup/system-20260324.tar.gz -C /Database Backup
MySQL / MariaDB
# Back up a single database
mysqldump -u root -p database_name > /backup/db-$(date +%Y%m%d).sql
# Back up all databases
mysqldump -u root -p --all-databases > /backup/all-db-$(date +%Y%m%d).sql
# Restore a database
mysql -u root -p database_name < /backup/db-20260324.sqlPostgreSQL
# Back up a single database
sudo -u postgres pg_dump dbname > /backup/pg-$(date +%Y%m%d).sql
# Back up all databases
sudo -u postgres pg_dumpall > /backup/pg-all-$(date +%Y%m%d).sql
# Restore
sudo -u postgres psql dbname < /backup/pg-20260324.sqlBackup Strategy Recommendations
The 3-2-1 Backup Rule
- 3 copies of your data (the original + 2 backups)
- 2 different storage media (e.g., local hard drive + cloud storage)
- 1 offsite backup (protection against physical disasters)
Recommendations by Scenario
| Scenario | Recommended Tools | Backup Frequency |
|---|---|---|
| Desktop personal user | Deja Dup + Timeshift | Daily / Weekly |
| Development workstation | rsync + Timeshift | Daily |
| Web server | rsync + database backup | Daily / Hourly |
| Critical business server | rsync + LVM snapshots + offsite backup | Real-time / Hourly |
Verifying Backups
Regularly verifying backup integrity is essential:
# Verify a tar backup
tar tzf /backup/system-20260324.tar.gz > /dev/null
# Verify an rsync backup
rsync -avhn --delete /source/ /backup/ # dry-run to compare differences
# Perform periodic restore drills