Data loss is a significant risk for any Linux server or desktop. Implementing a reliable backup solution is crucial to safeguarding your files. One effective method is setting up automatic remote backups, which helps protect against local storage failures.

Automating the backup process in Linux can be done using rsync and cron. rsync efficiently transfers files to a remote SSH server, while cron schedules these transfers to happen automatically. This approach ensures that your backups are consistent and reliable without requiring manual intervention.

By configuring these tools, you can create a secure and automated backup system for your Linux environment. This setup minimizes data loss risks by keeping a copy of your important files in a remote location, ready for recovery when needed.

Steps to configure automatic remote backups on Linux:

  1. Set up a passwordless SSH login from your local machine to the remote backup server.
  2. Create a backup directory on the remote server.
    remoteuser@remoteserver:$ mkdir -p ~/backup_folder/folder_01
  3. Ensure the SSH user has write permissions to the backup directory on the remote server.
    remoteuser@remoteserver:$ chmod -R 777 ~/backup_folder/folder_01
  4. Manually test the rsync command to verify the backup operation.
    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. Verify the files have been backed up on the remote server.
    remoteuser@remoteserver:$ ls -l ~/backup_folder/folder_01
  6. Open the crontab editor on your local machine.
    localuser@localhost:$ crontab -e
  7. Schedule the backup script to run automatically using cron.
    # 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 the crontab editor.
Discuss the article:

Comment anonymously. Login not required.