How to Run a Remote Backup Over SSH

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 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

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 /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 with the username and host name or IP address of your backups server.

# tar zcvf – /home/user/data_files | ssh "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.

If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for

Home » Linux » How to Run a Remote Backup Over SSH

Leave a Comment

Your email address will not be published. Required fields are marked *