Image: Jack Wallen
If your business uses Linux for desktops or servers, you might be unsure of the best way to back up your data, or you might want to roll your own backup scripts. Fortunately, you’ve chosen a platform that makes this process really easy…even over a network. Thanks to a couple of tools, you can have your data backed up from source to target in no time.
The tools you need to pull this off are rsync and ssh, both of which are most likely already on your Linux machines. The primary method I am going to show only works on Linux to Linux backups. There is a way to use rsync to back up from Linux to Windows by adding Samba into the mix—that method requires you to have your Windows shares mounted on your Linux box and then use the rysnc tool to run the backup. Because of this, I’ll first show the basics of rysnc, so you can back up directory to directory, as would be the case in backing up to a mounted Windows share on a Linux box.
I’ll demonstrate this with an elementary OS Freya client and a Ubuntu 16.04 server; both of these platforms will already have rsync installed. The ssh service will probably not be found. To install this, open a terminal window (this will be done on both client and server) and issue the command:
sudo apt-get install -y openssh-server
Once that installation is complete, you’re ready to go on both machines.
The command structure of rysnc looks like this:
rsync OPTIONS SOURCE DESTINATION
Say, for example, you want to back up ~/TEST into the folder /data/. To do this, you would issue the command:
rsync -av ~/TEST /data/
The options you see above are:
- a – recurse into child directories
- v – show verbose output
There are plenty more options, but those are the two you’ll want to know to get rsync up and running. To read a complete list of options, issue the command man rsync.
If the /data directory happens to be a mounted Windows share (mounted with a command like sudo mount -t cifs -o username:domainusername //SERVER_IP/ShareFolder /data/), then everything you back up with rsync will be found on the Windows shared directory.
Backing up with ssh
Now, let’s say you don’t want to back up locally or to a Windows share; instead, you have a Linux server that stands as your backup destination. Thanks to SSH, we can employ rsync to back up those files/directories from the source to the destination…over your network.
Say we’re going to back up the folder /data to the folder /backup on a server with the IP address 192.168.1.228. We’ll have to do this with a user that holds write privileges to the /backup folder on the destination (such as root or a special user you’ve created for that purpose). The structure of this command will look like:
rsync -av -e ssh /data [email protected]:/backup/
In the above example, you would be prompted to enter the password for the user named “backup”. As soon as the credentials are accepted, the data will be transferred.
A little zipping
A nifty trick you could do is zip your folders and then back them up with rsync. Say you want to zip the folder /data/MONDAY and back it up to a remote Linux server. To do this, you would employ the zip command like so:
zip /data/MONDAY.zip /data/MONDAY && rsync -av -e ssh /data/MONDAY.zip [email protected]:/backup/
That’s it! You’re ready to take advantage of rsync as a means to back up data either locally, on mounted shared directories, or over a network with ssh.
There is far more that can be done with rsync. The next time we cover this topic, I’ll show how to automate these same backups with the help of cron.