Last active
August 29, 2015 14:00
-
-
Save aa65535/11083063 to your computer and use it in GitHub Desktop.
Shell script to backup MySql database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Shell script to backup MySql database. | |
# Usage: dbbackup db_name / dbbackup "db_name1 db_name2 db_name3" | |
# -------------------------------------------------------------------- | |
MyUSER="user" # USERNAME | |
MyPASS="pwd" # PASSWORD | |
MyHOST="localhost" # HOSTNAME | |
# Backup Dest directory, change this if you have someother location | |
DEST="/backup" | |
# Main directory where backup will be stored | |
MBDS="$DEST/mysql" | |
# Linux bin paths, change this if it can not be autodetected via which command | |
DUMP="$(which mysqldump)" | |
GZIP="$(which gzip)" | |
# Get data in yyyy-mm-dd format | |
DATE="$(date +"%Y%m%d%H%M")" | |
[ ! -d $MBDS ] && mkdir -p $MBDS || : | |
# Only root can access it! | |
chown 0.0 -R $DEST | |
chmod 0600 $DEST | |
for db in $1 | |
do | |
FILE="$MBDS/$db.$DATE.sql.gz" | |
# connect to mysql using mysqldump for select mysql database | |
# and pipe it out to gz file in backup dir :-) | |
$DUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment