Skip to content

Instantly share code, notes, and snippets.

@mindevolution
Last active June 22, 2017 08:28
Show Gist options
  • Save mindevolution/84773e0c7228938c6d56c50e77441e7f to your computer and use it in GitHub Desktop.
Save mindevolution/84773e0c7228938c6d56c50e77441e7f to your computer and use it in GitHub Desktop.
Ssh export database to local machine
#!/bin/sh
### System Setup ###
NOW=`date +%Y-%m-%d`
KEEPDAYS=5
#
### SSH Info ###
SHOST="" # XXXX is mt gs number
SUSER="" # mt_domain is primary mt domain
SDIR="/home/hezhl/data/tmp" # Remote temp save path
PORT=22
#
### MySQL Setup ###
MUSER="root" # XXXX is mt gs number
MPASS=""
MHOST="127.0.0.1" # XXXX is mt gs number
DBS="sq_51tongda" # space separated list of databases to backup
#
#
#
### Local info ###
#
LOCALDIR=/var/www/client/tongda/docs/data/db # local path to save data
### Start MySQL Backup ###
attempts=0
for db in $DBS # for each listed database
do
attempts=`expr $attempts + 1` # count the backup attempts
FILE=$SDIR/mysql-$db.$NOW.sql.gz # Set the backup filename
# Dump the MySQL and gzip it up
ssh -p $PORT $SUSER@$SHOST "mysqldump -q -u $MUSER -h $MHOST -p$MPASS $db | gzip -9 > $FILE"
LOCAL_FILE=$LOCALDIR/mysql-$db.$NOW.sql.gz
scp -P $PORT $SUSER@$SHOST:$FILE $LOCAL_FILE # copy all the files to backup server
done
ssh -p $PORT $SUSER@$SHOST rm ./mysql* # delete files on db server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment