Using Rsync to backup an Ubuntu server to Synology NAS
This is a short guide on how to set up rsync on a schedule, to backup and create snapshots of any remote server that allows SSH access to your local Synology NAS. It assumes you have some knowledge of using the terminal and SSH.
1. Set up a user.
First I would recommend setting up a separate user in Synology as this allows you to enable things like speed limit controls for the user, but you could do this process under the root/admin user if you wanted.
2. Enable SFTP Access
Via the control panel File Services > FTP > SFTP > Enable SFTP service, then open up a terminal and try SSH into your NAS e.g ssh root@mynas.com or ssh root@my.local.ip, enter your password and you should be logged in, if you aren’t or nothing happens check you have port 22 open on your firewall and that it’s forwarded to your NAS.
3. Once logged in edit the passwd file
vi /etc/passwd
If your not familiar with VI check a guide here http://www.lagmonster.org/docs/vi.html or use your cursor to go down to the last line (which should be your newly added user) and enter “A” This will take you to the end of the line, you can then delete /sbin/nologin and replace with /bin/sh it should then look like this:
When you’re finished editing press ESC and then type :x, press ENTER.
I would also recommend changing the home directory (/var/services… as pictured above) to a different folder that sits in the root e.g /volume1/web-backup and adding the new user full access to this folder. As I had problems using a folder inside /homes so from here on in I’m assuming you have changed it to a folder in the root e.g /volume1/web-backup.
Allow access to a different folder with:
chown newuser:users /volume1/web-backup
4. Login as your newly created user
su - new_user
If you get any of the following messages
su: can't chdir to home directory '/volume1/web-backup' su: can't run /sbin/sh: No such file or directory
The first is a permissions error, make sure that you have chown’ed the relevant folder , second is that you forgot to add /bin/sh onto the user both are covered in step 3
5. Add the SSH key to the remote server
Create the key, accept the default file location and press enter when it prompts for a password
ssh-keygen -t rsa
Copy the key to the remote server
cat ~/.ssh/id_rsa.pub | ssh user@123.45.56.78 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Now try SSH into your remote server and it shouldn’t prompt for a password, if you get an error about permissions here’s how to set the correct permissions. .ssh directory permissions to be 700 (drwx——) and the public key (.pub file) to be 644 (-rw-r–r–). Your private key (id_rsa) should be 600 (-rw——-).
chmod /volume1/web-backup/.ssh 700;chmod /volume1/web-backup/.ssh/id_rsa.pub 644;chmod /volume1/web-backup/.ssh/id_rsa 600
6. Schedule the backup and backup structure
I created 3 folders inside my local backup folder e.g
web-backup/www
web-backup/mysql
web-backup/snapshots
First try a test rsync, don’t worry this is a dry run as in it won’t actually copy anything.
rsync --delete --stats -zav --dry-run user@remote-server.com:/var/www/ /volume1/web-backup/www
This should output something like:
Number of files: 212521 Number of files transferred: 71 Total file size: 8981539430 bytes Total transferred file size: 265780 bytes Literal data: 0 bytes Matched data: 0 bytes File list size: 5047457 File list generation time: 0.001 seconds File list transfer time: 0.000 seconds Total bytes sent: 171658 Total bytes received: 5300913 sent 171658 bytes received 5300913 bytes 142144.70 bytes/sec total size is 8981539430 speedup is 1641.19 (DRY RUN)
If you get any error like this:
rsync: failed to set times on "/volume1/web-backup/www/.": Operation not permitted (1)
It’s a permission problem check the folder your trying to write to the local folder on your NAS has the owner as the user your running rsync under in this article “newuser” is the owner of web-backup.
If all this runs smoothly you need need to create two files in the root of your web-backup folder.
web-backup/rsync.sh
web-backup/snapshot.sh
Upload these files via the admin GUI or however you wish with the following contents
rsync.sh
rsync --exclude /cache --delete --stats -zav root@remote-server.com:/var/www/ /volume1/web-backup/www rsync --delete --stats -zav root@remote-server.com:/backups /volume1/web-backup/mysql
In my case I store my MySQL backups in a seperate location so there’s a second command to get those too.
snapshot.sh
cd /volume1/web-backup/www; tar cvf /volume1/web-backup/snapshots/snapshot-$(date +%Y-%m-%d).tgz * find /volume1/web-backup/snapshots -mtime +120 -type f -exec rm -f '{}' \;
This creates a .tar archive of the /www directory and puts it in snapshots. It also checks for archives older than 120 days and removes those. You could add another snapshot for your mysql data.
7. Add the scheduled task
Finally simply add the tasks in Tasks > Task Scheduler make sure they run under the user your created at the start of this tutorial.
Missing something from this article? Please let me know and I will add it to the article
Tidak ada komentar:
Posting Komentar