This guide will explain how to run a remote backup over SSH so that your backups are stored in multiple places.
If you have a Linux server that you are using to host a web service or run anything important it’s a good idea to run frequent backups. It is an even better idea to run these backups from another location every once in a while, if you are not already copying your backups to another machine. Let’s see how to run a remote backup over SSH.
First step, of course, is to ensure that the SSH server is running correctly on your server. Launch a terminal window and run the following command, replacing hostserver.com with the host name or IP address of the server which contains the data you want to back up, and user with the username of the user you will log in as:
# ssh user@hostserver.com
Assuming things went well, log out of your SSH session. If you want to just copy the files from the remote server you can run an SCP command like shown below:
scp -r user@hostserver.com:/home/user/data_files/ /opt/backup/
In the above command we use the command scp -r which runs a recursive secure copy of the directory /home/user/data_files on your remote server and copies them to the location /opt/backup on the local server.
There’s a smarter way to do this. You can modify the command we just used so that it creates an archive of the files as it copies them. We will the tar command for this. Run the following command from the server that hosts the data you want to back up. Replace user@backupserver.com with the username and host name or IP address of your backups server.
# tar zcvf – /home/user/data_files | ssh user@backupserver.com "cat > /opt/backup/data_files.tgz"
You will most likely see a message like tar: Removing leading `/’ from member names, which you can safely ignore. After that it will prompt you for the password of the user you want to SSH it as. Then it will begin copying the tar file.
You can add the above command to a backup script if you like. Combined with a password less SSH setup it can be quite powerful.