将服务器的数据定时压缩并scp到跳板机
2021/4/12 10:25:10
本文主要是介绍将服务器的数据定时压缩并scp到跳板机,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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脚本,有点懵,感觉也写得很简易,可能会冗余,不过这可能需要我学习更多以后才能知道自己的问题所在了。(但是后来也没有再写过了
这篇关于将服务器的数据定时压缩并scp到跳板机的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-25Elevate Your Lead Generation Game with Maps Scraper AI
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能