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. -> How to configure it: https://optimusing.dyndns.org/linux-tutorial-basic/solutions-connect-to-the-ubuntu-server-via-ssh-without-typig-the-password/
- Basic knowledge of bash scripting and
rsync
.
The Bash Script
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 the
runtime
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”: Uses
rsync
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