Shell script-regular backup based on the number of files

Source: Internet
Author: User

Tag: style blog HTTP color file 2014

Recently, when learning about Jenkins, there is a problem: the number of subdirectories under the Linux ext3 system directory cannot exceed 31998 (refer to: http://www.xshell.net/linux/1267.html), but Jenkins does not stop compiling versions, sometimes a directory of more than 30000 log folders is created in a few hours, so that you can clean up the directories in this folder at any time. So I wrote a script to implement the automatic backup function. Since I have never been familiar with Shell for a long time, it took me a day to implement it! I am not familiar with the shell format, syntax, and so on. I found some information on the Internet and wrote and debugged it! I don't need to talk about it anymore. I just need to script it!

1 #! /Bin/bash 2 ################################ 3 # filename: backuplogtimer. sh 4 # function: Regular log file backup 5 # version: 0.1 6 # authon: xueya 7 # Date: 2014.06.26 8 ############################### 9 # obtain the current path 10 path = 'pwd' 11 echo "current1 path: $ {path} "12 # loop execution 13 while [[[1-EQ 1] 14 do15 # view files in the folder 16 filelist = 'ls 2>/dev/null' 17 # traverse the files in this folder 18 for pfile in $ filelist19 do20 echo "current path: $ {path}/$ {pfile} "21 # determine whether the folder belongs to 22 if [[-d" $ {pfile} "]; then23 # calculate the number of folders 24 num = 'LS-L. /tmp | grep "^ d" | WC-l '25 # determine whether the number of files exceeds 2000026 if [$ {num}-GT 5]; then27 # Get current time 28 currenttime = 'date + % Y % m % d % H % m % s' 29 # define compressed file name 30 tarfilename = "/home/hubin/backup /too many pfile1__1_1_currenttime1_.tar.gz "31 # compressed file 32 echo" backup files to $ tarfilename "33 tar-zcvf $ {tarfilename} 'Find $ {path}/$ {pfile}-mmin + 60- type d' -- remove-files34 fi35 fi36 done37 # Wait 1 hour 38 sleep 60039 done

Notes:

1. The first line must start #! Tells the system that the program specified in the subsequent path is the shell program that interprets the script file.

2. The value of the variable. Add $

3. When displaying variables in "", it is best to add {}, for example, $ {tarfilename}, to prevent confusion with the subsequent characters. The system cannot identify the variable name.

4. Pay attention to spaces in the condition test section. There are spaces on both sides of square brackets and spaces on both sides of the-F,-lt, =, and other symbols. Without these spaces, an error occurs when shell interprets the script. For example, if

5. When defining variables, = do not have spaces on both sides

6. When the variable contains shell commands, use '', yes! The one next to it.

 

 

Related Article

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.