Backup and Recovery Strategies
Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:16:52
For: System Administration, Development & Technical Interviews
Backup and Recovery Strategies: Linux Commands Cheatsheet (Advanced Linux Administration)
Section titled “Backup and Recovery Strategies: Linux Commands Cheatsheet (Advanced Linux Administration)”This cheatsheet provides a comprehensive guide to Linux commands used for backup and recovery, tailored for system administrators and developers.
1. Command Overview
Section titled “1. Command Overview”| Command | Description | Use Case |
|---|---|---|
tar | Archive utility for creating and extracting compressed archives (tarballs). | Creating full, incremental, and differential backups; archiving files for storage or transfer. |
gzip, bzip2, xz | Compression utilities for reducing file size. | Compressing archives created by tar to save storage space and speed up transfer. |
dd | Low-level disk imaging and data copying tool. | Creating full disk backups, cloning disks, and performing forensic analysis. |
rsync | Remote and local file synchronization tool. | Creating incremental backups, synchronizing files between servers, and mirroring data. |
cpio | Another archive utility (less common than tar, but still useful) | Similar to tar, but can handle devices directly. |
dump, restore | Filesystem-level backup and recovery utilities. | Creating filesystem-level backups and restoring individual files or entire filesystems. (Typically used with ext2/3/4 filesystems). |
lvm snapshots | Logical Volume Manager snapshot functionality | Creating consistent point-in-time copies of logical volumes for backup purposes. |
scp, sftp | Secure file transfer utilities. | Transferring backup archives to remote storage locations. |
zstd | Modern, high-performance compression algorithm. | Compressing backups with better compression ratios and speed compared to gzip or bzip2. |
2. Basic Syntax
Section titled “2. Basic Syntax”tar
# Create an archivetar -cvzf archive.tar.gz /path/to/directory
# Extract an archivetar -xvzf archive.tar.gz -C /path/to/extraction/directory
# List contents of an archivetar -tvf archive.tar.gzgzip
# Compress a filegzip filename
# Decompress a filegzip -d filename.gzgunzip filename.gz #Alternative
# Compress with best compression (slowest)gzip -9 filename
# Compress with fastest compression (least compression)gzip -1 filenamebzip2
# Compress a filebzip2 filename
# Decompress a filebzip2 -d filename.bz2bunzip2 filename.bz2 #Alternativexz
# Compress a filexz filename
# Decompress a filexz -d filename.xzunxz filename.xz #Alternativedd
# Create a disk imagedd if=/dev/sda of=disk.img bs=4M status=progress
# Restore a disk imagedd if=disk.img of=/dev/sda bs=4M status=progressrsync
# Synchronize a directory to a remote serverrsync -avz /path/to/local/directory user@remote_server:/path/to/remote/directory
# Synchronize a directory locallyrsync -av /path/to/source/directory /path/to/destination/directorylvm snapshot
# Create a snapshotlvcreate -L 1G -s -n my_snapshot /dev/vg0/my_volume
# Activate a snapshot (if necessary after a system restart)lvchange -ay /dev/vg0/my_snapshot
# Restore a volume from a snapshot (WARNING: Data loss!)lvconvert --merge /dev/vg0/my_snapshot
# Remove a snapshotlvremove /dev/vg0/my_snapshot3. Practical Examples
Section titled “3. Practical Examples”Example 1: Full Backup with tar and gzip
# Create a full backup of the /home directorytar -cvzf /backup/home_backup.tar.gz /home
# Verify the backuptar -tvf /backup/home_backup.tar.gz | head -n 10
#Expected Output (Example):#drwxr-xr-x user/user 0 2023-10-27 10:00 home/#drwxr-xr-x user/user 0 2023-10-27 10:00 home/user1/#-rw-r--r-- user/user 1024 2023-10-27 10:00 home/user1/file1.txt#-rw-r--r-- user/user 2048 2023-10-27 10:00 home/user1/file2.txt#...Example 2: Incremental Backup with rsync
# First full backuprsync -avz /data /backup/data_backup
# Subsequent incremental backups (only changes are copied)rsync -avz --delete /data /backup/data_backup
#Explanation:# -a: archive mode; preserves permissions, ownership, etc.# -v: verbose output# -z: compress data during transfer# --delete: delete files in destination that don't exist in source (important for maintaining an exact mirror).Example 3: Disk Cloning with dd
# Clone /dev/sda to /dev/sdb (WARNING: Overwrites the target disk!)dd if=/dev/sda of=/dev/sdb bs=4M status=progress conv=sync,noerror
#Explanation:# if: input file (source disk)# of: output file (destination disk)# bs: block size (4M is a good starting point, adjust for performance)# status=progress: shows the progress of the operation# conv=sync,noerror: handles read errors by padding with zeros.Example 4: Restoring Files from a tar Archive
# Restore a specific file from the archivetar -xvzf /backup/home_backup.tar.gz -C /home/user1 file1.txt
#Explanation:# -x: extract# -v: verbose# -z: decompress gzip# -f: specify archive file# -C: change to directory before extractingExample 5: Creating an LVM Snapshot
# Create a snapshot of the root volumelvcreate -L 5G -s -n root_snapshot /dev/vg0/root
# Mount the snapshot to access its datamkdir /mnt/root_snapshotmount /dev/vg0/root_snapshot /mnt/root_snapshot
#Restore the root volume from the snapshot (WARNING: Data loss!)umount /mnt/root_snapshotlvconvert --merge /dev/vg0/root_snapshot
#Remove the snapshotlvremove /dev/vg0/root_snapshotExample 6: Using zstd for compression
# Create a tar archive and compress with zstdtar -cf - /path/to/directory | zstd -T0 -19 -o backup.tar.zst
# Extract a zstd compressed tar archivezstd -d -c backup.tar.zst | tar -xf - -C /path/to/restore
#Explanation:# -T0: Use all available cores.# -19: Maximum compression level (slowest, best compression).# -o: Output file name.# -d: Decompress.# -c: Write to standard output (allows piping).
#You can also use 'pigz' for parallel gzip compression, 'pbzip2' for parallel bzip2, and 'pxz' for parallel xz, for faster compression on multi-core systems.4. Common Options
Section titled “4. Common Options”tar
-c: Create an archive.-x: Extract an archive.-v: Verbose output (list files processed).-z: Compress with gzip.-j: Compress with bzip2.-J: Compress with xz.-f: Specify the archive filename.-t: List contents of an archive.-C: Change to a directory before extracting.--exclude: Exclude files or directories from the archive.--listed-incremental=FILE: Create or update an incremental backup using a snapshot file.--one-file-system: Stay in one filesystem when creating archive.
gzip, bzip2, xz, zstd
-d: Decompress.-k: Keep the original file.-v: Verbose output.-1to-9: Compression levels (gzip, bzip2, xz).-1is fastest,-9is best compression. zstd uses-1to-19.-T0: Use all available cores (zstd).
dd
if: Input file.of: Output file.bs: Block size.status=progress: Show progress.conv=sync,noerror: Handle read errors.
rsync
-a: Archive mode (preserves permissions, ownership, etc.).-v: Verbose output.-z: Compress data during transfer.-r: Recursive.-l: Copy symlinks as symlinks.-p: Preserve permissions.-t: Preserve modification times.-o: Preserve owner.-g: Preserve group.-D: Preserve device files and special files.--delete: Delete files in the destination that don’t exist in the source.--exclude: Exclude files or directories.--include: Include files or directories.--progress: Show progress during transfer.-e: Specify the remote shell to use (e.g.,ssh).--bwlimit=KBPS: Limit bandwidth usage.
lvm snapshot
-L: Size of the snapshot.-s: Create a snapshot.-n: Name of the snapshot.lvconvert --merge: Merge the snapshot back into the original volume.lvremove: Remove a logical volume or snapshot.lvchange -ay: Activate a logical volume.
5. Advanced Usage
Section titled “5. Advanced Usage”Example 1: Incremental Backups with tar and Snapshot Files
# First full backuptar -cvzf /backup/full_backup.tar.gz --create /data
# Create a snapshot filetar -g /backup/snapshot_file -cvzf /backup/incremental_backup1.tar.gz /data
# Subsequent incremental backupstar -g /backup/snapshot_file -cvzf /backup/incremental_backup2.tar.gz /data
#Explanation:# -g: Uses a snapshot file to keep track of changes between backups.# --create: Creates a new archive (only for the initial full backup)Example 2: Remote Backups with rsync and SSH Keys
# Generate SSH key pair (if you don't have one already)ssh-keygen -t rsa
# Copy the public key to the remote serverssh-copy-id user@remote_server
# Create a backup script#!/bin/bashrsync -avz --delete /data user@remote_server:/backup/data_backup
# Run the backup script./backup_script.shExample 3: Backup Script with Error Handling and Logging
#!/bin/bash# Backup script
BACKUP_DIR="/backup"SOURCE_DIR="/data"LOG_FILE="$BACKUP_DIR/backup.log"DATE=$(date +%Y-%m-%d_%H-%M-%S)ARCHIVE_NAME="data_backup_$DATE.tar.gz"ARCHIVE_PATH="$BACKUP_DIR/$ARCHIVE_NAME"
# Log functionlog() { echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"}
# Create backup directory if it doesn't existmkdir -p "$BACKUP_DIR"
# Perform the backuplog "Starting backup..."tar -cvzf "$ARCHIVE_PATH" "$SOURCE_DIR"
# Check if the backup was successfulif [ $? -eq 0 ]; then log "Backup successful. Archive created at: $ARCHIVE_PATH"else log "Backup failed. Check the logs for errors." exit 1fi
# Verify the backuplog "Verifying backup..."tar -tvf "$ARCHIVE_PATH" > /dev/null 2>&1 # Redirect output to null to avoid cluttering the terminal.
if [ $? -eq 0 ]; then log "Verification successful."else log "Verification failed. The archive may be corrupt." exit 1fi
log "Backup completed."exit 0Example 4: Creating a Consistent Backup of a MySQL Database using LVM Snapshots
# 1. Flush tables and lock the databasemysql -u root -p -e "FLUSH TABLES WITH READ LOCK;"
# 2. Create an LVM snapshotlvcreate -L 2G -s -n mysql_snapshot /dev/vg0/mysql_volume
# 3. Unlock the databasemysql -u root -p -e "UNLOCK TABLES;"
# 4. Mount the snapshotmkdir /mnt/mysql_snapshotmount /dev/vg0/mysql_snapshot /mnt/mysql_snapshot -o ro
# 5. Copy the database files from the snapshotcp -a /mnt/mysql_snapshot/ /backup/mysql_backup
# 6. Unmount the snapshotumount /mnt/mysql_snapshotrmdir /mnt/mysql_snapshot
# 7. Remove the snapshotlvremove /dev/vg0/mysql_snapshot6. Tips & Tricks
Section titled “6. Tips & Tricks”- Use SSH keys for passwordless authentication with
rsyncandscpto automate backups. - Automate backups with
cronto schedule regular backups. - Test your backups regularly to ensure they are working correctly.
- Store backups in multiple locations (on-site and off-site) for redundancy.
- Use a strong naming convention for backup files to easily identify them.
- Monitor backup processes to identify and resolve issues quickly.
- Consider using a dedicated backup solution like Bacula, Amanda, or Duplicati for more advanced features.
- For very large files, consider using
splitto break them down into smaller chunks for easier handling. - Use
-noption with rsync for a dry-run. This allows you to see what changes would be made without actually making them. - When using dd, be extremely careful. Mistyping the
ofparameter can lead to irreversible data loss. - Use
pv(Pipe Viewer) withddto monitor its progress. Example:dd if=/dev/sda | pv > disk.img bs=4M
7. Troubleshooting
Section titled “7. Troubleshooting”- “No space left on device” error: Make sure you have enough free space on the destination disk.
- “Permission denied” error: Check file permissions and ownership. Use
sudoif necessary. - “rsync: connection unexpectedly closed” error: Check network connectivity and firewall settings.
- “tar: skipping unreadable file” error: Check file permissions and ownership. Use
--excludeto skip problematic files. - “dd: reading
/dev/sda: Input/output error” This often indicates a failing hard drive. Theconv=sync,noerroroption can help to create a usable image by padding errors with zeros, but the resulting image may contain corrupted data. - LVM snapshot is full: Increase the size of the snapshot when creating it. Monitor the snapshot’s utilization with
lvsand consider increasing its size before it fills.
8. Related Commands
Section titled “8. Related Commands”cp- Copy files and directories.mv- Move files and directories.find- Search for files and directories.df- Display disk space usage.du- Estimate file space usage.cron- Schedule commands to run automatically.scp- Securely copy files between hosts.sftp- Secure file transfer program.ssh- Secure Shell remote login program.lvs,vgs,pvs- LVM commands for displaying information about logical volumes, volume groups, and physical volumes.mount,umount- Commands for mounting and unmounting filesystems.
This cheatsheet provides a solid foundation for understanding and using Linux commands for backup and recovery. Remember to always test your backups and recovery procedures in a non-production environment before implementing them in production. Always double-check commands, especially destructive ones, before executing them.