Shell script-Backup According to the number of files (reprint)

Source: Internet
Author: User

#!/bin/bash############################### #FileName: Backuplogtimer.SH#Function: Scheduled backup log file #version:0.1#Authon: xueya#date:2014.06. -############################### #获取当前路径path=`pwd`Echo "current1 path: ${path}"#循环执行 while[[1-eq1 ]] Do#查看文件夹下的文件 fileList=`ls 2>/dev/NULL' #遍历此文件夹下的文件 forPFileinch$fileList Do       Echo "Current path: ${path}/${pfile}"#判断是否属于文件夹if[[-D"${pfile}"]]; Then#计算文件夹数量 Num=`ls-L./tmp|grep "^d"|WC-l ' #判断文件数是否超过了20000if[[${num}-GT5]]; Then#获取当前时间 currenttime=`Date+%y%m%d%h%m%S ' #定义压缩文件名称 tarfilename="/home/hubin/backup/${pfile}_${currenttime}.tar.gz"#压缩文件Echo "backup files to $tarFileName"              Tar-ZCVF ${tarfilename} 'Find${path}/${pfile}-mmin + --type d '--remove-Filesfi       fi    Done#等待1小时Sleep  - Done

Several points to note:

1. The first line needs to start with #!, telling the system that the program specified by the subsequent path is the shell that interprets the script file.

2. For the value of the variable. When you take a value, add $ to it before the variable name.

3. When displaying variables in "", it is best to add {}, such as ${tarfilename}, to prevent the concatenation of the following characters, confusion, the system can not identify the variable name

4. Note the spaces in the Condition Test section. There are spaces on both sides of the square brackets, and there are also spaces on both sides of the-F,-lt, =, and so on. Without these spaces, the shell will make an error when interpreting the script. such as if

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

6. When a variable contains a shell directive, you need to use ', yes! Next to that one.

Shell script-Backup According to the number of files (reprint)

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.