07 August 2024

Backup Multiple MySQL Tables Into One Single Compressed File

 #!/bin/bash

# Set variables
DB_NAME="usakbasari"
OUTPUT_FILE="/var/www/citizen_tables_backup.sql.gz"
TABLES_FILE="/tmp/citizen_tables.txt"

# Prompt for MySQL password
echo -n "Enter MySQL root password: "
read -s MYSQL_PASSWORD
echo

# Fetch the list of tables
mysql -u root -p$MYSQL_PASSWORD -N -e "SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = '$DB_NAME' AND TABLE_NAME LIKE 'citizen_%'" > $TABLES_FILE

# Create or clear the output file
> $OUTPUT_FILE

# Read the table names and back up in chunks
while read -r TABLE; do
    echo "Backing up $TABLE..."
    mysqldump -u root -p$MYSQL_PASSWORD $DB_NAME $TABLE | gzip >> $OUTPUT_FILE
done < $TABLES_FILE

echo "Backup complete: $OUTPUT_FILE"

No comments:

Post a Comment