According to the online code, this script is divided into two files, one is to execute the file backup.sh, and the other is to back up the project file Backup.txt. Backup.txt This file is better understood, is a text file, each line of it represents a project, that is, you need to back up the directory under the file or directory.
The code is as follows:
#!/bin/bash
#author: Rekfan
#date: 2013-01-11
#作用: Backing up files regularly
#website:http://blog.rekfan.com
##################################################
# Important Variable Name effect
# SourceDir directory to be backed up
# LogFile log file
# BakDir1 back up the upper directory
# BAKDIR2 Backup Directory
# Retainday Backup archive file retention days
# projecttxt Backup project file configuration file
##############★The following information needs to be configured★##################
Date= ' date + '%y-%m-%d '
Sourcedir=/var/web
Bakdir1=/web_bak
bakdir2=/web_bak/' date + '%y-%m-%d '
Logfile=/web_bak/log_ ' date + '%y-%m-%d '. log
Retainday=20
Projecttxt=/web_bak/backup.txt
##############★ Creating a backup directory ★##################
Mkdir-p $BakDir 2/
echo "Backup start at $ (date +"%y-%m-%d%h:%m:%s ")" > $LogFile
echo "--------------------------------------------------" >> $LogFile
echo "" >> $LogFile
echo "Create backup directory" $BakDir 2 "Success" >> $LogFile
CD $BakDir 2
##############★ Backup Core Code ★##################
projectlist= ' cat $ProjectTxt '
for Project in $ProjectList
do
Projectdata= $SourceDir/$Project
destdir= $BakDir 2/$Project
packfile= $Project-$DATE. tar.gz
 &NBSP
if [-F $BakDir 2/$PackFile]
then
echo "backup file has exist!" >> $LogFi Le
else
cd $SourceDir
TAR-ZCVF $BakDir 2/$PackFile $Project >/dev/null
echo "directory or file $ sourcedir/$Project has been backed up to $BakDir 2/$PackFile ">> $LogFile
fi
####### #######★ delete $retainday days ago ★##############
oldfile= "$BakDir 1/" $ (date--date= "$ Retainday days ago "+"%y-%m-%d ")
if [-F $OldFile]
then
RM-RF $OldFile >/dev/null
&NB Sp echo "[$OldFile],delete old File success!" >> $LogFile
fi
echo "" >> $LogFile
Done
echo "--------------------------------------------------" >> $LogFile
echo "Backup end at $ (date +"%y-%m-%d%h:%m:%s ")" >> $LogFile
echo "----------By:rekfan" >> $LogFile
echo "" >> $LogFile
Exit 0
Linux regularly automatically backs up folder scripts