#!/bin/bash
###################################
# author mail@zhaoyanan.cn
# function linux下单机的应用数据备份通用脚本,在此基础上可以变更为适合每台系统的备份脚本,但数据库逻辑备份需要做较大变更,另有脚本实现
#
# create: touch /root/sh/backup.sh; chmod 700 /root/sh/backup.sh
# vi /etc/crontab
# 30 4 * * * root /root/sh/backup.sh >> /root/sh/backup.log 2>&1
# Change History:
# date author note
# 2012/09/24 mail@zhaoyanan.cn create
# 2012/10/23 mail@zhaoyanan.cn 增加FTP上传功能
# 2013/09/23 mail@zhaoyanan.cn 增加功能
# 2015/04/22 mail@zhaoyanan.cn 变更上传方式,弥补FTP上传的小概率问题
###################################
####################### set env #########################################
export projectname="kaifu_large"
export backupdir=/opt/data_bak/app_bak
export tmpdir=/root/sh # /tmp danger
export days=20
export ftpsw="yes" # To turn on or off the FTP upload
export ftpip=backup.yhox.com
export ftpport=21
export ftpuser='backup'
export ftppw='pass'
export ftpdir=soonyo/123.123.123.123
####################### ENV #############################################
export LANG=C
export LC_ALL=C
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export datetime=$(date +%Y%m%d-%H%M)
echo ##############################################################
date
cd /tmp
cd $backupdir
find -maxdepth 1 -name "*.tar.gz" -mtime +$days -exec rm -f {} \;
find -maxdepth 1 -name "web_frontend_*.tgz" -mtime +5 -exec rm -f {} \;
cat < ${tmpdir}/backup_tmp_${projectname}.sh
tar czpf script_${datetime}.tar.gz /root/sh
tar czpf etc_${datetime}.tar.gz /etc
tar czpf tengine_conf_${datetime}.tar.gz /opt/tengine/conf
tar czpf php_conf_${datetime}.tar.gz /opt/php/etc/{php.ini,php-fpm.conf}
tar czpf redis_conf_${datetime}.tar.gz /opt/redis/redis.conf
sync;sleep 2s;sync
tar czpf web_${datetime}.tar.gz /opt/web --exclude=/opt/web/9z_front_end
tar czpf web_frontend_${datetime}.tgz /opt/web/9z_front_end
EOF
/bin/bash ${tmpdir}/backup_tmp_${projectname}.sh
egrep -v "^#|sleep" ${tmpdir}/backup_tmp_${projectname}.sh | grep "^tar\ " | awk '{ print $3; }' > ${tmpdir}/backup_tmp_${projectname}.txt
####### FTP backup ####################################
if [ "$ftpsw" == 'yes' ]; then
for filename in `cat ${tmpdir}/backup_tmp_${projectname}.txt`
do
ftp -v -n -i <
open $ftpip $ftpport
user $ftpuser $ftppw
bin
cd $ftpdir
put ${filename}
close
bye
END
done
fi