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:
- Set up a passwordless SSH login from your local machine to the remote backup server.
- Create a backup directory on the remote server.
remoteuser@remoteserver:$ mkdir -p ~/backup_folder/folder_01
- Ensure the SSH user has write permissions to the backup directory on the remote server.
remoteuser@remoteserver:$ chmod -R 777 ~/backup_folder/folder_01
- 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
- Verify the files have been backed up on the remote server.
remoteuser@remoteserver:$ ls -l ~/backup_folder/folder_01
- Open the crontab editor on your local machine.
localuser@localhost:$ crontab -e
- 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
Related: Crontab Generator
- Save and exit the crontab editor.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.