Today, a friend on the net asked me a shell topic, let me help him to do. Here are the topics and ideas for solving problems.
Topic:
Write a script that backs up the/etc directory, requiring that all files in the/etc directory be in a directory named after the day of the/var/backups directory, for example (/var/backups/etc.20140809). And to determine the day before the directory of the difference between the file and the current, if there is no difference, delete the previous previous backup directory.
[[Email protected] scripts]# cat auto_bak.sh #!/bin/bash## this script is used to automatically backup the specified directory, and judgment, and whether the previous backup # Written by Sunsky# mail : [email protected]# date : 2014-8-16 23:00:00#bak_from_ Name= "etc" bak_from_dir= "/etc" bak_to_dir_par= "/var/backups" bak_dir= "${bak_to_dir_par}${bak_from_dir}.$ (date +%y%m%d) "yes_bak_dir=" ${bak_to_dir_par}${bak_from_dir}.$ (date -d ' -1 day ' +%Y%m%d) " New_sum_file= "${bak_to_dir_par}/new_sum. $bak _from_name.$ (date +%y%m%d)" old_sum_file= "${bak_to_dir_par}/ Old_sum. $bak _from_name.md5 "if [ ! -d $bak _to_dir_par ];then mkdir $ bak_to_dir_parfifind $bak _from_dir |xargs -i {} md5sum {} >> $ new_sum_file 2>/dev/nullcp -a $bak _from_dir $bak _dir && echo "$bak _from_dir directory to $bak _dir directory backup successfully "new_sum_error= ' md5sum -c $new _sum_file 2>/dev/null|awk ' $2!~ ' OK ' |wc -l ' new_sum_ok= ' md5sum -c $new _sum_file 2>/ dev/null|awk ' $2~ "OK" ' |wc -l ' old_sum_error= ' md5sum -c $old _sum_file 2>/dev/null| awk ' $2!~ "OK" ' |wc -l ' old_sum_ok= ' md5sum -c $old _sum_file 2>/dev/null|awk ' $2~ "OK" ' |wc -l ' if [ $new _sum_error == $old _sum_error -a $new _sum_ok == $old _sum_ok ];then echo "Because today and yesterday ' s content is the same, delete the backup content of yesterday " rm -rf $yes _bak_dir $new _sum_filefiecho | cp $new _sum_file $old _sum_file &>/dev/nulL
A simple description for the above script:
This script declares variables for all the directories involved, which makes it easy to shrink the script functionality. Whether you change the directory entry to be backed up or the directory entry to which you have changed the backup is very different. Because the topic requires not to be backed up in a packaged way, this script does not involve packaging management of the script.
The bak_from_name variable is the directory name to be backed up Bak_from_dir variable is the directory path to be backed up Bak_to_dir_par variable is the parent (ancestor) directory path of the directory to which you want to back up Bak_dir variable is the directory path to which you want to back up Yes_bak The _DIR variable is the backup directory path of the day before the directory to be backed up new_sum_file variable is the MD5 list of the directories to be backed up as calculated today Old_sum_file variables are the MD5 list of the directories to be backed up as calculated today (the day of this variable is the current one after the script executes). , when the script starts executing, the file specified by the variable is the MD5 list of the previous day's backup directory, and the New_sum_error variable is the total number of entries that change the MD5 of the directory that is being backed up today New_sum_ The OK variable is the total number of entries for the MD5 of the directory that you want to back up the Old_sum_error variable is the total number of entries for the MD5 of the directory that you want to back up the previous day OLD_SUM_OK variable the total number of entries for the MD5 of the directory that you want to back up before
The first paragraph of the script is primarily to search for the MD5 value of each file in the directory that you want to back up, and then log to the MD5 list file of the directory that you want to back up today, and then perform the backup.
The following section of the script (after the execution of the backup) is mainly based on the MD5 list file of the directory to be backed up from the previous day, and whether the entries in the MD5 list file of the directory that you want to back up are the same, to determine whether the files in the directory you want to back up have changed, This determines whether to delete the previous day's backup.
Ok! This article concludes, hope to be able to help 51 Bo friends!
This article is from the "not only Linux" blog, so be sure to keep this source http://nolinux.blog.51cto.com/4824967/1541163