Quite recently I was done setting up my services on my remote VPS and I absolutely loved it. Setting up a remote VPS removes all the hassle of setting up the hardware locally and configuring it. Now I can focus on the things that really matter; setting up the services and software. So I went ahead and composed all my services over docker so that they are easily configurable. The only thing that remained, was the backup routine. I used
rsync to create a backup routine where it backs up my remote data to my locally hosted raspberry pi. In this post I briefly go about how I did it. There could be tons of ways to do this better (I believe); if so, please do let me know. I would love to make my setup more efficient if that’s what’s required.
The entire plan was to consolidate all the files and directories that are required to be backed up under a single directory. I usually backup the persistent volumes of the docker containers and some configuration files that are critical to some of the containers. Usually those files are scattered all around the server. So I created a directory called
.backup in my home directory and then used symlinks to link all the directories and files required inside the
.backup directory. It would look something like this.
ln -s /home/$USER/test.sh /home/$USER/.backup/test.sh
The above code block will create a link of
.backup directory. Think of it like a shortcut in windows if that’s what you are familiar with. By using this I can consolidate all the files and directories required to be backed up under the single directory. Symlinks are amazing.
Public Keys and Passwordless Login
Once adding symlinks was over, I had to find a way to grab the actual content of the directory from symlinks and then back them up to the in-home raspberry pi. Since the backups happen automatically without user’s input, I had to find a way to bypass the password mechanism triggered on raspberry pi during the backup.
ssh under the hood for remote authentication and remote data transfer. So public-private key authentication is the most obvious thing to do here.
So I logged in to my remote VPS, created public-private key pairs and added the public key to the in-home raspberry pi. The walkthrough would look something like this.
ssh-keygen -t rsa -b 4096
The above command will create an RSA key of 4096 bits. The default value is 2048 bits. You can have a value of 4096 for better security. Then I copied the public key to my in-home raspberry pi using the following command.
ssh-copy-id pi@<Public Address of Raspberry PI>
This will prompt you for the password one last time. Then the above command copied the public key that was just created to the raspberry pi and then the setup was almost over. Now I could login to my raspberry pi without any password. So that also means when
rsync tries to backup the contents to the raspberry pi, it won’t prompt the password and everything will happen automatically. Yayy!
Rsync it like a boss!
Okay, that heading was a bit awkward; But I kid you not, that’s what it made me feel like once everything was done. So now I got to find a way to get the actual contents and backup everything to the raspberry pi. I did it using this simple command.
rsync -rLav ~/.backup/ pi@<Public Address of Raspberry PI>:/media/hd-01/Backups.RemoteVPS
The above command has lots of pieces to it and let me go through them one by one.
rsync is the tool that backs up the content to the remote system.
-rLav are the flags that when used together, grabs the contents of the symlinks and recursively and incrementally copies the files and directories.
pi is the username of the user in raspberry pi.
/media/hd-01/Backups.RemoteVPS is the directory path where the files will be backed up.
All in all, its pretty simple. Yet, its very powerful. I have been backing up my data nightly to my raspberry pi and never found an issue so far. I kinda love this setup to be honest.
Backing up is a critical thing for any system. Because once the data is lost, its gone forever. Maybe that data had photos and videos of good memories, or an important paperwork that would cost you a lot of time to reproduce. Regardless, having a good backup routine is as critical and important as having a good remote VPS. I’m quite happy with my setup since I haven’t tried something else yet. Is there anything I could do to improve this? Please let me know.