shell脚本将服务器的数据定时压缩并scp到跳板机(无需输入密码)
这已经是去年还是前年的工作了,一直写了整理文档没有发,今天有空发一下啦。
运维有个新需求,是要将一个服务器的数据,传到另一台服务器,但中间还需要登录跳板机。因为这样传过于麻烦,所以我们最终商量决定在跳板机上加一个10T的硬盘用于存储数据,这样就减少了第二步的传输。所以我开始看linux命令的书。由于我是大白菜,所以我用了很久才写出来55555
首先这个脚本分成三部分,1:压缩;2:传数据;3:自动定时运行脚本。数据一共有三部分,将自动生成在当天日期的文件夹下,但是capture_image下文件夹是2019-12-01类型,而facedetectionrangetest下文件夹是2019-12-1类型,所以需要去分开判断压缩。
压缩代码比较简单:
#!/bin/bash #取前一天年月日 y=`date --date='1 days ago' "+%Y"` m=`date --date='1 days ago' "+%m"` d=`date --date='1 days ago' "+%d"` #取前一天年月日去0 day=`expr $d + 0` mon=`expr $m + 0` echo "$y-$mon-$day" filename1="$y-$m-$d.zip" ###命名方式1 filename2="$y-$mom-$day.zip" ###命名方式2 cd /home/www-root/SeetaAiBuildingCommunity/files/capture_image/ zip -r $filename1 "$y-$m-$d" cd /opt/facedetectionrangetest/image_19 zip -r $filename2 "$y-$mon-$day" cd /opt/facedetectionrangetest/image_21 zip -r $filename2 "$y-$mon-$day"
scp需要安装一个expect交互库将密码自动回复:
#!/usr/bin/expect set timeout 1 spawn scp /media/seeta/2230D49516EE741B/test/2019-12-16.zip xxx@xxx:/home/seetatech/Documents/ expect "*password*" send "161202\r" interact
然后将两个脚本结合的时候是把expect脚本嵌套进sh脚本:
下面是测试传输一个文件用的代码:
#!/bin/bash passwd='161202' datenum=`date --date='1 days ago' "+%Y-%m-%d"` cd /media/seeta/2230D49516EE741B/test filename="$datenum.zip" zip -r $filename "$datenum" /usr/bin/expect <<-EOF set timeout 10 spawn scp /media/seeta/2230D49516EE741B/test/$filename xxx@xxx:/home/seetatech/Documents/ expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } expect eof EOF
将脚本输出写入日志:
./script.sh >logfile 2>&1(不覆盖)
./script.sh >>logfile 2>&1(覆盖)
这里建议使用上一个,因为一直覆盖占用内存较大
然后想要定时执行任务是使用crontab
1.判断是否有crontab
which crontab
2.赋予shell脚本权限
chmod +x test4.sh
3.定时任务开启service crond restart/service cron restart
crontab -l 查询定时任务
crontab -e 添加定时任务
输入以下内容:
00 13 * * * /media/seeta/2230D49516EE741B/test/test4.sh /media/seeta/2230D49516EE741B/test/log 2>&1
【每天下午1点定时执行test4.sh且输出日志】
最后运行完成后将日志作为附件发送邮件给自己的邮箱,这里建议使用QQ邮箱,比较简单,然后把自己电脑的邮箱地址改成白名单才可以收到邮件。
yum -y install sendmail #安装sendmail
mail -s “test” xxx@qq.com < /home/seeta/zmp_script/log
形成最终脚本:
#!/bin/bash passwd='161202' #取前一天年月日 y=`date --date='1 days ago' "+%Y"` m=`date --date='1 days ago' "+%m"` d=`date --date='1 days ago' "+%d"` #取前一天年月日去0 day=`expr $d + 0` mon=`expr $m + 0` echo "$y-$mon-$day" filename1="$y-$m-$d.zip" filename2="$y-$mon-$day.zip" cd /home/www-root/SeetaAiBuildingCommunity/files/capture_image/ rm -f *.zip zip -r $filename1 "$y-$m-$d" /usr/bin/expect <<-EOF set timeout 300 spawn scp /home/www-root/SeetaAiBuildingCommunity/files/capture_image/$filename1 xxx@xxx:/data/ninghai_school/capture_image expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } set timeout 1000 expect eof EOF cd /opt/facedetectionrangetest/image_19 rm -f *.zip zip -r $filename2 "$y-$mon-$day" /usr/bin/expect <<-EOF set timeout 300 spawn scp /opt/facedetectionrangetest/image_19/$filename2 xxx@xxx:/data/ninghai_school/facedetectionrangetest/image_19 expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } set timeout 1000 expect eof EOF cd /opt/facedetectionrangetest/image_22 rm -f *.zip zip -r $filename2 "$y-$mon-$day" /usr/bin/expect <<-EOF set timeout 300 spawn scp /opt/facedetectionrangetest/image_22/$filename2 xxx@xxx:/data/ninghai_school/facedetectionrangetest/image_22 expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } set timeout 1000 expect eof EOF cd /opt/facedetectionrangetest/Recoed_19 /usr/bin/expect <<-EOF set timeout 300 spawn scp /opt/facedetectionrangetest/Recoed_19/test_19.csv xxx@xxx:/data/ninghai_school/facedetectionrangetest expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } set timeout 1000 expect eof EOF cd /opt/facedetectionrangetest/Recoed_22 /usr/bin/expect <<-EOF set timeout 300 spawn scp /opt/facedetectionrangetest/Recoed_22/test_22.csv xxx@xxx:/data/ninghai_school/facedetectionrangetest expect { "*yes/no*" { send "yes\r"; exp_continue } "*password*:" { send "$passwd\r" } } set timeout 1000 expect eof EOF
第一次写shell脚本,有点懵,感觉也写得很简易,可能会冗余,不过这可能需要我学习更多以后才能知道自己的问题所在了。(但是后来也没有再写过了