The importance of regular backup of the source code and database of the website it is clear to us that it will be a waste of time to delete the files as the backup file increases, but it is cumbersome to be cleaned up, so it needs to be deleted automatically periodically.
I think of two ways:
1 Use the time stamp difference to delete periodically
2 using Logrotate to delete files
The first type of code is as follows
#!/bin/bashwebdir=/usr/localbackdir=/usr/local/backdirvar1= ' date +%y%m%d ' timelimit=2592000/usr/local/ mysql/bin/mysqldump -u root -p123456 hyzn09 > $backdir/ Hyzn09_ ' date +%y%m%d ' .sqlif [[ $? -eq 0 ]];then echo "Mysql databases hyzn09 backup succeed "ficd $webdirtar -zcf hyweb_ ' date +%y%m%d '. tar.gz test# !/bin/bashwebdir=/usr/localbackdir=/usr/local/backdirvar1= ' date +%y%m%d ' timelimit=2592000 # This means that the time is 30 days/usr/local/mysql/bin/mysqldump -u root -p123456 test > $backdir/test_ ' date +%y%m%d ' .sqlif [[ $? -eq 0 ]];then echo "Mysql databases test backup succeed "ficd $webdirtar -zcf test_ ' date +%y%m%d '. tar.gz testif [[ $? -eq 0 ]];then echo "WebDir test backup succeed "Fimv test_ ' date +%y%m%d ' .tar.gz $backdircd $backdirfor i in ' ls $backdir | awk -f_ ' {print $2 } ' | awk -F. ' {print $1} ' do olddate= ' date -d $i +%s ' newdate= ' date +%s ' Diffdate= ' expr $ (($newdate-$olddate)) ' #通过文件创建时间与现在时间的时间戳的差值去判断是否删除文件 echo $diffdate echo $timelimit if [[ $diffdate -gt $timelimit ]];then rm -fr * $i * # echo "Yes" fidone
It is important to note that when packing, do not use absolute path, will prompt
tar: Remove a warning from the member name that starts with "/"
I added a CD, if there is a better idea, hope to tell me
CD $webdir
TAR-ZCF hyweb_ ' Date +%y%m%d '. tar.gz test
The second is to back up the files and then configure the Logrotate configuration file to delete the old files. You can also use the Python time module to control the deletion of files. Oh!!!
Summary:
For a typical file backup, a differential backup can use the md5sum command to remove redundant files, while full provisioning can be controlled using timestamps.
This article is from the "xiaoding" blog, please be sure to keep this source http://dqzshine.blog.51cto.com/8093967/1590711
Web site source and database backup rotation