Data is the core asset of modern IT systems, and establishing a comprehensive data backup strategy is crucial for ensuring business continuity and data security. This article will delve into data backup technologies in the Linux environment, storage media management, and remote synchronization solutions, helping readers build a reliable data protection system.
Key Data Identification and Backup Strategies
List of Critical System Directories
The data directories that need to be protected in a Linux system include:
System Configuration Data
/etc/ # System and service configuration files /root/ # Root user's home directory and configuration /var/spool/mail/ # System mail queue /var/log/ # System log files (optional)
User Data
/home/ # User home directories /var/spool/cron/ # User cron jobs /var/www/html/ # Web site data (if applicable)
Application Service Data
/var/lib/mysql/ # MySQL database files /var/lib/pgsql/ # PostgreSQL database /opt/application/ # Custom applications /usr/local/ # Locally installed software
Backup Strategy Classification
Choose the appropriate backup strategy based on business needs and recovery time requirements:
Full Backup
-
Definition: Backs up all specified files and directories
-
Advantages: Simple and quick recovery, requires only one backup set
-
Disadvantages: Takes up a lot of storage space, long backup time
-
Applicable: Scenarios with small data volumes or high recovery speed requirements
Incremental Backup
-
Definition: Only backs up files that have changed since the last backup
-
Advantages: Fast backup speed, small storage space requirement
-
Disadvantages: Requires multiple backup sets for recovery, longer recovery time
-
Applicable: Environments with large data volumes and frequent changes
Differential Backup
-
Definition: Backs up all files that have changed since the last full backup
-
Advantages: Recovery requires only the full backup + the latest differential backup
-
Disadvantages: Backup size gradually increases over time
-
Applicable: Scenarios that balance backup efficiency and recovery complexity
Tape Backup Technology
Tape Device Management
Tape drives are typically presented as /dev/st0 device files, providing high-capacity, long-term storage solutions.
Basic Tape Operations
# Install tape management tools yum install -y mt-st # Rewind operation mt -f /dev/st0 rewind # Erase tape mt -f /dev/st0 erase # Eject tape mt -f /dev/st0 offline # Check tape status mt -f /dev/st0 status # Fast forward to end of file mt -f /dev/st0 eod
Tape Data Operations
# View tape contents tar -tvf /dev/st0 # View current position contents tar -tvf /dev/st0 file.tar # View specific file # Backup data to tape tar -cvf /dev/st0 /home /etc # Full backup tar -rvf /dev/st0 /var/log # Append data # Restore data from tape tar -xvf /dev/st0 -C /restore/ # Restore to specified directory tar -xvf /dev/st0 home/user1 # Restore specific file # Seek to specific position on tape mt -f /dev/st0 fsf 2 # Skip 2 files mt -f /dev/st0 bsf 1 # Backtrack 1 file
Tape Backup Script Example
#!/bin/bash # Automated tape backup script TAPE_DEVICE="/dev/st0" BACKUP_DIRS="/home /etc /root /var/spool/mail" BACKUP_LOG="/var/log/tape_backup.log" # Check tape status if ! mt -f $TAPE_DEVICE status > /dev/null 2>&1; then echo "$(date): Tape device unavailable" >> $BACKUP_LOG exit 1 fi # Rewind preparation mt -f $TAPE_DEVICE rewind # Execute backup echo "$(date): Starting tape backup" >> $BACKUP_LOG tar -cvf $TAPE_DEVICE $BACKUP_DIRS 2>> $BACKUP_LOG if [ $? -eq 0 ]; then echo "$(date): Tape backup completed" >> $BACKUP_LOG # Eject tape mt -f $TAPE_DEVICE offline else echo "$(date): Tape backup failed" >> $BACKUP_LOG exit 1 fi
Hard Disk Backup Solutions
tar Command Backup
tar is the most basic backup tool in Linux, suitable for file-level backup operations.
Basic Backup Operations
# Full backup tar -czf /backup/system-$(date +%Y%m%d).tar.gz /home /etc /root # Backup while excluding specific files tar -czf /backup/data.tar.gz --exclude='*.tmp' --exclude='*.log' /data # Time-based incremental backup find /home -newer /backup/last_backup.timestamp -type f | \
tar -czf /backup/incremental-$(date +%Y%m%d).tar.gz -T - # Update timestamp touch /backup/last_backup.timestamp
Advanced tar Backup Strategies
# Multi-level compressed backup tar -cf - /data | gzip -9 > /backup/data-$(date +%Y%m%d).tar.gz # Split backup tar -czf - /large_data | split -b 1G - /backup/data-parts- # Verify backup integrity tar -tzf /backup/data.tar.gz > /dev/null echo "Backup verification result: $?" # Compare backup and original data tar -df /backup/data.tar.gz
dump/xfsdump Professional Backup Tools
The dump series of tools provide professional file system-level backup functionality, supporting multi-level incremental backups.
Install Backup Tools
# CentOS/RHEL systems yum install -y dump # For ext file systems yum install -y xfsdump # For XFS file systems # Ubuntu/Debian systems apt-get install -y dump e2fsprogs apt-get install -y xfsdump xfslibs-dev
xfsdump Backup Operations
# Check file system type df -T # Full backup (level 0) xfsdump -0uf /backup/boot_full.dump /boot xfsdump -f /backup/root_full.dump -L "Root_Full_Backup" -M "Session_1" /dev/sda1 # Incremental backup (level 1-9) xfsdump -1uf /backup/boot_inc1.dump /boot # First incremental xfsdump -2uf /backup/boot_inc2.dump /boot # Second incremental # View backup information xfsdump -I # View all backup sessions # Restore data xfsrestore -f /backup/boot_full.dump /restore/boot xfsrestore -f /backup/boot_inc1.dump /restore/boot # Apply incremental
Traditional dump Tool Usage
# Backup for ext file systems dump -0uf /backup/home_full.dump /home # Full backup dump -1uf /backup/home_inc1.dump /home # Incremental backup # Restore backup cd /restore restore -rf /backup/home_full.dump # Restore full backup restore -rf /backup/home_inc1.dump # Apply incremental # Interactive restore restore -if /backup/home_full.dump
dd Block-Level Backup
The dd command provides bit-level data copying functionality, suitable for full disk backups and raw device backups.
Basic dd Backup Operations
# Partition backup dd if=/dev/sda1 of=/backup/sda1.img bs=4M status=progress dd if=/dev/sdb2 of=/backup/boot.img bs=1M # Full disk clone dd if=/dev/sda of=/dev/sdb bs=4M status=progress dd if=/dev/sda of=/backup/disk_image.img bs=1M # Create ISO image dd if=/dev/cdrom of=/backup/cd.iso bs=2048 dd if=/dev/sr0 of=/tmp/dvd.iso bs=4M
Advanced dd Applications
# Compressed backup dd if=/dev/sda1 bs=4M | gzip > /backup/sda1.img.gz # Network transfer backup dd if=/dev/sda bs=4M | ssh user@remote 'dd of=/backup/remote_disk.img bs=4M' # Create virtual disk dd if=/dev/zero of=/backup/virtual_disk.img bs=1M count=1024 # Mount image file mkdir /mnt/backup_image mount -o loop /backup/sda1.img /mnt/backup_image
Recovery and Verification
# Restore partition dd if=/backup/sda1.img of=/dev/sda1 bs=4M status=progress # Verify backup integrity md5sum /dev/sda1 > /backup/sda1.md5 md5sum /backup/sda1.img >> /backup/sda1.md5 md5sum -c /backup/sda1.md5 # Mount verification mkdir /mnt/verify mount -o loop,ro /backup/boot.img /mnt/verify ls -la /mnt/verify umount /mnt/verify
Remote Backup and Synchronization
rsync Remote Synchronization
rsync is a powerful remote synchronization tool that supports incremental transfers and various synchronization modes.
Basic rsync Operations
# Local file synchronization rsync -av /source/directory/ /destination/directory/ rsync -av /etc/ /backup/etc-$(date +%Y%m%d)/ # Remote push synchronization rsync -av /local/data/ [email protected]:/remote/backup/ rsync -av /etc/passwd [email protected]:/backup/ # Remote pull synchronization rsync -av [email protected]:/remote/data/ /local/backup/
Advanced rsync Features
# Exclude specific files and directories rsync -av --exclude='*.tmp' --exclude='logs/' /data/ /backup/ # Delete extra files in the destination rsync -av --delete /source/ /destination/ # Limit transfer bandwidth rsync -av --bwlimit=1000 /large_data/ user@remote:/backup/ # Resume interrupted transfers rsync -avP /large_file user@remote:/backup/ # Compress transfer over SSH rsync -avz -e ssh /data/ user@remote:/backup/
Automated rsync Backup Script
#!/bin/bash # Automated remote backup script # Configuration variables SOURCE_DIRS="/home /etc /root /var/spool/mail" REMOTE_USER="backup" REMOTE_HOST="backup.example.com" REMOTE_PATH="/backup/$(hostname)" LOG_FILE="/var/log/remote_backup.log" EXCLUDE_FILE="/etc/backup_exclude.txt" # Create exclude list file cat > $EXCLUDE_FILE << EOF *.tmp *.log .cache/ lost+found/ /proc/ /sys/ /dev/ /tmp/ EOF # Execute remote backup echo "$(date): Starting remote backup to $REMOTE_HOST" >> $LOG_FILE for DIR in $SOURCE_DIRS; do echo "$(date): Backing up directory $DIR" >> $LOG_FILE rsync -avz --delete \
--exclude-from=$EXCLUDE_FILE \
-e "ssh -o ConnectTimeout=30" \
$DIR/ $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH$(basename $DIR)/ \
>> $LOG_FILE 2>&1 if [ $? -eq 0 ]; then echo "$(date): $DIR backup successful" >> $LOG_FILE else echo "$(date): $DIR backup failed" >> $LOG_FILE fi done echo "$(date): Remote backup completed" >> $LOG_FILE
Configure SSH Key Authentication
# Generate SSH key pair ssh-keygen -t rsa -b 4096 -C "backup@$(hostname)" # Copy public key to remote server ssh-copy-id [email protected] # Test passwordless login ssh [email protected] "echo 'SSH connection successful'" # Configure SSH client cat >> ~/.ssh/config << EOF Host backup-server HostName backup.example.com User backup Port 22 IdentityFile ~/.ssh/id_rsa StrictHostKeyChecking no EOF
Recommended Backup Strategies
3-2-1 Backup Strategy
Modern data protection follows the 3-2-1 principle:
-
3 Copies: Keep at least 3 copies of data
-
2 Media Types: Use at least 2 different storage media
-
1 Offsite Copy: Store at least 1 copy offsite
Backup Schedule Planning
# Daily incremental backup 0 2 * * * /scripts/daily_backup.sh # Weekly full backup 0 1 * * 0 /scripts/weekly_full_backup.sh # Monthly offsite backup 0 3 1 * * /scripts/monthly_offsite_backup.sh # Tape rotation backup 0 4 * * 5 /scripts/tape_rotation_backup.sh
Backup Verification and Testing
#!/bin/bash # Backup verification script BACKUP_DIR="/backup" TEST_RESTORE_DIR="/tmp/restore_test" LOG_FILE="/var/log/backup_verification.log" # Create test environment mkdir -p $TEST_RESTORE_DIR # Verify tar backups for backup in $BACKUP_DIR/*.tar.gz; do echo "$(date): Verifying backup file $backup" >> $LOG_FILE if tar -tzf $backup > /dev/null 2>&1; then echo "$(date): $backup format verification passed" >> $LOG_FILE else echo "$(date): $backup format verification failed" >> $LOG_FILE fi done # Clean up test environment rm -rf $TEST_RESTORE_DIR echo "$(date): Backup verification completed" >> $LOG_FILE
Monitoring and Reporting
Backup Status Monitoring
#!/bin/bash # Backup status monitoring script BACKUP_DIR="/backup" ALERT_EMAIL="[email protected]" MAX_AGE_HOURS=26 # Consider backups older than 26 hours as expired # Check the age of backup files find $BACKUP_DIR -name "*.tar.gz" -mtime +1 | while read old_backup; do echo "Warning: Found expired backup file $old_backup" echo "Expired backup: $old_backup" | mail -s "Backup Monitoring Alert" $ALERT_EMAIL done # Check disk space DISK_USAGE=$(df $BACKUP_DIR | awk 'NR==2 {print $5}' | sed 's/%//') if [ $DISK_USAGE -gt 80 ]; then echo "Warning: Backup disk usage reached $DISK_USAGE%" echo "Insufficient backup disk space: $DISK_USAGE%" | mail -s "Disk Space Warning" $ALERT_EMAIL fi
Disaster Recovery Planning
System Recovery Process
#!/bin/bash # System recovery guidance script echo "=== Linux System Recovery Process ===" echo "1. Boot into rescue mode or Live CD" echo "2. Create and mount target partition" echo "3. Restore system files" echo "4. Configure bootloader" echo "5. Verify system functionality" # Example recovery commands cat << 'EOF' # Partition and format fdisk /dev/sda mkfs.ext4 /dev/sda1 mkswap /dev/sda2 # Mount partition mount /dev/sda1 /mnt/sysroot # Restore system backup cd /mnt/sysroot tar -xzf /backup/system-full.tar.gz # Fix boot chroot /mnt/sysroot grub2-install /dev/sda grub2-mkconfig -o /boot/grub2/grub.cfg # Fix fstab vi /etc/fstab EOF
Conclusion
Data security and backup in Linux is one of the core skills in system management. It is hoped that readers can achieve the following levels:
-
Identify critical data in the system and formulate appropriate backup strategies
-
Master backup technologies for different storage media such as tape and hard disk
-
Proficiently use professional backup tools like tar, dump, and dd
-
Configure rsync for efficient remote data synchronization
-
Establish a complete backup verification and monitoring system
-
Develop a reliable disaster recovery plan
Data backup is not only a technical operation but also a risk management strategy. It is recommended that readers establish a multi-layered data protection system suitable for their environment, ensuring rapid recovery of business operations in various failure scenarios.