update deploy script

This commit is contained in:
atef 2024-12-31 16:20:10 +01:00
parent 6da651f2e4
commit 1dac314c38
1 changed files with 38 additions and 25 deletions

View File

@ -6,19 +6,22 @@ username='ie-entwicklung'
root_password='Salimax4x25' root_password='Salimax4x25'
set -e set -e
# Define directories # Define directories and configurations
DEST_DIR="/home/$username/salimax/SaliMax" # Path to the specific file DEST_DIR="/home/$username/salimax/SaliMax" # Path to the specific file
BACKUP_DIR="/home/$username/salimax/salimax_backup/$(date +'%Y%m%d_%H%M%S')" # Backup folder with timestamp BACKUP_ROOT="/home/$username/salimax/salimax_backup" # Base backup directory
MAX_BACKUPS=10 # Limit to 10 backups TIMESTAMP=$(date +'%Y%m%d_%H%M%S')
BACKUP_DIR="${BACKUP_ROOT}/script_backup_${TIMESTAMP}"
# Backup directory with unique name
MAX_BACKUPS=5 # Limit to 5 backups
echo -e "\n============================ Build ============================\n" echo -e "\n============================ Build ============================\n"
# Build the project # Build the project
dotnet publish \ dotnet publish \
./SaliMax.csproj \ ./SaliMax.csproj \
-p:PublishTrimmed=false \ -p:PublishTrimmed=false \
-c Release \ -c Release \
-r linux-x64 -r linux-x64
echo -e "\n============================ Backup Old SaliMax File ============================\n" echo -e "\n============================ Backup Old SaliMax File ============================\n"
@ -26,35 +29,45 @@ echo -e "\n============================ Backup Old SaliMax File ================
ssh $username@$salimax_ip "mkdir -p $BACKUP_DIR" ssh $username@$salimax_ip "mkdir -p $BACKUP_DIR"
# Move the 'SaliMax' file to the backup folder on the remote machine # Move the 'SaliMax' file to the backup folder on the remote machine
ssh $username@$salimax_ip "rsync -av --exclude='$BACKUP_DIR/*' --exclude='salimax_backup/*' $DEST_DIR $BACKUP_DIR/" ssh $username@$salimax_ip "rsync -av --exclude='${BACKUP_ROOT}/script_backup_*' --exclude='salimax_backup/*' $DEST_DIR $BACKUP_DIR/"
echo -e "\n============================ Cleanup Old Backups ============================\n" echo -e "\n============================ Cleanup Old Backups ============================\n"
# Cleanup old backups if the total number exceeds the maximum allowed # Cleanup old backups if the total number exceeds the maximum allowed
echo "Existing backups:"
ssh $username@$salimax_ip "ls -1d ${BACKUP_ROOT}/script_backup_* 2>/dev/null"
# List all backups and remove the oldest if the number exceeds MAX_BACKUPS # Count the backups created by this script
BACKUP_COUNT=$(ssh $username@$salimax_ip "ls -1 /home/$username/salimax/salimax_backup | wc -l") BACKUP_COUNT=$(ssh $username@$salimax_ip "ls -1d ${BACKUP_ROOT}/script_backup_* 2>/dev/null | wc -l")
echo "Current number of backups: $BACKUP_COUNT"
if [ $BACKUP_COUNT -gt $MAX_BACKUPS ]; then if [ "$BACKUP_COUNT" -gt "$MAX_BACKUPS" ]; then
# Find and delete the oldest backup # Find and delete the oldest backup created by the script
OLD_BACKUP=$(ssh $username@$salimax_ip "ls -1 /home/$username/salimax/salimax_backup | head -n 1") OLD_BACKUP=$(ssh $username@$salimax_ip "find ${BACKUP_ROOT} -type d -name 'script_backup_*' | sort | head -n 1")
echo "Backup limit reached. Deleting old backup: $OLD_BACKUP" echo "Old backup to delete: $OLD_BACKUP"
ssh $username@$salimax_ip "rm -rf /home/$username//salimax/salimax_backup/$OLD_BACKUP"
if [ -n "$OLD_BACKUP" ]; then
echo "Backup limit reached. Deleting old backup: $OLD_BACKUP"
ssh $username@$salimax_ip "rm -rf $OLD_BACKUP" && echo "Deleted: $OLD_BACKUP" || echo "Failed to delete: $OLD_BACKUP"
else
echo "No valid backups to delete."
fi
else else
echo "Backup limit not reached. Current number of backups: $BACKUP_COUNT" echo "Backup limit not reached. Current number of backups: $BACKUP_COUNT"
fi fi
echo -e "\n============================ Deploy ============================\n" echo -e "\n============================ Deploy ============================\n"
# Deploy new files to the remote destination # Deploy new files to the remote destination
rsync -v \ rsync -v \
--exclude '*.pdb' \ --exclude '*.pdb' \
./bin/Release/$dotnet_version/linux-x64/publish/* \ ./bin/Release/$dotnet_version/linux-x64/publish/* \
$username@"$salimax_ip":~/salimax $username@"$salimax_ip":~/salimax
echo -e "\nDeployment complete!\n" echo -e "\nDeployment complete!\n"
#echo -e "\n============================ Execute ============================\n" #echo -e "\n============================ Execute ============================\n"
#sshpass -p "$root_password" ssh -o StrictHostKeyChecking=no -t "$username"@"$salimax_ip" "echo '$root_password' | sudo -S sh -c 'cd salimax && ./restart'" 2>/dev/null #sshpass -p "$root_password" ssh -o StrictHostKeyChecking=no -t "$username"@"$salimax_ip" "echo '$root_password' | sudo -S sh -c 'cd salimax && ./restart'" 2>/dev/null