Skip to content

Latest commit

 

History

History
171 lines (128 loc) · 5.57 KB

3. Backup and Recovery.md

File metadata and controls

171 lines (128 loc) · 5.57 KB

Backup and Recovery

Backing up your data is crucial to ensure that you can recover from data loss due to hardware failure, accidental deletion, or other unexpected events. This guide covers the essential tools and strategies for performing backups and recovering data on a Linux system.

1. Introduction to Backup and Recovery

  • Importance: Regular backups protect your data and ensure that it can be restored in case of a failure.
  • Backup Types:
    • Full Backup: A complete copy of all data.
    • Incremental Backup: Copies only data that has changed since the last backup.
    • Differential Backup: Copies data that has changed since the last full backup.

2. Backup Strategies

2.1 Choosing a Backup Strategy

  • Frequency: Determine how often backups should be performed (daily, weekly, etc.).
  • Location: Decide where backups will be stored (local storage, network drives, cloud).
  • Automation: Implement automated backup processes to ensure regular backups without manual intervention.

2.2 Local vs. Remote Backups

  • Local Backups:

    • Stored on the same machine or local network.
    • Faster backup and recovery times.
    • Vulnerable to local hardware failures or disasters.
  • Remote Backups:

    • Stored on remote servers or cloud services.
    • Protects against local disasters.
    • May involve slower recovery times due to network speed.

3. Tools for Backup and Recovery

3.1 rsync

  • Overview: A versatile tool for syncing files and directories between two locations.
  • Basic Usage:
    rsync -av /source/ /backup/
    • Options:
      • -a: Archive mode (preserves permissions, timestamps, etc.).
      • -v: Verbose output.
      • --delete: Deletes files in the destination that are not in the source.

3.2 tar

  • Overview: A tool for creating compressed archive files.

  • Creating an Archive:

    tar -czvf backup.tar.gz /path/to/data
    • Options:
      • -c: Create a new archive.
      • -z: Compress the archive with gzip.
      • -v: Verbose output.
      • -f: Specifies the name of the archive file.
  • Extracting an Archive:

    tar -xzvf backup.tar.gz -C /restore/location
    • Extracts the contents of the archive to the specified location.

3.3 rsnapshot

  • Overview: A backup utility based on rsync, designed for making periodic snapshots of directories.
  • Basic Configuration:
    • Edit /etc/rsnapshot.conf to define backup intervals and locations.
    • Example Command:
      sudo rsnapshot daily
    • Automatically manages incremental backups, making it efficient for large datasets.

3.4 Bacula

  • Overview: A set of tools to manage backups, recovery, and verification across a network.

  • Basic Components:

    • Director: Manages the backup schedule.
    • Storage Daemon: Handles the backup storage.
    • File Daemon: Installed on each machine to be backed up.
  • Usage:

    • Configured via text files, allowing detailed control over backup processes.
    • Suitable for complex, multi-server environments.

3.5 Cloud-Based Backups

  • Popular Cloud Services:

    • AWS S3: Scalable cloud storage for backups.
    • Google Drive/Dropbox: Simple solutions for personal or small-scale backups.
  • Tools for Cloud Backups:

    • rclone: Syncs files and directories to and from cloud storage.
      rclone sync /path/to/data remote:backup
    • duplicity: Backs up files by encrypting them and uploading to a remote server.
      duplicity /path/to/data s3://bucket/backup

4. Implementing Automated Backups

4.1 Scheduling Backups with Cron

  • Edit Crontab:
    crontab -e
  • Example Cron Job for Daily Backup:
    0 2 * * * rsync -av --delete /source/ /backup/
    • Schedules a daily backup at 2 AM.

4.2 Using anacron for Missed Backups

  • Overview: anacron ensures scheduled tasks are executed even if the system was off during the scheduled time.
  • Configuration:
    • Edit /etc/anacrontab to define tasks and intervals.

5. Recovery Procedures

5.1 Restoring Files from Backup

  • Using rsync:

    rsync -av /backup/ /restore/
    • Restores files from the backup directory to the restore location.
  • Extracting tar Archives:

    tar -xzvf backup.tar.gz -C /restore/location
    • Extracts the backup archive to the specified directory.

5.2 Disaster Recovery

  • Full System Recovery:

    • Restore system files, configuration, and data from backups.
    • Reinstall the operating system if necessary, then restore backups.
  • Testing Backups:

    • Regularly verify that backups can be restored.
    • Perform test restores to ensure data integrity and recovery speed.

5.3 Cloud-Based Recovery

  • Restoring from Cloud:

    rclone copy remote:backup /restore/location
    • Restores data from a cloud backup to the local system.
  • Data Encryption Considerations:

    • Ensure that sensitive data is encrypted during backup and storage.
    • Use tools like duplicity to handle encryption and decryption during the backup and restore process.

Conclusion

Regular backups and a well-planned recovery strategy are essential for maintaining the integrity and availability of your data. By implementing the strategies and tools outlined in this guide, you can ensure that your data is protected and recoverable in the event of a failure.


Next: Scripting Basics

Previous: System Maintenance