This post explains how to SSH with Rsync to make backup of your system efficiently and securely.
The only requisite to start here is an access to SSH somewhere.
Create your ssh keys
Here, i just create a keypair called “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 your server.
scp ~/.ssh/rsync.pub backup@SERVER
Create a backup 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…)
Execute the script
Just give permission and launch the script.
chmod u+x rsync-backup.sh
./rsync-backup.sh
Conclusion
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.