#!/bin/bash # # Use this script to perform backups on one or more MySQL databases. # # Databases that you wish to be backed up by this script. You can have any number of databases specified; encapsilate each database name in single quotes and separate each database name by a space. # # Example: # databases=( '__DATABASE_1__' '__DATABASE_2__' ) databases=( ) # The host name of the MySQL database server; usually 'localhost' db_host="localhost" # The MySQL user to use when performing the database backup. db_user="" # The password for the above MySQL user. db_pass="" # Host name of the remote server to which the database backup files will be uploaded. # To skip the upload step, leave this blank. remote_host="" # The user that will be used when connecting to the remote server. # # Note: # You will need to set up Passwordless SSH with the remote server. remote_user="" # Date/time included in the file names of the database backup files. datetime=$(date +'%Y-%m-%dT%H:%M:%S') for db_name in ${databases[@]}; do # Create database backup and compress using gzip. mysqldump -u $db_user -h $db_host --password=$db_pass $db_name | gzip -9 > /home/backups/db/$db_name--$datetime.sql.gz done # Set strictest possible file permissions. chown root:root /home/backups/db/*--$datetime.sql.gz chmod 000 /home/backups/db/*--$datetime.sql.gz if [ "$remote_host" != '' ]; then # Use secure copy to upload backup files to remote server. scp /home/backups/db/*--$datetime.sql.gz $remote_user@$remote_host:/home/backups/db/ fi