Problem Statement
Explain comprehensive backup and disaster recovery strategies for Linux systems. Discuss backup types, automation, testing, and recovery procedures.
Explanation
Backup types: Full backup copies all data (complete snapshot, large, slow), Incremental backup copies changes since last backup (faster, requires all previous backups for restore), Differential backup copies changes since last full backup (faster than full, easier restore than incremental), Snapshot/delta backup copies only changed blocks (efficient, requires special tools like rsnapshot, borgbackup).
Backup strategy: 3-2-1 rule - 3 copies (original + 2 backups), 2 different media types (disk + tape/cloud), 1 offsite (protection against site disasters). RPO (Recovery Point Objective) - acceptable data loss timeframe, RTO (Recovery Time Objective) - acceptable downtime for restoration.
Tools and methods:
```bash
# Tar backup
tar -czf backup-$(date +%Y%m%d).tar.gz /data --exclude='*.tmp'
# Rsync incremental backup
rsync -av --delete /data/ /backup/latest/
# Rsnapshot (hard-link based, space-efficient)
rsnapshot daily
# Borgbackup (deduplication, compression, encryption)
borg create /backup/repo::$(date +%Y%m%d) /data
# Database backup
mysqldump -u root -p --all-databases | gzip > db-backup-$(date +%Y%m%d).sql.gz
```
Automation: cron jobs for scheduled backups:
```bash
# /etc/cron.d/backup
0 2 * * * root /usr/local/bin/backup.sh 2>&1 | logger -t backup
0 3 * * 0 root /usr/local/bin/full-backup.sh 2>&1 | logger -t backup
```
Include error handling, notifications, logging. Lock files prevent overlapping backups.
What to backup: configuration files (/etc), user data (/home), application data (/var/www, /var/lib), databases, log files (for forensics), system state (package list: dpkg --get-selections > packages.txt).
Testing backups: regularly test restoration process (monthly), automate test restores to separate system, document recovery procedures, verify backup integrity (checksums, test files), test offsite retrieval.
Recovery procedures:
```bash
# File restore from tar
tar -xzf backup.tar.gz -C / path/to/file
# Rsync restore
rsync -av /backup/latest/ /data/
# Borg restore
borg extract /backup/repo::20240115 path/to/restore
# Database restore
zcat db-backup.sql.gz | mysql -u root -p
```
Disaster recovery: document DR plan, maintain recovery media (bootable USB with tools), store offsite backup credentials securely, practice full system rebuild, automate infrastructure with config management (Ansible, Puppet) enabling quick rebuild, document dependencies and configurations.
Encryption: encrypt backups containing sensitive data: tar -czf - /data | gpg -c > backup.tar.gz.gpg, store encryption keys separately, test decryption regularly.
Retention policy: daily backups for 7 days, weekly for 4 weeks, monthly for 12 months, yearly for long-term archival. Balance storage cost vs. recovery requirements. Understanding backup strategies prevents data loss and minimizes recovery time.