How to Set Up Automated Backups with rsync and systemd Service

Data loss can be catastrophic for businesses and individuals. Automating backups with rsync, inotify-tools, and systemd ensures your data is securely stored and easily recoverable. This guide provides a detailed, step-by-step process to configure automated backups to a remote server, with all scripts and commands in plain text for compatibility with editors like Drupal 10.

Why Automate Backups?

  • Data Protection: Safeguard against hardware failures, human errors, or cyberattacks.
  • Time Efficiency: Automate repetitive tasks to minimize manual effort and errors.
  • Scalability: Handle growing data volumes seamlessly.
  • Reliability: Ensure consistent backups with minimal downtime.

Prerequisites

  • A Linux server (e.g., Ubuntu) with root access.
  • SSH access to a remote backup server.
  • Basic knowledge of Linux commands and systemd.

Step-by-Step Guide to Automated Backups

  1. Install Required Packages
    Update your system and install rsync and inotify-tools for file synchronization and monitoring:
    sudo apt update
    sudo apt install rsync inotify-tools
  2. Set Up SSH Key Authentication
    Generate an SSH key for secure, passwordless communication with the backup server:
    ssh-keygen -t rsa -b 4096
    ssh-copy-id -p 9022 backup_user@192.168.1.100
  3. Create the Backup Script
    This script creates compressed database backups and manages old files. Create the file:
    sudo nano /home/iotdev/backup.sh
    Add the following content:

#!/bin/bash

Tạo thư mục backup

backup_dir="/home/iotdev/backup" mkdir -p "$backup_dir"

Tạo timestamp cho file

timestamp=$(date +'%Y-%m-%dT%H-%M')

Backup database (iotdev)

cd /home/iotdev/vn-ielts-test && ./vendor/bin/drush sql-dump > "$backup_dir/db_vtech_$timestamp.sql" if [ -f "$backup_dir/db_vtech_$timestamp.sql" ]; then

Nén file .sql thành .gz.tmp

gzip -c "$backup_dir/db_vtech_$timestamp.sql" > "$backup_dir/db_vtech_$timestamp.sql.gz.tmp"

Kiểm tra nếu nén thành công thì đổi tên thành .gz

if [ $? -eq 0 ]; then mv "$backup_dir/db_vtech_$timestamp.sql.gz.tmp" "$backup_dir/db_vtech_$timestamp.sql.gz"

Xóa file .sql gốc sau khi nén

rm "$backup_dir/db_vtech_$timestamp.sql" fi fi

Backup database (ama)

cd /home/ama/vn-ielts-test && ./vendor/bin/drush sql-dump > "$backup_dir/db_ama_$timestamp.sql" if [ -f "$backup_dir/db_ama_$timestamp.sql" ]; then

Nén file .sql thành .gz.tmp

gzip -c "$backup_dir/db_ama_$timestamp.sql" > "$backup_dir/db_ama_$timestamp.sql.gz.tmp"

Kiểm tra nếu nén thành công thì đổi tên thành .gz

if [ $? -eq 0 ]; then mv "$backup_dir/db_ama_$timestamp.sql.gz.tmp" "$backup_dir/db_ama_$timestamp.sql.gz"

Xóa file .sql gốc sau khi nén

rm "$backup_dir/db_ama_$timestamp.sql" fi fi

Xoá các file backup cũ hơn 10 ngày (chỉ xóa file .gz)

find "$backup_dir"/* -type f -name "*.gz" -mtime +10 -exec rm -f {} ;

  1. Set Up the rsync Service
    Create a systemd service to monitor and sync .gz files to the remote server:
    sudo nano /etc/systemd/system/auto-sync-backup.service
    Add the following content:

[Unit] Description=Auto sync .gz backup files to remote server and delete source After=network.target [Service] ExecStart=/home/iotdev/backup/auto_sync_backup.sh Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" Environment="HOME=/home/iotdev" Restart=always User=root StandardOutput=append:/var/log/sync.log StandardError=append:/var/log/sync.log [Install] WantedBy=multi-user.target

  1. Create the rsync Script
    This script uses inotifywait to monitor and sync new .gz files. Create the file:
    sudo nano /home/iotdev/backup/auto_sync_backup.sh
    sudo chmod +x /home/iotdev/backup/auto_sync_backup.sh
    Add the following content:

#!/bin/bash

Thư mục chứa file backup trên server chính

SOURCE_DIR="/home/iotdev/backup/"

Đường dẫn đến thư mục trên server backup

DEST="backup_user@192.168.1.100:/home/backup_user/backup-data/storage/"

Giám sát thư mục SOURCE_DIR để phát hiện file .gz mới

inotifywait -m "$SOURCE_DIR" -e create -e moved_to | while read -r directory events filename; do if [[ "$filename" =~ .gz$ ]]; then # Chỉ đồng bộ file .gz echo "New file detected: $filename" rsync -avz --progress -e "ssh -p 9022" "$SOURCE_DIR/$filename" "$DEST" if [ $? -eq 0 ]; then echo "Successfully synced $filename to backup server"

Xóa file nguồn sau khi đồng bộ thành công

rm -f "$SOURCE_DIR/$filename" if [ $? -eq 0 ]; then echo "Deleted source file: $filename" else echo "Failed to delete source file: $filename" fi else echo "Failed to sync $filename" fi fi done

  1. Enable and Start the Service
    Activate the systemd service to run the sync script:
    sudo systemctl daemon-reload
    sudo systemctl enable auto-sync-backup.service
    sudo systemctl start auto-sync-backup.service
    sudo systemctl status auto-sync-backup.service
  2. Test the Setup
    Verify the rsync process with a test file:
    rsync -avz --progress -e "ssh -p 9022" /home/iotdev/backup/files/videos_full_test/Writing.mp4 backup_user@192.168.1.100:/home/backup_user/backup-data/storage/

Best Practices

  • Monitor Logs: Check /var/log/sync.log regularly for sync errors or issues.
  • Secure SSH: Use strong keys and non-standard ports (e.g., 9022) for enhanced security.
  • Schedule Backups: Use cron to run backup.sh periodically for consistent backups.
  • Test Restores: Periodically verify backup restoration to ensure data integrity.

Conclusion
Automating backups with rsync and systemd provides a reliable, scalable solution for data protection. This guide offers plain text scripts and commands, ensuring compatibility with editors like Drupal 10. Start implementing these steps today to safeguard your critical data!

Call to Action
Need more Linux backup tips? Subscribe to our blog or contact us for expert guidance on data protection solutions!

Working Contact Form with Ajax & PHP

Get a functional and working contact form with Ajax & PHP in a few minutes. Just copy and paste the files, add a little code and you’re done.

Download Now