/ Bash Scripts

Automate backups to remote servers

This guide explains how to create a Bash script for automating the process of creating backups and storing them on remote servers. By following the steps, you will learn how to build a script capable of receiving customized options and settings from the user. The script will make use of tools such as rsync and ssh for file transfer and remote server interaction.


You should have:

  1. A basic understanding of Bash scripting.
  2. Familiarity with ssh and rsync.
  3. Access to a Linux server where backups will be created and a remote server where backups will be stored.


Before creating the script, set up ssh key-based authentication between your local and remote server to enable password-less logins. This step is crucial to automating the script.

  1. On the local server, generate an ssh key pair with the ssh-keygen command:
   ssh-keygen -t rsa -b 4096
  1. Copy the public key to the remote server:
   ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote-server
  1. Test the connection:
   ssh username@remote-server

If the ssh connection is successful, you can now build the backup script.

Backup Script

The backup script will use rsync for transferring files, compressing them during the transfer to save bandwidth. Let’s start creating the script:

  1. Create a new bash script file:
   touch backup_script.sh
  1. Open the file with your preferred text editor:
   nano backup_script.sh
  1. Add the following content to the file:

   # Variables

   # Check for missing arguments
   if [ -z "$LOCAL_PATH" ] || [ -z "$REMOTE_USER" ] || [ -z "$REMOTE_IP" ] || [ -z "$REMOTE_PATH" ]
     echo "Error: Missing arguments."
     echo "Usage: ./backup_script.sh [local_path] [remote_user] [remote_ip] [remote_path]"
     exit 1

   # Rsync command for backup

   if [ $? -eq 0 ]
     echo "Backup completed successfully."
     echo "Backup failed. Please check your inputs and try again."

This script accepts four arguments:

  • local_path: The path of the directory you want to backup on the local server.
  • remote_user: The username of the remote server.
  • remote_ip: The IP address of the remote server.
  • remote_path: The path on the remote server where you want to store the backup.
  1. Save the file and exit the editor.
  2. Set execute permissions on the script:
   chmod +x backup_script.sh


To use the backup script, call it with the necessary arguments:

./backup_script.sh /path/to/local/dir username remote-ip /path/to/remote/dir

Replace /path/to/local/dir, username, remote-ip, and /path/to/remote/dir with your own information.

The script will then backup the specified local directory to the specified remote directory.


This guide provides the necessary steps to create a Bash script that automates the backup of data to remote servers. The script checks for input validity, and it gives user-friendly error messages if something goes wrong. As always, make sure to test the script thoroughly before using it in a production environment.

For more advanced usage, you could extend the script by adding more options, logging operations, or even integrating with a scheduling system like cron for regular automated backups.

Was this helpful?

Thanks for your feedback!