During our online maintenance work, we often need to back up some data. Although it is not complicated, it takes a lot of time. I will share a Linux backup script here to implement automatic backup with ease, in addition, the files backed up in N days can be retained. This script is divided into two files, one is the execution file backup. sh, and the other is the backup project file project. lst.
# Backup. sh
-- Important variable name Function
LogFile
Directory to be backed up by SourceDir
BakDir back up to this directory
RetainDay backup file retention days
ProjectLst backup project file configuration file
Code:
#!/bin/bash#author: InBi#date: 2011-07-28#role: backup files periodically.#website: http://www.itwhy.org/2011/07-28/707.html##################################################LogFile=/backup/log/`date +"%Y-%m"`.logDATE=`date +"%Y-%m-%d"`SourceDir=/var/RDfileBakDir=/backupRetainDay=20ProjectLst=/backup/project.lst##################################################echo "backup start at $(date +"%Y-%m-%d %H:%M:%S")" > $LogFileecho "--------------------------------------------------" >> $LogFilecd $BakDirPROJECTLIST=`cat $ProjectLst`for Project in $PROJECTLISTdoProjectData=$SourceDir/$ProjectDestDir=$BakDir/$ProjectPackFile=$DATE.$Project.tgzif [ -f $BakDir/$PackFile ]thenecho "backup file have exist !" >>$LogFileelsecp -RHpf $ProjectData $DestDir > /dev/nulltar -zcvf $PackFile $Project > /dev/nullecho "backup $Project done into $PackFile ">>$LogFilerm -rf $ProjectfiOldFile="$BakDir/"$(date --date="$RetainDay days ago" +"%Y-%m-%d").$Project.tgzif [ -f $OldFile ]thenrm -f $OldFile > /dev/nullecho "[$OldFile],Delete Old File Success!" >> $LogFilefiecho " " >> $LogFiledoneecho "--------------------------------------------------" >> $LogFileecho "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFileecho " " >> $LogFileexit 0
# Project. lst: this file is a text file. Each line of it represents a project, that is, the files or directories under the directory you want to back up.
onu_armonu_voiponu_wifi5xxx7xxx8xxxnms
Original article: http://wowubuntu.com/backup-script.html