#!/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)