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
"
set timeout 300
send "exit
"
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
"}
"*(yes/no)*" {send "yes
"}
}
}
-----------------------------------------------------------------
上传是反向,范例自行调整!从本地服务器--->数据服务器
-----------------------------------------------------------------
#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
"
#set timeout 300
#send "exit
"
#expect eof