If you’re looking to set up a one-way sync between a NAS running Debian and a backup machine also running Debian, rsync
is a powerful tool to achieve this. This guide will walk you through the necessary steps to ensure your files are securely and efficiently synchronised. It should work with any type of Linux and not just Debian.
1. Install rsync
First things first, ensure that rsync
is installed on both your NAS and Backup machine by running the following commands:
sudo apt update
sudo apt install rsync
2. Create an rsync Script
To achieve a one-way sync, we will create a script that uses the rsync
command. This setup ensures that if files are deleted from the Backup, they will be copied over from the NAS the next time the script runs, without overwriting any existing files on the Backup machine.
Here’s a sample script you can use:
#!/bin/bash
rsync -av --ignore-existing --delete-missing-args /path/to/nas/ user@backup-machine:/path/to/backup/
Explanation of the options:-a
: Archive mode, which preserves permissions, timestamps, symbolic links, and more.-v
: Verbose output, so you can see what rsync
is doing.--ignore-existing
: Ensures that rsync
will skip updating files that already exist on the Backup machine.--delete-missing-args
: Deletes files that are missing from the source (NAS), thereby copying them back during the next sync.
3. Set Up SSH Key-Based Authentication
While optional, setting up SSH key-based authentication between your NAS and Backup machine helps automate the process without repeatedly entering a password. Here’s how you can do it:
Generate SSH keys on your NAS:
ssh-keygen
Copy the public key to your Backup machine:
ssh-copy-id user@backup-machine
4. Schedule the Sync Using cron
To automate the sync process every hour, you’ll use cron
. Open the crontab
configuration for editing:
crontab -e
Add the following line to schedule the script to run every hour:
0 * * * * /path/to/your/rsync-script.sh
With these steps, you’ll have a robust setup for a one-way sync from your NAS to your Backup machine. The files will be copied over if they’re missing in the Backup, without affecting the original files on the NAS.