Skip to content

Instantly share code, notes, and snippets.

@Soupala
Forked from chill117/mysql_backup.sh
Created March 30, 2014 03:50
Show Gist options
  • Save Soupala/9867212 to your computer and use it in GitHub Desktop.
Save Soupala/9867212 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment