Most of us have experienced the loss of valuable files and data at some point in time when a local storage system fails. We know the importance of backing up our files and data, but few have implemented a proper backup solution.

Good backup strategies include creating a consistent and automatic backup and store the backup in a remote location.

Automated remote backup in Linux could be implemented by automating rsync to back up to a remote SSH server.

Steps to automatically backup to remote server using SSH and rsync:

  1. Configure passwordless SSH login from your local machine to the remote backup server.
  2. Create a directory on the remote server as a backup target.
    remoteuser@remoteserver:$ mkdir -p ~/backup_folder/folder_01
  3. Make sure the connecting SSH user has full access to the directory on the remote backup server.
    remoteuser@remoteserver:$ chmod -R 777 ~/backup_folder/folder_01
  4. Manually run your backup script on your local machine to test if the backup operation is successful.
    localuser@localhost:$ rsync -av --delete /path/to/folder_01/ remoteuser@remoteserver:backup_folder/folder_01

    Sample of a more complete script for automated backup.

    backup.sh
    #!/bin/bash
     
    TARGET="remoteuser@remoteserver:~/backup_folder"
     
    for i in folder_01 folder_02 folder_03; do
    	rsync -av --delete $i/ $TARGET/$i;
    done
  5. Check on the remote server if the files were successfully backed up.
    remoteuser@remoteserver:$ ls -l ~/backup_folder/folder_01

  6. Open crontab editor on the local machine.
    localuser@localhost:$ crontab -e
  7. Configure cron on your local machine to automatically run your backup script at a set time.
    # Run backup command every day on midnight, sending the logs to a file.
    0 0 * * * rsync -av --delete /path/to/folder_01/ remoteuser@remoteserver:backup_folder/folder_01 >>~/.backup.log 2>&1 
  8. Save and exit crontab editor.
Discuss the article:

Comment anonymously. Login not required.

Share!