Moving scripts to their own files, adding a basic migration script, marking most sql files for ignore
This commit is contained in:
parent
36e95d36f7
commit
52ac142907
|
|
@ -76,39 +76,11 @@ ENVEOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Checking running containers..."
|
echo "Checking running containers..."
|
||||||
# Check containers
|
|
||||||
docker compose -f docker-compose.stg.yml ps
|
|
||||||
|
|
||||||
echo "Creating migration runner script..."
|
# Copy migration and restore scripts to server
|
||||||
cat > /home/cmc/run_all_migrations.sh <<'EOF'
|
cp /home/cmc/cmc-sales-staging/deploy/scripts/run_all_migrations.sh /home/cmc/run_all_migrations.sh
|
||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
SQL_DIR="/home/cmc/cmc-sales-staging/sql/schema"
|
|
||||||
DB_USER="cmc"
|
|
||||||
DB_PASS="xVRQI&cA?7AU=hqJ!%au"
|
|
||||||
DB_NAME="cmc"
|
|
||||||
DB_HOST="127.0.0.1"
|
|
||||||
|
|
||||||
for sqlfile in "$SQL_DIR"/*.sql; do
|
|
||||||
echo "Running migration: $sqlfile"
|
|
||||||
mariadb -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$sqlfile"
|
|
||||||
done
|
|
||||||
echo "All migrations applied."
|
|
||||||
EOF
|
|
||||||
chmod +x /home/cmc/run_all_migrations.sh
|
chmod +x /home/cmc/run_all_migrations.sh
|
||||||
echo "Creating restore script for database from backups..."
|
cp /home/cmc/cmc-sales-staging/deploy/scripts/restore_db_from_backup.sh /home/cmc/restore_db_from_backup.sh
|
||||||
# Create restore script for database from backups
|
|
||||||
cat > /home/cmc/restore_db_from_backup.sh <<'EOF'
|
|
||||||
#!/bin/bash
|
|
||||||
LATEST_BACKUP=$(ls -t /backups/backup_*.sql.gz | head -n1)
|
|
||||||
echo "Restoring database from latest backup: $LATEST_BACKUP"
|
|
||||||
if [ -f "$LATEST_BACKUP" ]; then
|
|
||||||
gunzip < "$LATEST_BACKUP" | mariadb -h 127.0.0.1 -u cmc -p"xVRQI&cA?7AU=hqJ!%au" cmc
|
|
||||||
echo "Database restore complete."
|
|
||||||
else
|
|
||||||
echo "No backup file found in /backups. Skipping database restore."
|
|
||||||
fi
|
|
||||||
EOF
|
|
||||||
chmod +x /home/cmc/restore_db_from_backup.sh
|
chmod +x /home/cmc/restore_db_from_backup.sh
|
||||||
|
|
||||||
echo "Creating sync script for staging directories..."
|
echo "Creating sync script for staging directories..."
|
||||||
|
|
|
||||||
13
deploy/scripts/restore_db_from_backup.sh
Normal file
13
deploy/scripts/restore_db_from_backup.sh
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
# Sync latest backup from production
|
||||||
|
rsync -avz -e "ssh -i ~/.ssh/cmc-old" --progress cmc@sales.cmctechnologies.com.au:~/backups /home/cmc/
|
||||||
|
LATEST_BACKUP=$(ls -t /home/cmc/backups/backup_*.sql.gz | head -n1)
|
||||||
|
echo "Restoring database from latest backup: $LATEST_BACKUP"
|
||||||
|
if [ -f "$LATEST_BACKUP" ]; then
|
||||||
|
docker cp "$LATEST_BACKUP" cmc-db:/tmp/backup.sql.gz
|
||||||
|
docker exec cmc-db sh -c "gunzip < /tmp/backup.sql.gz | mariadb -u cmc -p'xVRQI&cA?7AU=hqJ!%au' cmc"
|
||||||
|
echo "Database restore complete."
|
||||||
|
else
|
||||||
|
echo "No backup file found in /home/cmc/backups. Skipping database restore."
|
||||||
|
fi
|
||||||
18
deploy/scripts/run_all_migrations.sh
Normal file
18
deploy/scripts/run_all_migrations.sh
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
SQL_DIR="/home/cmc/cmc-sales-staging/go-app/sql/schema"
|
||||||
|
DB_USER="cmc"
|
||||||
|
DB_PASS="xVRQI&cA?7AU=hqJ!%au"
|
||||||
|
DB_NAME="cmc"
|
||||||
|
DB_HOST="127.0.0.1"
|
||||||
|
|
||||||
|
for sqlfile in "$SQL_DIR"/*.sql; do
|
||||||
|
# Skip files starting with "ignore_"
|
||||||
|
if [[ "$sqlfile" == ignore_* ]]; then
|
||||||
|
echo "Skipping ignored migration: $sqlfile"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo "Running migration: $sqlfile"
|
||||||
|
mariadb -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$sqlfile"
|
||||||
|
done
|
||||||
|
echo "All migrations applied."
|
||||||
Loading…
Reference in a new issue