We often need to back up some data in our online maintenance work. Although the work is not complicated, it takes a lot of time. I will share a Linux backup script here to implement automatic backup with ease and
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 of which is the execution file backup. sh. 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
The code is as follows:
- #! /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 = 20 ProjectLst =/backup/project. lst ####################################### ########## echo "backup start at $ (date +" % Y-% m-% d % H: % M: % S ")"> $ LogFileecho "success" >>$ LogFilecd $ BakDirPROJECTLIST = 'cat $ projectlst' for Project in $ PROJECTLISTdoProjectData = $ SourceDir/$ ProjectDe StDir = $ 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