Linux system backup and recovery are important guarantees for data security, requiring mastery of various backup strategies and recovery methods.
Backup types:
- Full Backup: backup all data
- Incremental Backup: backup only data changed since last backup
- Differential Backup: backup data changed since last full backup
- Hot Backup: perform backup while system is running
- Cold Backup: perform backup while system is stopped
Backup tools:
- tar: archiving tool
- Common options:
- -c: create archive
- -x: extract archive
- -v: display detailed information
- -f: specify archive file
- -z: use gzip compression
- -j: use bzip2 compression
- Examples:
- tar -czvf backup.tar.gz /path/to/backup: create compressed backup
- tar -xzvf backup.tar.gz: extract compressed backup
- tar -czvf backup-$(date +%Y%m%d).tar.gz /path/to/backup: backup with timestamp
- Common options:
- rsync: synchronization and backup tool
- Common options:
- -a: archive mode, preserve file attributes
- -v: display detailed information
- -z: compress during transfer
- --delete: delete extra files in destination
- --exclude: exclude files or directories
- Examples:
- rsync -avz /source/ /destination/: synchronize directories
- rsync -avz --delete /source/ /destination/: synchronize and delete extra files
- rsync -avz --exclude '*.log' /source/ /destination/: exclude log files
- Common options:
- dd: disk and partition backup tool
- Examples:
- dd if=/dev/sda of=/backup/sda.img: backup entire disk
- dd if=/dev/sda1 of=/backup/sda1.img: backup partition
- dd if=/backup/sda.img of=/dev/sda: restore disk
- Examples:
- dump/restore: filesystem backup tools (ext2/ext3/ext4)
- dump -0u -f /backup/root.dump /: backup root filesystem
- restore -rf /backup/root.dump: restore filesystem
- Bacula: enterprise-level backup solution
- Amanda: Advanced Maryland Automatic Network Disk Archiver
- Duplicity: encrypted backup tool
- Borg: deduplication backup tool
Database backup:
- MySQL:
- mysqldump: logical backup tool
- mysqldump -u root -p database > backup.sql: backup database
- mysqldump -u root -p --all-databases > all.sql: backup all databases
- mysqldump -u root -p --single-transaction database > backup.sql: consistent backup
- mysql -u root -p database < backup.sql: restore database
- mysqlhotcopy: physical backup tool (MyISAM)
- Percona XtraBackup: physical backup tool (InnoDB)
- mysqldump: logical backup tool
- PostgreSQL:
- pg_dump: logical backup tool
- pg_dump database > backup.sql: backup database
- pg_dumpall > all.sql: backup all databases
- psql database < backup.sql: restore database
- pg_basebackup: physical backup tool
- pg_dump: logical backup tool
- MongoDB:
- mongodump: backup tool
- mongorestore: recovery tool
System backup strategy:
- 3-2-1 principle:
- 3 backup copies
- 2 different media
- 1 offsite backup
- Backup frequency:
- Full backup: once a week
- Incremental backup: once a day
- Differential backup: once a day
- Retention policy:
- Daily backups kept for 7 days
- Weekly backups kept for 4 weeks
- Monthly backups kept for 12 months
- Automated backup:
- Use cron to execute backup scripts regularly
- Example:
bash
#!/bin/bash BACKUP_DIR="/backup" DATE=$(date +%Y%m%d) tar -czvf $BACKUP_DIR/backup-$DATE.tar.gz /path/to/backup find $BACKUP_DIR -name "backup-*.tar.gz" -mtime +7 -delete
Cloud backup solutions:
- AWS S3: object storage service
- Azure Blob Storage: Azure object storage
- Google Cloud Storage: Google cloud storage
- Alibaba Cloud OSS: Alibaba cloud object storage
- Tencent Cloud COS: Tencent cloud object storage
- rclone: multi-cloud storage synchronization tool
Backup verification:
- Regularly test recovery process
- Verify backup integrity
- Check backup file size
- Use checksums to verify data
Recovery strategies:
- Full recovery: recover from full backup
- Incremental recovery: start from full backup, apply incremental backups in sequence
- Differential recovery: start from full backup, apply latest differential backup
- Point-in-time recovery: recover to specified point in time (requires binlog)
- Disaster recovery: offsite backup recovery
Backup best practices:
- Document backup strategy
- Automate backup process
- Regularly test recovery
- Encrypt sensitive data
- Monitor backup status
- Keep multiple backup versions
- Offsite backup
- Regularly review backup strategy
- Document recovery process
Troubleshooting:
- Backup failure: check disk space, permissions, network connection
- Recovery failure: verify backup file integrity, check destination path
- Performance issues: optimize backup time, use incremental backups
- Insufficient storage: clean old backups, use compression