Skip to content

πŸ“‚ Rsync Backup Tutorial: Types and Scripts

Welcome to this tutorial on using rsync for different types of backups. We’ll cover how to create full, incremental, differential, mirror, snapshot, and incremental forever backups using rsync. Plus, we’ll provide options for both local and remote backups. πŸš€

πŸ“‘ Table of Contents

  1. πŸ” What is rsync?
  2. πŸ—‚οΈ Types of Backup
  3. πŸ› οΈ Creating Backups with rsync
  4. 1. Full Backup
  5. 2. Incremental Backup
  6. 3. Differential Backup
  7. 4. Mirror Backup
  8. 5. Snapshot Backup
  9. 6. Incremental Forever Backup
  10. πŸ“¦ How to Archive and Compress Data Before rsync

πŸ” What is rsync?

rsync is a powerful command-line tool for syncing files and directories between two locations. It’s highly efficient and flexible, ideal for backups and mirroring. Key features include:

  • Delta Transfers 🌐: Only changes are transferred.
  • Compression πŸ’¨: Reduces data transfer size.
  • Incremental Transfers πŸ”„: Syncs only changed files.
  • Versatility πŸ“: Use locally or remotely.

πŸ—‚οΈ Types of Backup

  1. Full Backup πŸ“¦: All data from the source is copied.
  2. Incremental Backup ⏳: Only changes since the last backup are copied.
  3. Differential Backup πŸ“ˆ: Changes since the last full backup are copied.
  4. Mirror Backup πŸͺž: Exact mirror of the source directory.
  5. Snapshot Backup πŸ“Έ: State of data at a specific point in time.
  6. Incremental Forever Backup ♾️: Continuous incremental backups after the initial full backup.

πŸ› οΈ Creating Backups with rsync

1. Full Backup πŸ“¦

A full backup copies all data from the source to the backup destination.

Script Example:

#!/bin/bash
# Full backup with rsync

SOURCE="/path/to/source/"

# Uncomment the following line for a local backup
# rsync -avz --progress $SOURCE /path/to/backup/

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
DESTINATION="/path/to/backup/"
rsync -avz --progress $SOURCE $REMOTE_USER@$REMOTE_HOST:$DESTINATION

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/backup.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/backup.tar.gz

2. Incremental Backup ⏳

An incremental backup only copies files that have changed since the last backup.

Script Example:

#!/bin/bash
# Incremental backup with rsync

SOURCE="/path/to/source/"

# Uncomment the following line for a local backup
# rsync -avz --progress --link-dest=/path/to/backup/incremental/ $SOURCE /path/to/backup/

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
DESTINATION="/path/to/backup/"
INCREMENTAL_DIR="/path/to/backup/incremental/"
rsync -avz --progress --link-dest=$REMOTE_USER@$REMOTE_HOST:$INCREMENTAL_DIR $SOURCE $REMOTE_USER@$REMOTE_HOST:$DESTINATION

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/incremental.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/incremental.tar.gz

3. Differential Backup πŸ“ˆ

A differential backup copies files that have changed since the last full backup.

Script Example:

#!/bin/bash
# Differential backup with rsync

SOURCE="/path/to/source/"

# Uncomment the following line for a local backup
# rsync -avz --progress --compare-dest=/path/to/backup/full/ $SOURCE /path/to/backup/

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
DESTINATION="/path/to/backup/"
FULL_BACKUP="/path/to/backup/full/"
rsync -avz --progress --compare-dest=$REMOTE_USER@$REMOTE_HOST:$FULL_BACKUP $SOURCE $REMOTE_USER@$REMOTE_HOST:$DESTINATION

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/differential.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/differential.tar.gz

4. Mirror Backup πŸͺž

A mirror backup creates an exact copy of the source directory, including deletions.

Script Example:

#!/bin/bash
# Mirror backup with rsync

SOURCE="/path/to/source/"

# Uncomment the following line for a local backup
# rsync -avz --delete --progress $SOURCE /path/to/backup/

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
DESTINATION="/path/to/backup/"
rsync -avz --delete --progress $SOURCE $REMOTE_USER@$REMOTE_HOST:$DESTINATION

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/mirror.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/mirror.tar.gz

5. Snapshot Backup πŸ“Έ

A snapshot backup captures the state of the data at a specific point in time using hard links for unchanged files.

Script Example:

#!/bin/bash
# Snapshot backup with rsync

SOURCE="/path/to/source/"
SNAPSHOT_NAME="backup-$(date +%Y%m%d-%H%M%S)"
LATEST="/path/to/backup_root/latest"

# Uncomment the following line for a local backup
# rsync -avz --link-dest=$LATEST $SOURCE /path/to/backup_root/$SNAPSHOT_NAME

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
BACKUP_ROOT="/path/to/backup_root/"
ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $BACKUP_ROOT/$SNAPSHOT_NAME"
rsync -avz --link-dest=$REMOTE_USER@$REMOTE_HOST:$LATEST $SOURCE $REMOTE_USER@$REMOTE_HOST:$BACKUP_ROOT/$SNAPSHOT_NAME
ssh $REMOTE_USER@$REMOTE_HOST "rm -f $LATEST; ln -s $BACKUP_ROOT/$SNAPSHOT_NAME $LATEST"

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/snapshot.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/snapshot.tar.gz

6. Incremental Forever Backup ♾️

An incremental forever backup involves an initial full backup followed by incremental backups, maintaining a chain of changes.

Script Example:

#!/bin/bash
# Incremental forever backup with rsync

SOURCE="/path/to/source/"

# Uncomment the following line for a local backup
# rsync -avz --link-dest=/path/to/backup/incremental/ $SOURCE /path/to/backup/backup-$(date +%Y%m%d-%H%M%S)/

# Uncomment the following lines for a remote backup
REMOTE_USER="user"
REMOTE_HOST="remote_server"
DESTINATION="/path/to/backup/"
INCREMENTAL_DIR="/path/to/backup/incremental/"
rsync -avz --link-dest=$REMOTE_USER@$REMOTE_HOST:$INCREMENTAL_DIR $SOURCE $REMOTE_USER@$REMOTE_HOST:$DESTINATION/backup-$(date +%Y%m%d-%H%M%S)/

# Uncomment the following line to archive and compress before syncing
# tar -cvzf /tmp/incremental_forever.tar.gz $SOURCE

# Uncomment the following line to compress the archive after syncing
# gzip /tmp/incremental_forever.tar.gz

πŸ“¦ How to Archive and Compress Data Before rsync

To archive and compress data before syncing with rsync, you can use tar and gzip. Uncomment the relevant lines in the scripts if you want to archive and compress before syncing.

Archiving and Compressing Example:

# Create a tar archive
tar -cvzf /tmp/backup.tar.gz $SOURCE

# Compress the tar archive
gzip /tmp/backup.tar.gz

This tutorial now offers flexibility for both local and remote backups, with clear instructions on how to adjust the scripts accordingly. Happy backing up! πŸš€πŸ“‚