Automating Blog Updates Across Multiple Servers with cron Jobs and Shell Scripts
Automating Blog Updates Across Multiple Servers with cron Jobs and Shell ScriptsLogo.jpgAbout Script With these scripts, update blog servers data just once each time.Update the first server, and the updates will propagate to the other servers automatically. And with the backup cron job, also, the data is always safe and secure.Scenario There have 3 servers for blog: blog 01, blog 02, and blog 03. Whenever update the first server (blog 01), it triggers the update of the second server (blog 02), which in turn triggers the update of the third server (blog 03). Additionally, a backup of the latest zip file is stored on a storage server through a cron job.Architecture 2023-05-19_120653.pngInformation on the serversInformation on the servers NumberServer NameIP Address01blog 0110.1.1.102blog 0210.2.2.203blog 0310.3.3.304storage server10.4.4.4Requirements Each server must be able to communicate with the others via SSH without a password.The blog services on each server must be deployed through Docker and Docker Compose.A user account (in this scenario, named "user") must have privileges to run Docker.1st Server (Blog01) In the first server, blog 01. Set up a cron job to run a shell script that checks if a directory has been modified in the last 5 minutes.crontab -e0 1 * bash /apps/bak_dnmp.sh > /apps/archive/$(date +%Y-%m-%d).logIf it has been modified, the script performs a backup of a specified directory, copies the backup to a remote server, and runs an update script on the remote server. If the directory has not been modified, the script does nothing.The shell script for blog 01:#!/bin/bash # Set the directory to check DIR="/apps/dnmp/www/" if find "$DIR" -type f -mmin -5 | grep -q "." then echo "Directory has been modified in the last 5 minutes" # Variables BACKUP_FILE="/apps/dnmp_backup_$(date +%Y-%m-%d).tar.gz" DNMP_PATH="/apps/dnmp" ARCHIVE_DIR="/apps/archive/$(date +%Y-%m-%d)" # Stop docker-compose docker-compose -f $DNMP_PATH/docker-compose.yml stop # Backup DNMP folder tar -czf "$BACKUP_FILE" $DNMP_PATH || exit 1 # Change permission chmod 777 "$BACKUP_FILE" # Copy to 2nd server scp "$BACKUP_FILE" user@10.2.2.2:/apps/ || exit 2 # Start docker-compose docker-compose -f $DNMP_PATH/docker-compose.yml up -d # Create archive directory if it doesn't exist mkdir -p "$ARCHIVE_DIR" # Move the backup file to the archive directory mv "$BACKUP_FILE" "$ARCHIVE_DIR/" # SSH to 2nd server and run update script ssh -tq user@10.2.2.2 <<EOF bash /apps/update_dnmp.sh EOF else echo "Directory has not been modified in the last 5 minutes" fiNote the last syntax of the shell script, which runs a remote command via SSH. Specifically, it connects to the 2nd server at IP address 10.2.2.2 as the user "user" using SSH. The -tq options are used to force a pseudo-terminal allocation and suppress warning and diagnostic messages. Run the update_dnmp.sh script located in the /apps directory. Once the script has completed running, the SSH connection is terminated.2nd Server (Blog02) In the second server, blog 02. Set up a shell script thatStops the Docker servicesCreates an archive of the current DNMP directoryExtracts the latest backup file from blog 01Starts the Docker servicesCopies the backup file to the 3rd server (10.3.3.3)Finally, the script runs the next update_dnmp.sh script on the 3rd server.The shell script for blog 02:#!/bin/bash # Variables DNMP_DIR="/apps/dnmp" ARCHIVE_DIR="/apps/archive/$(date +%Y-%m-%d)" DOCKER_COMPOSE_FILE="$DNMP_DIR/docker-compose.yml" BACKUP_FILE="/apps/dnmp_backup_$(date +%Y-%m-%d).tar.gz" # Stop docker-compose docker-compose -f "$DOCKER_COMPOSE_FILE" down # Create archive directory if it doesn't exist mkdir -p "$ARCHIVE_DIR" # Move old dnmp directory to the archive directory mv "$DNMP_DIR" "$ARCHIVE_DIR/" # Extract the backup file tar -xzf "$BACKUP_FILE" -C /apps mv /apps/apps/dnmp /apps && rm -r /apps/apps # Start docker-compose docker-compose -f "$DOCKER_COMPOSE_FILE" up -d # Copy the backup file to the 3rd server scp "$ARCHIVE_DIR/dnmp_backup_$(date +%Y-%m-%d).tar.gz" user@10.3.3.3:/apps/ # Move the backup file to the archive directory mv "$BACKUP_FILE" "$ARCHIVE_DIR/" # SSH to 3rd server and run update script ssh -tq user@10.3.3.3 <<EOF bash /apps/update_dnmp.sh EOF3rd Server (Blog03) In the third server, blog 03. Set up a shell script thatStops the Docker servicesCreates an archive of the current DNMP directoryUnzip the backup file from blog 02 server (10.2.2.2)Starts the Docker servicesCopies the backup file to a Storage server (10.4.4.4)The shell script for blog 03:#!/bin/bash # Set variables DNMP_DIR="/apps/dnmp" ARCHIVE_DIR="/apps/archive/$(date +%Y-%m-%d)" DOCKER_COMPOSE_FILE="$DNMP_DIR/docker-compose.yml" BACKUP_FILE="/apps/dnmp_backup_$(date +%Y-%m-%d).tar.gz" REMOTE_BACKUP_DIR="user@10.4.4.4:/wwww/backup/" # Stop docker-compose docker-compose -f "$DOCKER_COMPOSE_FILE" down # Create archive directory if it doesn't exist mkdir -p "$ARCHIVE_DIR" # Move old dnmp directory to the archive directory mv "$DNMP_DIR" "$ARCHIVE_DIR/dnmp_$(date +%H%M%S)" # Extract the backup file tar -xzf "$BACKUP_FILE" -C /apps mv /apps/apps/dnmp /apps && rm -r /apps/apps # Start docker-compose docker-compose -f "$DOCKER_COMPOSE_FILE" up -d # Move the backup file to the archive directory mv "$BACKUP_FILE" "$ARCHIVE_DIR/" # Copy the backup file to the storage server scp "$ARCHIVE_DIR/dnmp_backup_$(date +%Y-%m-%d).tar.gz" "$REMOTE_BACKUP_DIR"Telegram notification when a script is done (Alternative) Reference:Sending Telegram Messages with Linux ScriptsCreate telegram bot to get alerts from server run scriptsSend a notification on every SSH login into your serverConclusion If you have multiple Linux servers for your blog, each with a different domain, and you want to update them all at once, you can use a combination of shell scripts and cron jobs to automate the process.Related 啟用 Windows 10/11 內置 WSL2 的 Crontab 服務
啟用Windows 10/11 內置 WSL2的Crontab服務Logo.pnglogo source: PNGWing前言: Linux的Crontab排程，無論是備份還是執行任務都非常方便。但Windows 內置的 WSL2 (Windows Subsystem for Linux) Crontab服務，預設是沒有啟用，所有的排程也不會被執行，只能每次進入到WSL2內手動啟用Crontab服務。操作步驟: 進入到WSL內，設置帳號sudo免密碼。這樣是為了給Windows呼叫Crontab服務時不用輸入密碼。sudo tee /etc/sudoers.d/user <<< 'user ALL=(ALL) NOPASSWD: ALL'在Windows 的執行 (run)，開啟自啟動目錄。2022-04-22_080954.pngSHELL:STARTUP右鍵 -> 新增(W) -> 捷徑(S)對應圖示，目標(T) 及 開始位置(S) 貼上以下內容。2022-04-22_081024.png%WINDIR%\System32\wsl.exe sudo /etc/init.d/cron start%%WINDIR%\System32最後進入到WSL2，新增排程指命。2022-04-22_081054.pngcrontab -e0 0 * * 7 /home/user/archiveHW.sh @reboot /home/user/mount.sh 0 4 * * * /mnt/nas04/Tools/user/Bak_Rsync.sh結語: 這樣就可以在Windows的系統內自動執行Linux的排程及腳本。