Backup Files to a Remote Server using a Bash Script

Backup a File to a Remote Server

A bash script is one method to backup files to a remote server. An account is needed with a SSH key pair and a file needs to be created at ~/.ssh/config so the script can automatically login to the remote server. Also, you can set up a CRON job to run the script nightly. Be sure to not add a passphrase to the private key. Setting up SSH authentication is beyond the scope of this article.

The primary command in the script is the ‘scp’ command. SCP is good if you need a copy a single file at a time. The scp command uses SSH underneath the hood. As such, it will automatically check for a a ~/.ssh/config file and use it to authenticate to the remote server.

If you need to backup multiple files, modify the script and add additional variables, updates checks, and scp commands. If you need to transfer entire directories use the rsync command. However, rsync can sometime have issues using an identity file. Rsync like SCP should automatically use the ~/.ssh/conf file by default.

Lets set up the head of the script and call bash and add any comments.

Next, let’s set up the script variables.

Next, lets test if the file path exists!

Finally, we will copy the file to the remote server using scp or rsync.

Ref. https://unix.stackexchange.com/questions/127352/specify-identity-file-id-rsa-with-rsync

Related Posts