Efficient Data Backup with rsync

Oct 2, 2024

LearnLinux TV - rsync Backup Tutorial

Introduction to rsync

  • Purpose: rsync is used to sync data from one location to another (e.g., another server, backup drive, network share).
  • Recommendation: Highly recommended utility; essential to learn for effective data backup management.

Example Server Setup

  • Example: Using Nextcloud as the server for backup.
  • Backup Server: A dedicated backup server created for this video.
  • Storage Details:
    • Nextcloud data stored in a special volume.
    • Backup server has a 200 GB block storage volume.

Initial Setup and Directory Creation

  1. Connect to Servers:
    • SSH into Nextcloud instance.
    • SSH into backup server.
  2. Prepare Backup Directory:
    • Navigate to backup server: cd /mnt/backup_data.
    • Create a directory for Nextcloud backups:
      • Command: mkdir nextcloud.learnlinux.cloud
    • Change ownership of the directory:
      • Command: sudo chown -R [username]:[group] /mnt/backup_data/nextcloud.learnlinux.cloud

Performing the Backup with rsync

  1. Switch to Root User on Nextcloud server for access to all files.
  2. Identify Source Directory:
    • Nextcloud data located at /mnt/Nextcloud_data.
  3. Dry Run with rsync:
    • Command structure:
      • rsync -avz /mnt/Nextcloud_data/ [username]@[IP address]:/mnt/backup-data/nextcloud.learnlinux.cloud
    • Importance of Dry Run: Simulates the backup process without transferring files; helps prevent overwriting important data.
  4. Execute Actual Backup:
    • Remove dry run option and run the command to copy files.
    • Verify files are copied to the backup directory.

Creating a Backup Script

  1. Clear Existing Backup (rm -rf *):
    • Be cautious with this command.
  2. Create Backup Script:
    • Save the rsync command in backup.sh.
    • Add a hashbang: #!/bin/bash
  3. Implement Date Variable in Script:
    • Capture current date for backup folder naming:
      • Example: date=$(date +%F)
    • Modify the backup destination to include the date in the folder name.
  4. Mark Script as Executable:
    • Use command: chmod +x backup.sh
  5. Run the Backup Script:
    • Check backup server for new date-named directory containing backed-up files.

Conclusion

  • Backup folders are named by date for easy tracking of backups.
  • Facilitates restoration of files from specific points in time.
  • Encourage feedback and interaction:
    • Ask viewers to like and comment on the video for future content suggestions.

Final Note

  • Thank you for watching! Looking forward to the next video.