How to Synchronize Data Between Distributed Servers Across Regions
Last updated
ssh-copy-id -i ~/.ssh/id_rsa.pub user@server_ipssh user@server_ipcurl https://rclone.org/install.sh | sudo bashrclone configrclone ls your_rclone_name:/rootrclone copy /path/to/data your_rclone_name:/root/ --progresswhich rclonevi /usr/local/bin/rclone-sync.sh#!/usr/bin/env bash
set -euo pipefail
LOG_DIR="/var/log/rclone"
LOCK_FILE="/var/lock/rclone-sync.lock"
SRC="/root/test.txt" # <-- IMPORTANT FIX: Using the full path
DST="testmumbai:/root/" # Destination path
mkdir -p "$LOG_DIR"
ts=$(date +%F_%H-%M-%S)
# Pass variables as arguments to the bash -c shell
exec /usr/bin/flock -w 0 "$LOCK_FILE" bash -c '
# Script arguments are assigned to variables for clarity
log_dir="$1"
timestamp="$2"
source="$3"
destination="$4"
echo "[$(date -Is)] start" >> "${log_dir}/sync.log"
# IMPORTANT FIX: --ignore-existing flag has been removed
rclone sync "${source}" "${destination}" \
--log-file "${log_dir}/sync-${timestamp}.log" \
--log-level INFO
rc=$?
echo "[$(date -Is)] end rc=$rc" >> "${log_dir}/sync.log"
exit $rc
' bash "$LOG_DIR" "$ts" "$SRC" "$DST"chmod +x /usr/local/bin/rclone-sync.shvi /etc/systemd/system/rclone-sync.service[Unit]
Description=Rclone periodic sync (test.txt to testmum)
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/rclone-sync.sh
[Install]
WantedBy=multi-user.targetvi /etc/systemd/system/rclone-sync.timer[Unit]
Description=Run rclone-sync.service every 1 minute
Requires=rclone-sync.service
[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
Unit=rclone-sync.service
[Install]
WantedBy=timers.targetsudo systemctl daemon-reloadsudo systemctl start rclone-sync.servicesudo systemctl enable rclone-sync.servicesudo systemctl status rclone-sync.servicesudo systemctl start rclone-sync.timersudo systemctl enable rclone-sync.servicesudo systemctl status rclone-sync.servicesudo touch test.txt
sudo echo "Health check is okay" >> test.txt
sudo cat test.txt