A simple summary of keeping up-to-date files or purging outdated files

Source: Internet
Author: User

In operations, you often encounter files that you want to manage timestamps related to backups, logs, and so on, such as deleting backup/log files for more than 10 days, saving up to 10 of the latest backup/log files, and so on.

"Delete backup/log files older than 10 days" is a more common use, usually used to schedule tasks on a regular basis, and each time the files are generated are regular points in time.

"Save up to 10 backup/log Files" This requirement is typically used to generate backups at irregular intervals (for example, each time a manual execution occurs) and a log file (cut over a certain size).

The two different requirements correspond to different methods to achieve, regardless of the method used to find the required operation of the file or remove the unwanted files.

Linux Bash Shell Code "Delete backup/log files longer than 10 days" find these files for more than 10 days and then perform the removal:

function clean_old_files_older_than_day () {     save_days=10    files_ops= "/data/docker/logs/myapp/"     need_clean=$ ( find ${files_ops} -name  "*.log"  -mtime +${save_days} -exec ls  ' {} '  \;)     if [ ! -z ${need_clean} ]; then         echo  "old logs have been found, clean  old logs now.  "        find -l ${files_ops}  -maxdepth 1 -name  "*.log"  -a ! -name  "^."  -mtime +${save_days} -exec rm -rf  ' {} '  \;    else         echo  "all logs are not expired,  skipping.  "    fi} 

  Linux Bash shell code   Save up-to-date 10 backup/log files find these files in addition to the latest 10 files, and then perform the delete:

Function keep_some_newest_files () {    num_save=10    files_ops= "/ data/backup/db/mysql/"    num_files=$ (find ${files_ops} -type d - printf  "%[email protected] %p\n"  | sort -n | wc -l)      if test ${num_files} -gt ${num_save};then         echo  "total number of files is  $num _files."         num_ops=$ (Expr ${num_files} - ${num_save})         echo  "$num _ops files are going to  be handled. "         list_ops=$ (find ${files_ops} -type d - printf  "%[email protected] %p\n"  | sort -n | head -n${num_ops}  | awk -F  ' [&nbsP;] + '   ' {print $2} ')         # ifs= '   ' $ ' \ t ' $ ' \ n ',  If ifs is unset, or its value is exactly <space><tab> <newline>        old_ifs= $IFS          ifs= " "         for file_ops in $ {list_ops};d o            echo  "$file _ops"             test -d ${file_ops}  && rm -rf ${file_ops}        done         ifs= "$old _ifs"     else         echo  "total number of files is  $num _files."         echo  "0 files are going to be handled, skipping." &NBSP;&NBSP;&NBSP;&NBSP;FI}

Tag: Log file management, backup file management, find delete files

--end--


This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1833665

A simple summary of keeping up-to-date files or purging outdated files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.