使用 shell 脚本备份 Mysql 数据库 - 绑定地址设置为 localhost
Backup Mysql database with shell script - bind-address set to localhost
我正在使用 shell 脚本将我的数据库从远程服务器备份到我的 Synology NAS。该脚本工作正常,但是当我将 de mysql config @ 远程服务器中的绑定地址设置为 'localhost' 时,该脚本不再工作。该脚本需要 SSH 验证。
问题:如何在此脚本中创建 SSH 连接/凭据?
下面我使用的脚本:
#! /bin/bash
TIMESTAMP=$(date +"%Y-%m-%d")
PAST=$(date +"%Y-%m-%d" -d "30 days ago")
BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
PAST_DIR="/volume1/dir/di2/$PAST"
HOST="myip"
MYSQL_USER="username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
rm -rv $PAST_DIR
mkdir -p $BACKUP_DIR
databases=`$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db.sql.gz"
done
您需要通过ssh
发送命令。脚本的修改版本应该可以完成任务。
#! /bin/bash
TIMESTAMP=$(date +"%Y-%m-%d")
PAST=$(date +"%Y-%m-%d" -d "30 days ago")
BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
PAST_DIR="/volume1/dir/di2/$PAST"
HOST="myip"
MYSQL_USER="username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
SSH_KEY=""
SSH_USER=""
SSH_HOST=""
REMOTE_COMMAND="$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -s -e 'SHOW DATABASES' | grep -v 'information_schema'"
databases=$(ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND")
created=0
for db in $databases; do
DUMP="$BACKUP_DIR/$db.sql"
# To save backup on remote machine
case $created in
0) REMOTE_COMMAND="rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR"; created=1
ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" ;;
*) ;;
esac
REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip -9 > $DUMP.gz"
ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND"
# To save backup on local machine
# case $created in
# 0) rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR; created=1 ;;
# *) ;;
# esac
# REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db"
# ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" | gzip -9 > $DUMP.gz
done
注意:您需要设置 SSH_KEY
(如果您有基于 ssh 密钥的身份验证),SSH_USER
, SSH_HOST
个变量。我还提供了两种解决方案,以防您想将备份保存在您启动命令的本地计算机上。还要注意带有 mysql
命令(SHOW DATABASES
部分)的 -s
标志,它不会打印出列名(这里是 Database
)。
这是我的脚本:
#!/bin/bash
## Designed to run in Synology DSM > Control Panel > Task Scheduler > User-defined script
TODAY=$(date +"%Y-%m-%d")
TIME=$(date +"%T")
################################################################
################## Update below values ########################
DB_BACKUP_PATH='/volume2/Backups/Alpine1MySQL/proddata'
MYSQL_HOST='servername.domain.co.nz'
MYSQL_PORT='3306'
MYSQL_USER='backup'
MYSQL_PASSWORD='backuppassword'
DATABASE_NAME='dbname'
BACKUP_RETAIN_DAYS=90 ## Number of days to keep local backup copy
MYSQLDUMP=/volume2/@appstore/MariaDB10/usr/local/mariadb10/bin/mysqldump ## Get this file by installing the MariaDB package in Synology package manager (it doesn't need to actually run)
#################################################################
mkdir -p ${DB_BACKUP_PATH}/${TODAY} | echo ${DB_BACKUP_PATH}/${TODAY} "directory created"
echo "Backup started for database: ${DATABASE_NAME}"
$MYSQLDUMP -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | zip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}_${TODAY}_${TIME}.sql.zip
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
echo "Removing backups older than" ${BACKUP_RETAIN_DAYS} "days "
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
### End of script ####
我正在使用 shell 脚本将我的数据库从远程服务器备份到我的 Synology NAS。该脚本工作正常,但是当我将 de mysql config @ 远程服务器中的绑定地址设置为 'localhost' 时,该脚本不再工作。该脚本需要 SSH 验证。
问题:如何在此脚本中创建 SSH 连接/凭据?
下面我使用的脚本:
#! /bin/bash
TIMESTAMP=$(date +"%Y-%m-%d")
PAST=$(date +"%Y-%m-%d" -d "30 days ago")
BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
PAST_DIR="/volume1/dir/di2/$PAST"
HOST="myip"
MYSQL_USER="username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
rm -rv $PAST_DIR
mkdir -p $BACKUP_DIR
databases=`$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db.sql.gz"
done
您需要通过ssh
发送命令。脚本的修改版本应该可以完成任务。
#! /bin/bash
TIMESTAMP=$(date +"%Y-%m-%d")
PAST=$(date +"%Y-%m-%d" -d "30 days ago")
BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
PAST_DIR="/volume1/dir/di2/$PAST"
HOST="myip"
MYSQL_USER="username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
SSH_KEY=""
SSH_USER=""
SSH_HOST=""
REMOTE_COMMAND="$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -s -e 'SHOW DATABASES' | grep -v 'information_schema'"
databases=$(ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND")
created=0
for db in $databases; do
DUMP="$BACKUP_DIR/$db.sql"
# To save backup on remote machine
case $created in
0) REMOTE_COMMAND="rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR"; created=1
ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" ;;
*) ;;
esac
REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip -9 > $DUMP.gz"
ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND"
# To save backup on local machine
# case $created in
# 0) rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR; created=1 ;;
# *) ;;
# esac
# REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db"
# ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" | gzip -9 > $DUMP.gz
done
注意:您需要设置 SSH_KEY
(如果您有基于 ssh 密钥的身份验证),SSH_USER
, SSH_HOST
个变量。我还提供了两种解决方案,以防您想将备份保存在您启动命令的本地计算机上。还要注意带有 mysql
命令(SHOW DATABASES
部分)的 -s
标志,它不会打印出列名(这里是 Database
)。
这是我的脚本:
#!/bin/bash
## Designed to run in Synology DSM > Control Panel > Task Scheduler > User-defined script
TODAY=$(date +"%Y-%m-%d")
TIME=$(date +"%T")
################################################################
################## Update below values ########################
DB_BACKUP_PATH='/volume2/Backups/Alpine1MySQL/proddata'
MYSQL_HOST='servername.domain.co.nz'
MYSQL_PORT='3306'
MYSQL_USER='backup'
MYSQL_PASSWORD='backuppassword'
DATABASE_NAME='dbname'
BACKUP_RETAIN_DAYS=90 ## Number of days to keep local backup copy
MYSQLDUMP=/volume2/@appstore/MariaDB10/usr/local/mariadb10/bin/mysqldump ## Get this file by installing the MariaDB package in Synology package manager (it doesn't need to actually run)
#################################################################
mkdir -p ${DB_BACKUP_PATH}/${TODAY} | echo ${DB_BACKUP_PATH}/${TODAY} "directory created"
echo "Backup started for database: ${DATABASE_NAME}"
$MYSQLDUMP -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | zip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}_${TODAY}_${TIME}.sql.zip
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
echo "Removing backups older than" ${BACKUP_RETAIN_DAYS} "days "
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
### End of script ####