首页 > 代码库 > shell备份脚本
shell备份脚本
1 #!/bin/bash 2 export PATH=$PATH:$JAVA_HOME/bin:/usr/local/lnmp/php5/bin:/usr/local/lnmp/php5/sbin 3 4 #start------------------------------------------------------------------------------------------------------------------------------------------------------ 5 6 #定义0.程序rsync目录,1.程序rsync网站目录,2.备份目录,3.日志目录,4程序目录,5.数据库备份目录,6.oss名称,7.项目名称 7 #注意1和4一定不能写反了 8 backup_dir=(‘/backup/data/‘ ‘/backup/data/www_root/‘ ‘/backup/old/‘ ‘/backup/logs/‘ ‘/www_root/‘ ‘/backup/mysql/‘ ‘www186‘ ‘www_root‘) 9 10 tomcat_dir=‘/zenghui/tomcat/apache-tomcat-7.0.53/logs/‘ 11 tomcat_dir2=‘/backup/tomcat_logs/‘ 12 13 #定义0.数据库登陆用户名,1.数据库登陆密码,2.数据库名,3.数据库ip地址 14 mysql_message=(‘zenghui‘ ‘zenghui‘ ‘zenghui‘ ‘10.109.1.196‘) 15 16 #定义脚本所在目录 17 scripts_dir=`dirname $0` 18 19 #定义时间 20 month=`date +%Y%m` 21 hours=`date +%Y-%m-%d-%H` 22 hours2=`date -d"6 hour ago" "+%Y-%m-%d-%H"` 23 time=`date +%Y-%m-%d` 24 yestd=`date -d"4 day ago" "+%Y-%m-%d"` 25 yestd2=`date -d"1 day ago" "+%Y-%m-%d"` 26 ht=`date +%H` 27 dy=`date +%d` 28 29 30 #获取backup_dir长度 31 len=$(( ${#backup_dir[@]}-1 )) 32 33 #定义oss目录 34 month_dir=("${backup_dir[$len]}${month}" "mysql${month}" "log${month}") 35 36 #end------------------------------------------------------------------------------------------------------------------------------------------------------ 37 38 #共享函数 39 function share_comm() 40 { 41 eval "$1" 42 echo $1" ...is ok" 43 } 44 45 #定义目录存不存在,不存在创建它 46 function rsync_dir() 47 { 48 len2=$(( $len-1 )) 49 for i in ${backup_dir[@]:0:${len2}} 50 do 51 if [ ! -d $i ];then 52 share_comm "mkdir -p $i" 53 fi 54 done 55 } 56 57 #定义rsync同步更新程序 58 function rsync_upload() 59 { 60 rsync_file="rsync --delete -az ${backup_dir[4]} ${backup_dir[1]}" 61 share_comm "$rsync_file" 62 63 } 64 65 66 #对rsync同步好的文件进行打包备份 67 function backup_targz() 68 { 69 file_tar="tar zcfP ${backup_dir[2]}${backup_dir[${len}]}${time}.tar.gz ${backup_dir[1]} >/dev/null" 70 yestd_rm_file="rm -rf ${backup_dir[2]}${backup_dir[${len}]}${yestd2}.tar.gz" 71 share_comm "$file_tar" 72 share_comm "$yestd_rm_file" 73 } 74 75 76 #数据库操作 77 function mysql_export() 78 { 79 mysql_dump="mysqldump -u${mysql_message[0]} -p${mysql_message[1]} -h${mysql_message[3]} ${mysql_message[2]} > ${backup_dir[5]}${mysql_message[2]}${hours}.sql" 80 share_comm "$mysql_dump" 81 mysql_rm 82 } 83 function mysql_rm() 84 { 85 sql_hours="rm -rf ${backup_dir[5]}${mysql_message[2]}${hours2}.sql" 86 share_comm "$sql_hours" 87 } 88 function mysql_targz() 89 { 90 sql_tar="tar zcfP ${backup_dir[2]}mysql${time}.tar.gz ${backup_dir[5]}" 91 sql_rm_tar="rm -rf ${backup_dir[2]}mysql${yestd}.tar.gz" 92 sql_rm="rm -rf ${backup_dir[5]}*" 93 share_comm "$sql_tar" 94 share_comm "$sql_rm_tar" 95 share_comm "$sql_rm" 96 } 97 98 99 #对日志文件进行操作100 function log_targz()101 {102 log_tar="tar zcfP ${backup_dir[2]}log${time}.tar.gz ${backup_dir[3]}"103 log_rm_tar="rm -rf ${backup_dir[2]}log${yestd}.tar.gz"104 log_rm="rm -rf ${backup_dir[3]}*"105 share_comm "$log_tar"106 share_comm "$log_rm_tar"107 share_comm "$log_rm"108 nginx_reload109 }110 function nginx_reload()111 {112 nginx_re="`ps -ef | grep "nginx: master" | grep -v grep | awk ‘{print $NF}‘` -s reload"113 share_comm "$nginx_re"114 }115 116 117 function tomcat_log()118 {119 if [ -d "$tomcat_dir" ] && [ -d "$tomcat_dir2"]120 then121 tomcat_rm="find ${tomcat_dir} -ctime +10 -type f -name ‘"*"‘ | xargs -exec rm -rf {};"122 tomcat_rm2="find ${tomcat_dir2} -ctime +5 -type f -name ‘"*"‘ | xargs -exec rm -rf {};"123 share_comm "$tomcat_rm"124 share_comm "$tomcat_rm2"125 fi126 }127 128 129 #定义上传130 function file_upload()131 {132 #判断oss目录存不存在133 mn_mess="`python ${scripts_dir}/oss/osscmd ls oss://wdzjoss/${backup_dir[6]}/`"134 for i in ${month_dir[*]}135 do136 if ! echo "$mn_mess" | grep "$i" >/dev/null137 then138 py_mkdir="python ${scripts_dir}/oss/osscmd mkdir oss://wdzjoss/${backup_dir[6]}/${i} 2>/dev/null"139 share_comm "$py_mkdir"140 fi141 done142 bc_oss="python ${scripts_dir}/oss/osscmd put ${backup_dir[2]}${backup_dir[${len}]}${time}.tar.gz oss://wdzjoss/${backup_dir[6]}/${month_dir[0]}/ >/dev/null"143 ms_oss="python ${scripts_dir}/oss/osscmd put ${backup_dir[2]}mysql${time}.tar.gz oss://wdzjoss/${backup_dir[6]}/${month_dir[1]}/ > /dev/null"144 log_oss="python ${scripts_dir}/oss/osscmd put ${backup_dir[2]}log${time}.tar.gz oss://wdzjoss/${backup_dir[6]}/${month_dir[2]}/ > /dev/null"145 share_comm "$bc_oss"146 share_comm "$ms_oss"147 share_comm "$log_oss"148 }149 150 echo "开始时间:"`date +%H:%M:%S`151 echo "-------------------"152 153 rsync_dir154 mysql_export155 tomcat_log156 rsync_upload157 #定义凌晨2点更新打包文件158 if [ "$ht" == "02" ];then159 backup_targz160 mysql_targz161 log_targz162 fi163 164 #定义每2天上传一次oss165 if [ "$ht" == "04" ] && [ `echo "scale=1;${dy}%3"|bc` == "0" ]166 then167 file_upload168 fi169 echo "结束时间:"`date +%H:%M:%S`170 echo "-------------------"171 echo ""
shell备份脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。