21运维原创,转载请注明出处。
线上的配置文件以及应用配置文件是非常有必要进行备份的,不排除能够上服务器的朋友没有备份就直接修改出错或者其他人员误操作的情况。现在花了十分钟简单写了一个备份脚本,另外也可以自己加上其他备份脚本进去。
虽简单,但有了这个东西还是放心了不少,再也不用担心原始配置文件找不到了。
#!/bin/bash
# bakup configfile for system 、nginx 、 php 、ELK and so on
#author:21yunwei
#date:2017.4.22
time=`date +%Y%m%d`
LOGFILE=/backup/configbak/configbak.log
[ ! -e /backup/configbak/elkbak/$time ] && mkdir -p /backup/configbak/elkbak/$time
[ ! -e /backup/configbak/nginxbak/$time ] && mkdir -p /backup/configbak/nginxbak/$time
[ ! -e /backup/configbak/phpbak/$time ] && mkdir -p /backup/configbak/phpbak/$time
[ ! -e /backup/configbak/etcbak/$time ] && mkdir -p /backup/configbak/etcbak/$time
function etcbak() {
tar zcf /backup/configbak/etcbak/$time/etc$time.tar.gz /etc/*
}
function nginxbak() {
cp -rfpu /usr/local/nginx/conf/* /backup/configbak/nginxbak/$time/
}
function phpbak() {
cp -rfpu /usr/local/php/etc/* /backup/configbak/phpbak/$time/
}
function elkbak() {
cd /backup/configbak/elkbak/$time && mkdir es logstash kibana
cp -rfpu /usr/local/ELK/elasticsearch-5.1.1/config/* /backup/configbak/elkbak/$time/es/
cp -rfpu /usr/local/ELK/logstash-5.1.1/config/* /backup/configbak/elkbak/$time/logstash/
cp -rfpu /usr/local/ELK/kibana-5.1.1/config/* /backup/configbak/elkbak/$time/kibana/
}
#start bak working:
etcbak
nginxbak
phpbak
elkbak
#bakwork end
ret=$?
FINISHTIME=`date +%Y%m%d_%H%M%S`
[ $ret -eq 0 ] && echo "configbak finished, time: $FINISHTIME">> $LOGFILE || echo "configbak failed,please check time:$FINISHTIME" >> $LOGFILE
find /backup/configbak/ -type d -mtime +15 |xargs rm -rf