#!/bin/bash # to run : ./deploy_mks.sh # This will backup the current contents of /opt/MarketData/MarketDataServer to /opt/MarketData/MarketDataServer_Rollback # and deploy the mks binary to /opt/MarketData/MarketDataServer # 1) Stop the mks service # 2) Perform the backup # 3) Perform the deployment # set xtrace if you want to see trace level #set -o xtrace # use systemctl so we can bypass the pager sudo systemctl --no-pager stop mks sudo systemctl --no-pager status mks # Define source and destination directories SOURCE_DIR="/home/pi/ARM64/MarketDataServer/bin/Debug/net8.0" DEST_DIR="/opt/MarketData/MarketDataServer" ROLLBACK_DIR="/opt/MarketData/MarketDataServer_Rollback" # Here begins the actual deployment process # Check if source directory exists if [ ! -d "$SOURCE_DIR" ]; then echo "Error: Source directory '$SOURCE_DIR' not found." # use systemctl so we can bypass the pager prompts sudo systemctl --no-pager start mks exit 1 fi # Check if destination directory exists, create if not if [ ! -d "$DEST_DIR" ]; then echo "Destination directory '$DEST_DIR' not found. Creating it..." mkdir -p "$DEST_DIR" fi # Check if rollback directory exists, create if not if [ ! -d "$ROLLBACK_DIR" ]; then echo "Rollback directory '$ROLLBACK_DIR' not found. Creating it..." mkdir -p "$ROLLBACK_DIR" fi # Clean the rollback folder echo "Clean the rollback folder" rm -rf $ROLLBACK_DIR/* num_files=$(find "$ROLLBACK_DIR" -maxdepth 1 -type f | wc -l) echo "Number of files after clean '$ROLLBACK_DIR': $num_files" echo "" # Backup current release echo "Backup the current release" cp -rv "$DEST_DIR"/* "$ROLLBACK_DIR"/ num_files=$(find "$ROLLBACK_DIR" -maxdepth 1 -type f | wc -l) echo "Number of files after copy to backup '$ROLLBACK_DIR': $num_files" echo "" # Remove files echo "Clean the deployment folder" find "$DEST_DIR" -mindepth 1 -path "$DEST_DIR/logs" -prune -o -exec rm -rf {} + num_files=$(find "$DEST_DIR" -maxdepth 1 -type f | wc -l) echo "Number of files after clean '$DEST_DIR': $num_files" echo "" # Copy all files from source to destination # The -v option provides verbose output (shows files being copied) # The -r option is for recursive copy, useful if source_folder contains subdirectories echo "Deploy the release candidate" cp -rv "$SOURCE_DIR"/* "$DEST_DIR"/ num_files=$(find "$DEST_DIR" -maxdepth 1 -type f | wc -l) echo "Number of files after deployment '$DEST_DIR': $num_files" echo "" echo "Files copied from '$SOURCE_DIR' to '$DEST_DIR'." # use systemctl so we can bypass the pager prompts sudo systemctl --no-pager restart mks sudo systemctl --no-pager status mks echo "Done."