This is a regular directory backup file. the MD5 check is used to determine whether the Directory and file are modified today and yesterday. If not, delete the Backup Directory of yesterday. Reduce disk space consumption. Some backup directories may be very large. We generally refer to modifying one type of files. We can select the specified file type to back up the files that have been modified for a long time. This script can also make more changes, such as adding a function and using the function method to select whether to back up a specified type of files. Selecting backup is not calculated by day, but accurate to the hour. Of course, you must add crontab. Thank you, a blogger.AaaoProvided MD5 verification ideas.
#!/bin/bash#written by dqznn88#maill: [email protected]#date: 2014-09-23 #DSTDIR="/backup/tomcat_$(date +%Y%m%d)/"POSTFIX="xml"md5_sum_dir="/backup/tomcat/sum/"md5_in="${DIR}"md5_new_sum_file="${md5_sum_dir}tomcat_md5_sum.$(date +%Y%m%d)"md5_old_sum_file="${md5_sum_dir}tomcat_md5_sum_old"############################################################## backup directory# for DIRNAME in $(find $DIR -type d )# do# #echo "dirctory name is $DIRNAME"# FULLDIR=${DIRNAME/$DIR/$DSTDIR}# if [ ! -d $FULLDIR ];then# mkdir -p $FULLDIR# fi# done# echo "backup $DIR directory specified $POSTFIX"##################################################################if [ ! -d $DSTDIR ];thencp -a $DIR $DSTDIR && echo "$DIR directory to $DSTDIR backup successfully"################################################################if [ ! -d ${md5_sum_dir} ];then mkdir -p ${md5_sum_dir}fifind ${md5_in} | xargs -I {} md5sum {} >> $md5_new_sum_file 2>/dev/null && echo "directory md5 sum "new_sum_ok=`md5sum -c ${md5_new_sum_file} 2>/dev/null | awk ‘$2~"OK"‘ |wc -l`old_sum_ok=`md5sum -c ${md5_old_sum_file} 2>/dev/null | awk ‘$2~"OK"‘ |wc -l`if [ ${old_sum_ok} == ${new_sum_ok} ];then echo "because today and yesterday content is the same,delete yesterday centent!" cp $md5_new_sum_file $md5_old_sum_file -r rm -fr ${md5_in_yer} && rm -fr ${md5_new_sum_file} && echo "delete old file "fi###################################################################the first execute this script,create ‘md5_old_sum_file‘ file,afterwards,everytime change $DSTDIR directory backup ‘sum_file‘ file. cp $md5_new_sum_file $md5_old_sum_file -r &>/dev/null
The following are some variables
Directory where md5_sum_dir MD5 checksum is stored
Md5_new_sum_file: stores the MD5 value files of each backup file.
MD5 value File backed up before md5_old_sum_file
New_sum_ OK the MD5 file just generated to verify the backup target file. Several records are checked as OK.
The MD5 file generated by old_sum_ OK yesterday is used to verify the backup target file. At the same time, several records are checked as OK. If the target file has been changed, the test result is certainly not OK, in addition, the numbers of new and deleted files are different.
If [$ {old_sum_ OK }== {new_sum_ OK}], check whether the file is changed, and whether the file is added or deleted.
Explain why there are two
CP $ md5_new_sum_file $ md5_old_sum_file-r refresh the information of the md5_old_sum_file file every time the script is executed, so that the next verification must be the verification file of yesterday. In fact, if the above CP is executed, the following CP will not be executed.
If you have a better idea, please leave a message. Thank you !!
This article from the "ding zi" blog, please be sure to keep this source http://dqzshine.blog.51cto.com/8093967/1557277
Regular automatic file backup script