Backing Up and Recovering Data
1. Creating Backups with tar
| Option | Values | Description |
|---|---|---|
| tar -cf | archive.tar file1 file2 |
Create a new tar archive |
| tar -xf | archive.tar -C /backup |
Extract files from an archive |
| tar -czf | archive.tar.gz /path/to/files |
Create a compressed tar archive using gzip |
| tar -cjf | archive.tar.bz2 /path/to/files |
Create a compressed tar archive using bzip2 |
| tar -tvf | archive.tar |
List contents of an archive |
Note: The
-z flag compresses with gzip, while -j uses bzip2. Choose based on compression needs and speed trade-offs.
2. Creating Incremental Backups
Incremental backups only save files that have changed since the last backup, making them faster and more space-efficient than full backups.
| Command | Description | Notes |
|---|---|---|
| rsync -av --link-dest=/backup/last /source/ /backup/current/ | Creates an incremental backup using rsync with hard links to unchanged files. | Useful for backing up large directories efficiently. |
| tar --listed-incremental=/backup/incremental.snar -czf /backup/backup-$(date +%Y-%m-%d).tar.gz /source | Creates a compressed incremental backup using tar. | The .snar file stores metadata about previous backups. |
Example: Using rsync for incremental backups
# Create a daily backup directory
mkdir -p /backup/daily-$(date +%Y-%m-%d)
# Perform incremental backup using rsync
rsync -av --link-dest=/backup/last /home/user/data/ /backup/daily-$(date +%Y-%m-%d)/
# Update the symlink to point to the latest backup
ln -sf /backup/daily-$(date +%Y-%m-%d) /backup/last
Note: The
--link-dest option in rsync allows reusing unchanged files from a previous backup, reducing storage usage and time.
Warning: Ensure that the
.snar file is preserved when using tar incremental backups, as it's required to continue the backup chain.
3. Backing Up with rsync
| Option | Values | Description |
|---|---|---|
| -a | --archive |
Archive mode: preserves permissions, timestamps, symbolic links, etc. |
| -v | --verbose |
Verbose output to show progress and files being transferred. |
| -z | --compress |
Compress data during transfer to reduce bandwidth usage. |
| -r | --recursive |
Recursively copy directories. |
| --delete | Delete files in destination that are not present in source. | |
| --dry-run | Simulate the backup without actually copying files. |
Note: rsync uses a delta-sync algorithm to transfer only differences between files, making it efficient for incremental backups.
4. Creating Disk Images
| Command | Description | Usage Example |
|---|---|---|
| dd | Copy and convert files, often used for disk imaging | dd if=/dev/sda of=backup.img bs=4M status=progress |
| ddrescue | Rescue bad sectors from disks, useful for creating disk images from failing drives | ddrescue /dev/sda backup.img rescue.log |
Note: Ensure you have sufficient disk space for the image and consider using
ddrescue for drives with bad sectors to avoid data loss.
5. Cloning Partitions with dd
| Option | Values | Description |
|---|---|---|
| dd | dd if=source of=target |
Basic command to clone a partition or disk |
| if | /dev/sda1 |
Input file or device (source partition) |
| of | /dev/sdb1 |
Output file or device (target partition) |
| bs | 4M |
Block size for faster copying (default is 512 bytes) |
| conv | noerror,sync |
Handle read errors gracefully and pad blocks |
Note: Ensure the target device has sufficient space and is unmounted before running the command to avoid data corruption.
Warning: Using
dd can overwrite data permanently. Always double-check the input and output devices.
6. Using rsnapshot for Snapshots
| Option | Values | Description |
|---|---|---|
| rsnapshot.conf | /etc/rsnapshot.conf |
Configuration file location |
| snapshot_root | /backup/rsnapshot |
Base directory for snapshots |
| cmd_cp | /bin/cp |
Copy command used for snapshots |
| cmd_rm | /bin/rm |
Remove command used for cleanup |
| cmd_rsync | /usr/bin/rsync |
Rsync command used for file transfers |
Example: Basic rsnapshot Configuration
# rsnapshot.conf
config_version = 1.2
snapshot_root = /backup/rsnapshot
cmd_cp = /bin/cp
cmd_rm = /bin/rm
cmd_rsync = /usr/bin/rsync
# Backup sources
backup '/home/' localhost/
backup '/etc/' localhost/
backup '/var/www/' localhost/
# Backup intervals
interval hourly 1
interval daily 7
interval weekly 4
interval monthly 12
Example: Running rsnapshot Backup
# Perform a backup
rsnapshot hourly
# Check current snapshot structure
ls -l /backup/rsnapshot/
# Restore a file from the backup
rsnapshot restore localhost/home/user/file.txt 2024-05-10_12:00:00
Note: rsnapshot uses hard links to save space, so snapshots are efficient in terms of disk usage.
Warning: Ensure that the
snapshot_root directory has sufficient space for all backups and that permissions are correctly set to avoid data loss.
7. Backing Up with Duplicity
Duplicity is a backup tool that uses encryption and compression to securely back up data to various storage systems including local filesystems, cloud storage providers, and remote servers.
| Option | Values | Description |
|---|---|---|
| --encrypt-key | <GPG key ID> |
Specify the GPG key to use for encryption |
| --archive-dir | <path> |
Directory to store backup archives |
| --verbosity | info, debug |
Set verbosity level for logging output |
Example: Basic Backup Command
<pre><code>duplicity backup /home/user/data  
8. Restoring from Backups
| Command | Description | Usage Example |
|---|---|---|
| restore | Restores files or directories from a backup archive | restore -f /backup/file.tar.gz |
| tar -xzf | Extracts files from a tar.gz archive | tar -xzf backup.tar.gz -C /restore/path |
| cp | Copies files from backup location to system | cp /backup/data.txt /home/user/data.txt |
Example: Restore a Directory from Tar Archive
tar -xzf /home/user/backup.tar.gz -C /home/user/recovery/
Note: Ensure the target directory exists before restoring to avoid errors.
Warning: Restoring files may overwrite existing data. Always verify the backup before restoring.
| Option | Description | Notes |
|---|---|---|
| -C | Specify the directory to extract files into | Useful for restoring to a different location |
| -v | Verbose output during extraction | Shows progress and files being restored |
| -f | Specify the archive file to use | Required when using restore command |
9. Scheduling Automated Backups
| Command | Description | Example |
|---|---|---|
| crontab -l | List current cron jobs | crontab -l |
| crontab -e | Edit cron jobs | crontab -e |
| at | Schedule one-time jobs | echo "backup.sh" | at now + 1 hour |
Example: Weekly Backup with Log
0 2 * * 0 /usr/bin/rsync -avz /home/user/backup/ /mnt/backup/ &> /var/log/backup.log
Note: Cron uses a 5-field format: minute, hour, day of month, month, day of week.
10. Testing Backup Integrity
| Command | Description | Notes |
|---|---|---|
| tar --test-label | Verify tape archive label integrity | Use with magnetic tape devices to check if the tape is labeled correctly |
| md5sum /path/to/backup/file | Calculate MD5 checksum for backup file | Compare with known good checksum to verify integrity |
| sha256sum /path/to/backup/file | Calculate SHA-256 checksum for backup file | More secure than MD5; preferred for verifying large backups |
| rsync --dry-run | Simulate rsync operation without transferring files | Useful to check if backup would be consistent before actual run |
Example: Verify integrity of a tar backup using checksums
# Calculate checksum of backup file
md5sum /backup/linux-system-2026-04-05.tar > /backup/linux-system-2026-04-05.md5
# Later, verify the backup
md5sum -c /backup/linux-system-2026-04-05.md5
Note: For large backups, SHA-256 is more reliable than MD5 due to reduced collision risk.