This post explains how to use SSH with Rsync to make backup of your system efficiently and securely.

Server configuration

All backups will go on a new directory (/home/backup). You probably want a new SSH access to separate things.

useradd -d -m /bin/bash -G ssh-user backup
passwd backup

Create your ssh keys

Here, i just called my key “rsync”, ssh will create a private and a public key.

ssh-keygen -t ed25519 -o -a 100 -f ~/.ssh/rsync

After, just send the public key to the server with scp targeting the fresh user ‘backup’

 scp ~/.ssh/rsync.pub backup@SERVER

Create a script

Create a shell script named rsync-backup.sh with the following content:

#/usr/bin/env sh

set -o errexit

# All variables
SERVER="192.168.1.11"
BACKUP_FILE="/tmp/backup.filter"

# More efficient than playing with --include,--exclude
cat << EOF > "$BACKUP_FILE"
# Include patterns
+ /documents/***
+ /Downloads/***
+ /downloads/***
+ /images/***
+ /musics/***

# Exclude everything else
- /**
EOF

# And finally the rsync command
rsync -avP --delete -e "ssh -i $HOME/.ssh/rsync" \
    --bwlimit=400 \
    --filter="merge $BACKUP_FILE" \
    "$HOME/" "backup@${SERVER}:backups/"

The script here will backup your $HOME with only certain directory in the “include pattern list”. Just ensure the server address is correct.

I also limit the bandwidth because it break my wifi card (bad hardware…)

Run the script

Give permission and execute the script.

 chmod u+x rsync-backup.sh
./rsync-backup.sh

Using SSH with Rsync is an efficient and secure method for backing up your system. By generating SSH keys and creating a simple shell script, you can automate the backup process, ensuring that your important files are safely stored on a remote server.

Adjusting settings like bandwidth limitation can help optimize performance, especially on networks with limited resources. This approach not only provides a reliable backup solution but also enhances data security through encrypted connections.