cat mysql-dump.sh
#!/usr/bin/expect -f # 变量定义 set HOST "192.168.26.42" set USERNAME ipms set PASSWORD ipms set DB ipms set DIR "/home/ipms/bs/bin" set FILE $DB.bak set DB_USER root set DB_PASS root # 传输文件,直到传输完成 spawn mysqldump -u$DB_USER -p$DB_PASS --databases $DB > $DIR/$FILE spawn scp -P 22 $DIR/$FILE $USERNAME@$HOST:$DIR/$FILE expect { "password" {set timeout -1; send "$PASSWORDn"; exp_continue;} "(yes/no)?" {send "yesn";} } # SSH登录 spawn ssh $USERNAME@$HOST expect { "password" {send "$PASSWORDn"} "(yes/no)?" {send "yesn";} } # 恢复数据库 expect "$USERNAME@*" {send "mysql -u$DB_USER -p$DB_PASS < $DIR/$FILEn"} # 退出SSH登录 expect "$USERNAME@*" {send "exitn"} expect eof
根据实际测试情况
- 如果远程已有该数据库,则会被覆盖。
- 如果远程无该数据库,则会被增加
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)