Created
November 27, 2018 20:44
-
-
Save ReS4/e8666183b9aa1064b6a0135e1e920007 to your computer and use it in GitHub Desktop.
MySQL database backup and transfer to another server over ssh
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 | |
apt-get install sshpass -y | |
# or `yum install sshpass` for centos | |
DATE=`date +%Y-%m-%d` | |
DB_USER="user" | |
DB_PASS="pass" | |
DB_NAME="db_name" | |
BACKUP_DIR="/var/www/db_backups/" | |
REMOTE_SERVER="x.x.x.x" | |
REMOTE_USER="root" | |
REMOTE_PASS="pass" | |
REMOTE_DIR="/var/www/backup_server/backup_dir/" | |
mysqldump -u $DB_USER --password=$DB_PASS $DB_NAME > $BACKUP_DIR$DATE.sql | |
sshpass -p $REMOTE_PASS rsync -avzhe ssh $BACKUP_DIR$DATE.sql $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR | |
#we considered you didn't put your public_key into server .ssh/authorized_keys file, otherwise just use: | |
#rsync -avzhe ssh $BACKUP_DIR$DATE.sql $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR | |
rm $BACKUP_DIR$DATE.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment