A very practical script for regular backup on Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.