scp 实现远程异地备份

1.先做好脚本

2.再使用crontab 定时执行计划任务

0 1 * * * /usr/bin/expect /root/script/scpautodown.sh  #执行,也可先定义环境变量

0 5 * * * /root/script/del10day.sh  #删除10前备份

scpautodown.sh

#以下方向为本地备份服务器

--------------------------------------从远程数据服务器下载至本地备份服务器----------------------

脚本范例如下:

#!/usr/bin/expect -f
set password 123456 #设置远程linux  登陆密码

#download

spawn scp root@192.168.1.2:/root/home/*.tar.gz /root/backpath/
set timeout 300
expect "root@192.168.1.2's password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof

#循环

spawn scp root@192.168.1.2:/root/home/*.tar.gz  /root/backpath/
for { set i 1 }
{$i<500}
{incr i}
{ expect
{"*password:" {send "$password\r"}
"*(yes/no)*" {send "yes\r"}
}
}

 

-----------------------------------------------------------------

上传是反向,范例自行调整!从本地服务器--->数据服务器

-----------------------------------------------------------------

#upload
#spawn scp /home/yangyz/abc.sql root@192.168.1.218:/root/test.sql
#set timeout 300
#expect "root@192.168.1.2's password:"
#set timeout 300
#send "$password\r"
#set timeout 300
#send "exit\r"
#expect eof

posted @ 2018-09-07 11:15  方面盘  阅读(1406)  评论(1编辑  收藏  举报