π 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
- π What is rsync?
- ποΈ Types of Backup
- π οΈ Creating Backups with rsync
- 1. Full Backup
- 2. Incremental Backup
- 3. Differential Backup
- 4. Mirror Backup
- 5. Snapshot Backup
- 6. Incremental Forever Backup
- π¦ 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
- Full Backup π¦: All data from the source is copied.
- Incremental Backup β³: Only changes since the last backup are copied.
- Differential Backup π: Changes since the last full backup are copied.
- Mirror Backup πͺ: Exact mirror of the source directory.
- Snapshot Backup πΈ: State of data at a specific point in time.
- 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! ππ