Skip to content

Commit

Permalink
Fix scripts for syncing databases
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhoy94 committed May 1, 2024
1 parent d26c419 commit d6b5f15
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
3 changes: 1 addition & 2 deletions scripts/sync-local-db-from-production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ mv "$SCRIPT_DIR/database_dump.sql" "$BACKUP_DIR/remote_dump.sql"
echo "" >> "$BACKUP_DIR/migration_log.txt"
echo "//-----------------------------------------------------//" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration from remote server complete!" >> "$BACKUP_DIR/migration_log.txt"
echo "Backup file name: $BACKUP_FILE" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration description: $MIGRATION_DESCRIPTION" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration time: $(date +'%Y-%m-%d %H:%M:%S')" >> "$BACKUP_DIR/migration_log.txt"

echo "Migration log updated."
echo "Migration complete!"
60 changes: 51 additions & 9 deletions scripts/sync-production-db-from-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Load environment variables from .env file
source "$SCRIPT_DIR/.env"

# Function to ask user for migration description
ask_for_description() {
echo "Please enter a short description for this migration:"
read -r MIGRATION_DESCRIPTION
if [ -z "$MIGRATION_DESCRIPTION" ]; then
echo "Migration description cannot be empty. Please try again."
ask_for_description
fi
}

# Ask user for migration description
ask_for_description

# Set backup directory based on current year and month
BACKUP_DIR="$SCRIPT_DIR/backups/$(date +'%Y/%m')/prod_sync_$(date +'%Y-%m-%d_%H-%M-%S')"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Dump local MySQL database
echo "Dumping local database..."
dump_output=$(docker exec $MARIADB_CONTAINER_NAME mysqldump -u $LOCAL_DB_USER -p$LOCAL_DB_PASSWORD $LOCAL_DB_NAME --ignore-table=$LOCAL_DB_NAME.wp_users 2>&1)
Expand All @@ -17,18 +36,38 @@ if [ $? -ne 0 ]; then
exit 1
fi

echo "$dump_output" > "$SCRIPT_DIR/database_dump.sql"
echo "$dump_output" > "$BACKUP_DIR/local_database_dump.sql"

# Transfer the database dump file to the remote server
echo "Transferring database dump to remote server..."
scp "$SCRIPT_DIR/database_dump.sql" "$SSH_LOGIN@$SSH_SERVER:/tmp"
scp "$BACKUP_DIR/local_database_dump.sql" "$SSH_LOGIN@$SSH_SERVER:/tmp"


# Dump remote MySQL database directly to the specified file
echo "Dumping remote database to file..."
ssh_result=$(ssh "$SSH_LOGIN@$SSH_SERVER" "mysqldump -h $REMOTE_DB_HOST -u $REMOTE_DB_USER -p'$REMOTE_DB_PASSWORD' $REMOTE_DB_NAME --ignore-table=$REMOTE_DB_NAME.wp_users --no-tablespaces > database_dump.sql" 2>&1)

# Check if the SSH command was successful
if [ $? -eq 0 ]; then
echo "Remote database dumped successfully."
else
echo "Error dumping remote database: $ssh_result"
exit 1
fi

# Check if the database_dump.sql file was created on the remote server
ssh "$SSH_LOGIN@$SSH_SERVER" "[ -e database_dump.sql ] && echo 'database_dump.sql exists' || echo 'database_dump.sql does not exist'"

# Transfer the database dump file from the remote server
echo "Transferring database dump from remote server..."
scp "$SSH_LOGIN@$SSH_SERVER:database_dump.sql" "$BACKUP_DIR/production_dump.sql"

# Connect to SSH server and import the database dump
echo "Connecting to SSH server and importing database dump..."
ssh -T "$SSH_LOGIN@$SSH_SERVER" <<EOF
# Import the database dump
echo "Importing database dump..."
mysql -h "$REMOTE_DB_HOST" -u "$REMOTE_DB_USER" -p"$REMOTE_DB_PASSWORD" "$REMOTE_DB_NAME" < /tmp/database_dump.sql
mysql -h "$REMOTE_DB_HOST" -u "$REMOTE_DB_USER" -p"$REMOTE_DB_PASSWORD" "$REMOTE_DB_NAME" < /tmp/local_database_dump.sql
# Check if import command executed successfully
if [ $? -ne 0 ]; then
Expand All @@ -48,11 +87,14 @@ ssh -T "$SSH_LOGIN@$SSH_SERVER" <<EOF
# Remove the temporary dump file
echo "Cleaning up..."
rm /tmp/database_dump.sql
rm /tmp/local_database_dump.sql
rm database_dump.sql
EOF

# Clean up local database dump file
echo "Cleaning up local database dump..."
rm "$SCRIPT_DIR/database_dump.sql"

# Write migration log entry
echo "" >> "$BACKUP_DIR/migration_log.txt"
echo "//-----------------------------------------------------//" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration to remote server complete!" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration description: $MIGRATION_DESCRIPTION" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration time: $(date +'%Y-%m-%d %H:%M:%S')" >> "$BACKUP_DIR/migration_log.txt"
echo "Migration log updated."
echo "Migration complete!"

0 comments on commit d6b5f15

Please sign in to comment.