Skip to content

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.

CommandDescriptionUse Case
tarArchive utility for creating and extracting compressed archives (tarballs).Creating full, incremental, and differential backups; archiving files for storage or transfer.
gzip, bzip2, xzCompression utilities for reducing file size.Compressing archives created by tar to save storage space and speed up transfer.
ddLow-level disk imaging and data copying tool.Creating full disk backups, cloning disks, and performing forensic analysis.
rsyncRemote and local file synchronization tool.Creating incremental backups, synchronizing files between servers, and mirroring data.
cpioAnother archive utility (less common than tar, but still useful)Similar to tar, but can handle devices directly.
dump, restoreFilesystem-level backup and recovery utilities.Creating filesystem-level backups and restoring individual files or entire filesystems. (Typically used with ext2/3/4 filesystems).
lvm snapshotsLogical Volume Manager snapshot functionalityCreating consistent point-in-time copies of logical volumes for backup purposes.
scp, sftpSecure file transfer utilities.Transferring backup archives to remote storage locations.
zstdModern, high-performance compression algorithm.Compressing backups with better compression ratios and speed compared to gzip or bzip2.

tar

Terminal window
# Create an archive
tar -cvzf archive.tar.gz /path/to/directory
# Extract an archive
tar -xvzf archive.tar.gz -C /path/to/extraction/directory
# List contents of an archive
tar -tvf archive.tar.gz

gzip

Terminal window
# Compress a file
gzip filename
# Decompress a file
gzip -d filename.gz
gunzip filename.gz #Alternative
# Compress with best compression (slowest)
gzip -9 filename
# Compress with fastest compression (least compression)
gzip -1 filename

bzip2

Terminal window
# Compress a file
bzip2 filename
# Decompress a file
bzip2 -d filename.bz2
bunzip2 filename.bz2 #Alternative

xz

Terminal window
# Compress a file
xz filename
# Decompress a file
xz -d filename.xz
unxz filename.xz #Alternative

dd

Terminal window
# Create a disk image
dd if=/dev/sda of=disk.img bs=4M status=progress
# Restore a disk image
dd if=disk.img of=/dev/sda bs=4M status=progress

rsync

Terminal window
# Synchronize a directory to a remote server
rsync -avz /path/to/local/directory user@remote_server:/path/to/remote/directory
# Synchronize a directory locally
rsync -av /path/to/source/directory /path/to/destination/directory

lvm snapshot

Terminal window
# Create a snapshot
lvcreate -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 snapshot
lvremove /dev/vg0/my_snapshot

Example 1: Full Backup with tar and gzip

Terminal window
# Create a full backup of the /home directory
tar -cvzf /backup/home_backup.tar.gz /home
# Verify the backup
tar -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

Terminal window
# First full backup
rsync -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

Terminal window
# 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

Terminal window
# Restore a specific file from the archive
tar -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 extracting

Example 5: Creating an LVM Snapshot

Terminal window
# Create a snapshot of the root volume
lvcreate -L 5G -s -n root_snapshot /dev/vg0/root
# Mount the snapshot to access its data
mkdir /mnt/root_snapshot
mount /dev/vg0/root_snapshot /mnt/root_snapshot
#Restore the root volume from the snapshot (WARNING: Data loss!)
umount /mnt/root_snapshot
lvconvert --merge /dev/vg0/root_snapshot
#Remove the snapshot
lvremove /dev/vg0/root_snapshot

Example 6: Using zstd for compression

Terminal window
# Create a tar archive and compress with zstd
tar -cf - /path/to/directory | zstd -T0 -19 -o backup.tar.zst
# Extract a zstd compressed tar archive
zstd -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.

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.
  • -1 to -9: Compression levels (gzip, bzip2, xz). -1 is fastest, -9 is best compression. zstd uses -1 to -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.

Example 1: Incremental Backups with tar and Snapshot Files

Terminal window
# First full backup
tar -cvzf /backup/full_backup.tar.gz --create /data
# Create a snapshot file
tar -g /backup/snapshot_file -cvzf /backup/incremental_backup1.tar.gz /data
# Subsequent incremental backups
tar -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

Terminal window
# Generate SSH key pair (if you don't have one already)
ssh-keygen -t rsa
# Copy the public key to the remote server
ssh-copy-id user@remote_server
# Create a backup script
#!/bin/bash
rsync -avz --delete /data user@remote_server:/backup/data_backup
# Run the backup script
./backup_script.sh

Example 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 function
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Perform the backup
log "Starting backup..."
tar -cvzf "$ARCHIVE_PATH" "$SOURCE_DIR"
# Check if the backup was successful
if [ $? -eq 0 ]; then
log "Backup successful. Archive created at: $ARCHIVE_PATH"
else
log "Backup failed. Check the logs for errors."
exit 1
fi
# Verify the backup
log "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 1
fi
log "Backup completed."
exit 0

Example 4: Creating a Consistent Backup of a MySQL Database using LVM Snapshots

Terminal window
# 1. Flush tables and lock the database
mysql -u root -p -e "FLUSH TABLES WITH READ LOCK;"
# 2. Create an LVM snapshot
lvcreate -L 2G -s -n mysql_snapshot /dev/vg0/mysql_volume
# 3. Unlock the database
mysql -u root -p -e "UNLOCK TABLES;"
# 4. Mount the snapshot
mkdir /mnt/mysql_snapshot
mount /dev/vg0/mysql_snapshot /mnt/mysql_snapshot -o ro
# 5. Copy the database files from the snapshot
cp -a /mnt/mysql_snapshot/ /backup/mysql_backup
# 6. Unmount the snapshot
umount /mnt/mysql_snapshot
rmdir /mnt/mysql_snapshot
# 7. Remove the snapshot
lvremove /dev/vg0/mysql_snapshot
  • Use SSH keys for passwordless authentication with rsync and scp to automate backups.
  • Automate backups with cron to 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 split to break them down into smaller chunks for easier handling.
  • Use -n option 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 of parameter can lead to irreversible data loss.
  • Use pv (Pipe Viewer) with dd to monitor its progress. Example: dd if=/dev/sda | pv > disk.img bs=4M
  • “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 sudo if necessary.
  • “rsync: connection unexpectedly closed” error: Check network connectivity and firewall settings.
  • “tar: skipping unreadable file” error: Check file permissions and ownership. Use --exclude to skip problematic files.
  • “dd: reading /dev/sda: Input/output error” This often indicates a failing hard drive. The conv=sync,noerror option 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 lvs and consider increasing its size before it fills.
  • 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.