MySql backup and Restore
MySql backup and Restore
MySQL offers various methods for backup and recovery. Here's a brief backup and recovery
walkthrough with scripts and examples:
This shuts down the MySQL server, copies the physical directory of the database_name to a backup
location, and then restarts the server.
This applies the SQL statements in the backup.sql file to the existing database_name or creates a new
database with the same name.
For example, if you want to take backup of both sampledb and newdb database, execute the
mysqldump as shown below:
This uses the binary logs to extract specific changes within a time range and then applies them to
recover the database to a specific point in time.
---This backs up multiple databases (db1, db2, db3) into a single multi_databases.sql file.
For example, if you want to take backup of both sampledb and newdb database, execute the
mysqldump as shown below:
Write a script to loop through a list of databases and call mysqldump for each one.
#!/bin/bash
# Database credentials
MYSQL_USER="your_mysql_username"
MYSQL_PASSWORD="your_mysql_password"
# List of databases to back up (modify as needed)
DATABASES="db1 db2 db3"
# Backup directory (adjust as needed)
BACKUP_DIR="/path/to/backups"
# Loop through the databases
for db_name in $DATABASES; do
# Create the backup file path
backup_file="$BACKUP_DIR/$db_name.sql"
# Create the backup
echo "Backing up database $db_name..."
mysqldump -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$db_name" > "$backup_file"
done
Important notes:
• Replace the placeholders with your actual MySQL username, password, backup
directory path, and list of databases.
• Ensure the backup directory exists and is writable.
• Test the script thoroughly on a non-production environment before using it in
production.
• Consider error handling: Add error handling to the script to catch potential issues
during the backup process.
---In this example, we backup only the ta2 table from sampledb database.
This backs up only the table_name from the database_name into table_backup.sql.
Write a script to loop through the backup files of each database and call mysql to restore them
individually.
#!/bin/bash
# Database credentials
MYSQL_USER="your_mysql_username"
MYSQL_PASSWORD="your_mysql_password"
# Directory containing backup files (adjust as needed)
BACKUP_DIR="/path/to/backups"
# Loop through backup files
for file in "$BACKUP_DIR"/*.sql; do
# Extract database name from filename (assuming filename format is database_name.sql)
db_name=$(basename "$file" .sql)
# Restore the database
echo "Restoring database $db_name..."
mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$db_name" < "$file"
done
Important notes:
• Replace the placeholders with your actual MySQL username, password, and backup
directory path.
• Ensure the backup files are in the specified directory and have the .sql extension.
• Test the script thoroughly on a non-production environment before using it in
production.
• Consider error handling: Add error handling to the script to catch potential issues
during the restoration process.
---With gzip:
mysqldump --all-databases | gzip> databasebackup.sql.gz
This pipes the output of mysqldump directly to gzip for compression, creating a compressed backup
file backup.sql.gz.
Note: Please remember to choose the appropriate method based on your specific needs
and environment. Always test your recovery process on a non-production system before
relying on it in a critical situation.
These are just some basic examples, and the specific commands may vary depending on
your MySQL version and configuration. Refer to the official MySQL documentation for
detailed information on backup and recovery options: https://dev.mysql.com/doc/mysql-
backup-excerpt/8.0/en/