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 parameter description
File Content
#! /Bin/bash
# Author: InBi
# Date: 2011-08-05
# Role: backup files periodically.
# Website: http://www.itwhy.org/2011/07-28/707.html
######################################## ##########
LogFile =/backup/log/'date + "% Y-% m" '. log
SourceDir =/var/RDfile
BakDir =/backup
RetainDay = 20
ProjectLst =/backup/project. lst
######################################## ##########
DATE = 'date + "% Y-% m-% d "'
Echo "backup start at $ (date +" % Y-% m-% d % H: % M: % S ")"> $ LogFile
Echo "--------------------------------------------------"> $ LogFile
Cd $ BakDir
PROJECTLIST = 'cat $ projectlst'
For Project in $ PROJECTLIST
Do
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
Fi
Done
Find $ Bakdir-type f-mtime + $ RetainDay-name "*. $ Project. tgz"-exec rm {}\;>/dev/null
Echo "--------------------------------------------------"> $ LogFile
Echo "backup end at $ (date +" % Y-% m-% d % H: % M: % S ")" >>$ LogFile
Echo ""> $ LogFile
Exit 0
Project. lst file description
Each line indicates a project, that is, the file or directory under the directory you want to back up. Let's write it based on your actual needs. For example
Date
Files
IP
Pro