Last active
September 21, 2017 12:35
-
-
Save V1ncNet/07a1df4e143fd1a0721cd229cb5addee to your computer and use it in GitHub Desktop.
Dump MySQL databases remotely and copy to backup server
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/sh | |
### SSH Info ### | |
RHOST="sub.domain.tld" # Remote Host | |
RUSER="username" # Remote User | |
RDIR="/home/username/backup" # Remote Backup Directory | |
### System Setup ### | |
DATE=`date +%Y-%m-%d` # Current Date (2017-10-01) | |
KEEPDAYS=14 # Days to keep | |
LDIR="/home/localusername/backups/$RHOST" # Local Backup Directory | |
### MySQL Setup ### | |
MUSER="db-user" # MySQL User | |
MPASS="db-pass" # MySQL Password | |
MHOST="localhost" # XXXX is mt gs number # MySQL Host | |
DBS="db-1 db-2 db-3" # Databases | |
mkdir -p $LDIR | |
### Start MySQL Backup ### | |
for db in $DBS # For each Database | |
do | |
echo ":: Dumping $db on Remote Server" | |
FILE=$RDIR/$DATE.mysql-$db.sql.gz # Backup Filename | |
ssh $RUSER@$RHOST "mysqldump -q -u $MUSER -h $MHOST -p$MPASS $db | gzip -9 > $FILE" | |
# Dump the MySQL and gzip it up | |
done | |
echo ":: Copying all files to Backup Server" | |
scp $RUSER@$RHOST:$RDIR/$DATE.mysql-*.sql.gz $LDIR | |
# Copy all the Files to Backup server | |
echo ":: Removing old Files on Remote and Backup Server" | |
ssh $RUSER@$RHOST "find $RDIR/*.mysql-*.sql.gz -type f -daystart -mtime +$KEEPDAYS -exec rm {} \;" | |
# Delete Files on Remote Server | |
find $LDIR/*.mysql-*.sql.gz -type f -daystart -mtime +$KEEPDAYS -exec ls {} \; | |
find $LDIR/*.mysql-*.sql.gz -type f -daystart -mtime +$KEEPDAYS -exec rm {} \; | |
# Delete old Files on Backup Server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment