Windows 下负责触发备份的 index.bat @echo off & setlocal ENABLEEXTENSIONS
:: 这里只是 SSH 连接和备份路径配置信息 :: 请同时修改同目录下 linux 文件中要备份的路径和 MySQL 数据库
:: SSH 相关信息 set SERVER=192.168.1.100 set PORT=22 set USER=root set PASSWORD=password
:: 和同目录下 linux 文件中配置的路径对应 set BACKUP_PATH=/root/backup
:: 备份要下载到的本机位置(会自动创建) set SAVE_PATH=D:/Backup/
:: ---------- 配置结束,以下请勿修改 ----------
:: 运行时变量 set EXE_SSH=%~dp0/exe/plink.exe set EXE_SCP=%~dp0/exe/pscp.exe set LINUX=%~dp0/linux
set YEAR=%date:~0,4% set MONTH=%date:~5,2% set DAY=%date:~8,2% set TRUE_PATH=%SAVE_PATH%%YEAR%-%MONTH%-%DAY%/
:: 创建备份路径 echo Creating Backup Dir... if not exist %TRUE_PATH% ( mkdir %TRUE_PATH% 2>nul ) if not exist %TRUE_PATH% ( echo Backup path: %TRUE_PATH% not exists, create dir failed. goto exit ) echo Creating Backup Dir... Done.
:: 生成备份目录和文件 echo Connecting to remote server and creating backups... %EXE_SSH% -C %USER%@%SERVER% -P %PORT% -pw %PASSWORD% -m %LINUX% 2>nul echo Connecting to remote server and creating backups... Done.
# 运行时变量 DATETIME=$(date -d now +%Y-%m-%d) CURPATH=$(cd "$(dirname "$0")"; pwd)
# 创建备份目录 if [ ! -d $BACKUP_PATH ]; then mkdir -p $BACKUP_PATH fi
rm -fR $BACKUP_PATH/*
# 备份 MySQL 数据库 cd $BACKUP_PATH for database in $DATABASES do if [ "$PASSWD" == "" ]; then $MYSQLDUMP -u$USER $database > $database.$DATETIME.dump.sql else $MYSQLDUMP -u$USER -p$PASSWD $database > $database.$DATETIME.dump.sql fi tar czf $database.$DATETIME.dump.sql.tar.gz $database.$DATETIME.dump.sql rm -f $database.$DATETIME.dump.sql done
# 备份文件或目录 for file in $FILES do tar czf $file.$DATETIME.tar.gz $file mv $file.$DATETIME.tar.gz $BACKUP_PATH done