适用于非数据库等静态文件,首先创建脚本/root/script/backup.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#!/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 <<EOF > ${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 <<END open $ftpip $ftpport user $ftpuser $ftppw bin cd $ftpdir put ${filename} close bye END done fi |
附件中的脚本文件是有DOS换行符(CR/LF)的,复制文件中的内容到SSH客户端没有问题,如果直接拷贝文件到Linux系统中,可先运行下面的命令:
mv *.txt *sh
dos2unix *.sh
发表回复