because the last time to do the project, the customer asked for the system state to do automatic backup, at that time feel that their script is not enough on the grade, began to search the internet a variety of better scripts, and now found a pretty good script, with everyone to analyze share. In fact, I looked at the first time a small part of the middle did not understand, feel that the grammar is really right, but still slightly modified a little .
#说明使用的是bash语法来写脚本 #!/bin/bash# Set the log file, the/backup/log directory logfile=/backup/log/' date + "%y-%m" is built. log# Backup Source directory, At this point we can write "root directory", because the system state must be saved in the root directory. #从网上查资料得知, the general backup system State backs up/etc,/var,/root,/home,/opt, and other directories are sourcedir=/that are not important to the state of the system #备份目标BakDir =/backup# Save 20 days expired automatic deletion retainday=20# the contents of the backup can be written into project.lst,project.lst fill in the backup directory, the following is a backup of the # specific script, the core is tar packaging, and the important content recorded in the log file Projectlst =/backup/project.lst################################################# #DATE = ' date + '%Y-%m-%d ' echo ' backup start at $ (date + "%y-%m-%d %h:%m:%s") " > $LogFileecho "----------- ---------------------------------------" >> $LogFilecd $BakDirPROJECTLIST = ' cat $ProjectLst ' for project in $PROJECTLISTdo projectdata= $SourceDir/$Project destdir= $BakDir/$ project packfile= $DATE. $Project .tgz if [ -f $BakDir/$PackFile ] then echo "backup file have exist !" >> $LogFile
else cp -RHpf $ProjectData $DestDir >/dev/null tar - zcvf $PackFile $Project >/dev/null echo "backup $Project done into $PackFile >> $LogFile rm -rf $Project fidoneecho "-- ------------------------------------------------" >> $LogFileecho " Backup end at $ (date + "%y-%m-%d %h:%m:%s") " >> $LogFileecho " >> $ logfile################################################## #下面的内容就是把刚才备份的内容传到服务器上, provided you have an access to the end of the FTP server #put backup to ftp serverhost=192.168.110.111ftp_username=ftpuserftp_password=123456cd $ bakdirecho "start open ftp serverat $ (date +"%y-%m-%d %h:%m:%s ")" > > $LogFileecho -------------------------------------------------- >> $LogFile/usr/bin /ftp -in <<eofopen $HOSTuser $FTP _username $FTP _passwordput $DATE. $Project .tgzbyeeofecho "put ftp end at $ (date + "%y-%m-%d %h:%m:%s") " >> $LogFile # After the final upload and then view the local backup more than 20 days of automatic deletion, so that you can implement local remote dual backup find $Bakdir -type f -mtime + $RetainDay - name "*. $Project. tgz" -exec rm {} \; >/dev/nullexit 0# Finally, we can also use crontab to make a recurring plan, such as weekly full backup #59 23 * * * /home/backup.sh
Explain a few grammatical questions:
About if [-F $ $]
This article is from the "Liu Qiong @ tiandaochouqin" blog, be sure to keep this source http://lqiong.blog.51cto.com/8170814/1542483