1.推送
scp -r imageAPP/ root@ip:/data/soft/
本地当前文件下的 imageAPP文件 推送到ip服务器 /data/soft/ 目录下
2.拉取
scp -r root@ip:/data/mysqlbackup /home/backup
拉取ip服务器 /data/mysqlbackup 文件到本地 /home/backup 目录下
自动拉取
因为拉取需要密码,所有手动执行时可以去输入密码,但如果是自动拉取,则需要借助expect
安装 expect
yum -y install expect
具体使用查看 https://blog.csdn.net/qq_41944882/article/details/105956317
vi test.sh
#!/usr/bin/expect
#set password "**********"
spawn scp -r root@ip:/data/mysqlbackup /home/backup
expect {
"密码:"
{
send "$password "
}
"password"
{
send "$password "
}
"yes/no"
{
sleep 5
send_user "send yes"
send "yes "
}
eof
{
sleep 5
send_user "eof "
}
}
set timeout 3000
send "exit "
expect eof
但是只能直接set具体的值,如果想获取当前时间之类的则需要使用下面的方式
vi start.sh
#!/bin/sh
DATE=`date +%Y%m%d`
file=****_${DATE}*.sql.gz
password=******
#传递参数
./hanby $password $file
echo "pull data end"
vi hanby
#!/usr/bin/expect
#按顺序接受传递过来的参数
set password [lindex $argv 0]
set file [lindex $argv 1]
#全部拉取
#spawn scp -r root@47.94.203.241:/data/mysqlbackup/${BACK_DATA}.sql.gz /home/backup/mysqlbackup
#拉取指定的文件
spawn scp -r root@47.94.203.241:/data/mysqlbackup/$file /home/backup/mysqlbackup
expect {
"密码:"
{
send "$password "
}
"password"
{
send "$password "
}
"yes/no"
{
sleep 5
send_user "send yes"
send "yes "
}
eof
{
sleep 5
send_user "eof "
}
}
set timeout 3000
send "exit "
expect eof
注意:2个文件放在同一目录