阿里云日志文件备份

  • 密钥访问
1
2
3
* 准备备份日志的服务器
ssh-keygen -t rsa -P ''
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.250
  • 脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vim /root/sh
#!/bin/bash
#
nettylog=/apps/echarging/log/nettylog
processorlog=/apps/echarging/log/processor_1_log
nettyback=/home/nettylog
processorback=/home/processorlog1
nettybackR=/home/logbackup/nettylog
processorbackR=/home/logbackup/processorlog1

cd $nettylog
find ./ -name netty.log-`date -d yesterday +%F`."*".log -exec cp {} $nettyback \;
tar -zcf $nettyback/netty-log.`date -d yesterday +%F`.tar.gz $nettyback/*.log
scp $nettyback/netty-log.`date -d yesterday +%F`.tar.gz 192.168.2.250:$nettybackR
rm -rf $nettyback/*

cd $processorlog
find ./ -name processor_1.log-`date -d yesterday +%F`."*".log -exec cp {} $processorback \;
tar -zcf $processorback/processor_1.log-`date -d yesterday +%F`.tar.gz $processorback/*.log
scp $processorback/processor_1.log-`date -d yesterday +%F`.tar.gz 192.168.2.250:$processorbackR
rm -rf $processorback/*
#本打算使用find ./ -name processor_1.log-`date -d yesterday +%F`."*".log -exec tar -zcf processor_log-`date -d yesterday +%F`.tar.gz {} \;命令来直接打包,但测试发现打出的包中只有find查到的最后一个文件。所以将命令改为了两条,先将找到的文件移到一个临时目录,再打包发送。
  • 定时任务
1
2
3
crontab -e
* 4 * * * /bin/bash /root/sh/scplog.sh
crontab -l
作者

John Doe

发布于

2018-09-18

更新于

2023-03-17

许可协议