Automate Daily Backup (Sync) Ubuntu Server Folder
Automating daily backups is crucial for protecting valuable data on remote servers. Without a proper backup plan, accidental data loss, hardware failures, or unforeseen issues can lead to significant disruptions. In this post, we’ll explore how to automate the process of backing up a specific folder on a remote Ubuntu server.
The rsync
command on Ubuntu is a versatile tool for syncing files and directories, which can be used for backups as it efficiently copies and synchronizes data locally or remotely, preserving permissions, timestamps, and optionally deleting files no longer present at the source. In this case we are not deleting files which are no longer present at the source.
To ensure that files not present on the source are removed from the destination, you need to add the --delete
option and remove --ignore-existing
.
Introduction
In this article, I will present you my way to automate daily backup with a Bash script of the folder on remote Ubuntu Server.
Backing up data is crucial for any system administrator to ensure data integrity and availability. Automating backups can save time and reduce the risk of human error. In this post, we will guide you through setting up a daily backup of a folder from a remote Ubuntu server using a bash script and rsync
.
Prerequisites
- Two Ubuntu machines (one acting as the backup server, another as the source).
- SSH access between the machines. -> Click on the link to check how to configure it
- Basic knowledge of bash scripting and
rsync
.
The Bash Script for Automate Daily Folder Backup
Create a script:
nano /opt/scripts/bkp_pi-share.sh
#!/bin/sh
runtime=$(date +%Y%m%d_%H%M)
# Createa a log entry for the executed backup
echo "Starting sync between remote host and folder on local host" >> /opt/scripts/log/bkp_pi-share_$runtime.log
# Write a exact time before start
echo $(date) >> /opt/scripts/log/bkp_pi-share_$runtime.log
# Source system: user@REMOTE_HOST_IP:/remote_folder/
# Destination: folder on the local server
sudo rsync -avzh --ignore-existing --rsync-path="sudo rsync" ubuntu@192.168.20.116:/srv/share/ /opt/data/bkp_pi-share >> /opt/scripts/log/bkp_pi-share_$runtime.log
Script Breakdown
runtime=$(date +%Y%m%d_%H%M)
: This line sets theruntime
variable to the current date and time, formatted as YYYYMMDD_HHMM.echo "Start sync..." >> /opt/scripts/log/bkp_pi-share_$runtime.log
: Logs the start of the sync operation.sudo rsync -avzh --ignore-existing --rsync-path="sudo rsync"
: Usesrsync
to synchronize the files from the remote server to the local backup directory. The--ignore-existing
flag ensures that only new or modified files are copied, saving time and bandwidth.
Make the script executable:
chmod +x /opt/scripts/bkp_pi-share.sh
Setting Up the Cron Job
To automate the script to run daily, add a cron job:
- Open the crontab file:
crontab -e
- Add the following line to schedule the script to run daily at 2 AM:
0 2 * * * /opt/scripts/bkp_pi-share.sh