CentOS Use the tar command to do an incremental backup script

Source: Internet
Author: User
Tags gz file mkdir centos


Want to give subversion an automatic backup script, a look at the size of the directory, already has dozens of G.
It is a good solution to make a full backup too cost system resources every day.
Make a full backup once a week, and then do an incremental backup every day.

CentOS It is easy to do an incremental backup, and the tar command can be completely qualified.

Set in Cron, run every Sunday night (full backup every Sunday, incremental backup for the rest of the time).

Example one:

The code is as follows Copy Code

#!/bin/bash

# define
dayofweek= ' Date ' +%u '
today= ' Date ' +%y%m%d '
source=/data/
backup=/backup/

# action
CD $backup

If [$dayofweek-eq 1]; Then
if [!-F "full$today.<span class= ' wp_keywordlink_affiliate ' ><a href=" Http://lxy.me/tag/tar "title=" View tar All the articles in the "target=" _blank ">tar</a></span>.gz"]; Then
RM-RF Snapshot
<span class= ' wp_keywordlink_affiliate ' ><a href= http://lxy.me/tag/tar ' title= ' view all articles in tar "target=" _blank ">tar</a></span>-G snapshot-zcf" full$today.tar.gz "$source
Fi
Else
if [!-F "inc$today.tar.gz"]; Then
Tar-g snapshot-zcf "inc$today.tar.gz" $source
Fi
Fi

Example two:

The code is as follows Copy Code

#!/bin/bash
# Simple backup script. Intended to run the from crontab
# called "Biedacula" after "Bieda", which are Polish word for "poor".

# Implements poor man ' s GFS scheme, hence the name:
# requires GNU tar, GNU gzip and ncftp

# These files must contain file/dir paths (one a line)
Pathfile=/etc/backup-defs
Skipfile=/etc/backup-excludes
# This is a snapshot file auto-created by GNU Tar
Snapshot=/etc/backup-snapshot

# FTP host to send backups (must allow anonymous RW access for me)
ftphost=192.168.1.118
Ftpport=21

# When to does full, monthly/weekly backups
Full_monthday=1 # 1st day of Month
Full_weekday=7 # Sunday

# How many ' tapes ' for monthly backups
Keep_monthly=3
# How many ' tapes ' for weekly backups
Keep_weekly=4
# in total you have (keep_monthly + keep_weekly + 6) "Tapes"

function Biedump {
Local type=$1
Local label=$2
Local start= ' Date +%y%m%d%h%m '
echo "$start: Starting $TYPE dump to label $LABEL"
If ["x$type" = = "Xfull"]; Then
RM-RF $SNAPSHOT
Fi
Tar-c-t$pathfile-x$skipfile-g$snapshot-p-F-
| Gzip
| Ncftpput-c-s.tmp-p $FTPPORT $FTPHOST $LABEL
Local res=$?
Local end= ' Date +%y%m%d%h%m '
If [$res-eq 0]; Then
echo "$end: $TYPE dump OK."
Else
echo "$end: $TYPE dump FAILED with exit code $res."
Fi
}

Host= ' Hostname-f '
yyyy= ' Date +%y '
mm= ' Date +%m '
dd= ' Date +%d '
ww= ' Date +%v '
day_of_week= ' Date +%u '
dayofweek= ' Date +%a '

echo "Hello. This is Biedacula backup running at $host. "
echo "Today is $yyyy/$mm/$DD, day $day _of_week ($dayofweek) of week $WW."

If [$dd-eq $FULL _monthday]; Then
Let "n = (mm% $KEEP _monthly) + 1"
Biedump ' full ' $host-m-$n. tgz "
elif [$day _of_week-eq $FULL _weekday]; Then
Let "n = (ww% $KEEP _weekly) + 1"
Biedump ' full ' $host-w-$n. tgz "
Else
Biedump ' incr ' "$host D-$dayofweek. tgz"
Fi

Example Three

or use the Crontab function, this function is too practical, praise a first

Take advantage of someone else's backup script:

The code is as follows Copy Code

#!/bin/bash
#好的做法
#根据项目不同来重命名该脚本
#例如 amountebak.sh or pandawillsbak.sh
#该脚本最好放在 the absolute path part of a $BAKPP definition
Find the appropriate backup file #你可以在 $BAKPP
#例如/usr/backup/amountebak.sh or/usr/backup/pandawillsbak.sh


########## Init Path ########
# The parameters that must be defined in the following parameters are $BAKPP, $project, $PROJECTPP
Tar=/bin/tar
# Where to store the backup files, to distinguish them by project name
Bakpp=/var/www/html/media/backupfiles/"$project"
# folders that need to be backed up, path to the folder
Project=media
projectpp=/var/www/html/media/root/
# Parament for Varible
ym= ' Date +%y%m '
ymd= ' Date +%y%m%d '
# A subdirectory of a file that is stored in a store, distinguished by month, his superior directory is $bakpp defined
monthbakpp= $BAKPP/$ym
gidpp= $MONTHBAKPP
Gidshot=gid$project$ym
# file name for full backup
Fullname= $ym
# The file name of the incremental backup
Incrementalname= $ymd
# Record the location of the log
log= $bakpp/$project. Log

############ Chk_full #######################
# Check if a full backup exists, create it if it doesn't exist

#this function Check fullbackup file exist or not, if does then create Fullbackup now

Chk_full ()
{
If [e "$monthbakpp"/"$project" _ "$ym" _full.tar.gz];then
Echo "" $project "_" $ym "_ full.tar.gz file exist!! = = ' Date +%y-%m-%d-%t ' >> $log
Else
Tar_full
Fi
}
######### chk_incremental ########
# Check for Does the volume backup exist
Chk_incremental ()
{
While [e "$monthbakpp"/"$project" _ "$incrementalname" _incremental.tar.gz]
do
Incrementalname= ' echo ' $incrementalname + 0.1 ' | BC '
Done
}
######## tar_incremental #######
# Perform an incremental backup
Tar_incremental ()
{
CD $PROJECTPP
echo "beigin_time===== ' date +%y-%m-%d-%t ' = =" $projec T "_" $incrementalname "_incremental.tar.gz" >> $log
Sleep 3
$TAR-G $GIDPP/$gidshot-ZCF $MONTHBAKPP/"$ Project "_" $incrementalname "_incremental.tar.gz $project
Echo" end_time======== ' date +%y-%m-%d-%t ' = = = $ Project "_" $incrementalname "_incremental.tar.gz" >> $log
}

######## Tar_full ###########
Tar_full ()
{
Touch $GIDPP/$gidshot
CD $PROJECTPP
echo "beigin_time===== ' date +%y-%m-%d-%t ' = =" $project "_" $fullname "_full.tar.gz" >> $log
$TAR-G $GIDPP/$gidshot-zcf $monthbakpp/"$project" _ "$fullname" _full.tar.gz $project
echo "end_time======== ' date +%y-%m-%d-%t ' = =" $project "_" $fullname "_full.tar.gz" >> $log
}

########### Backup ##############################
# Call the backup in general, and check accordingly to ensure that the backup prerequisites are fully prepared
Backup ()
{
If [-D $MONTHBAKPP]; Then
Chk_full
Chk_incremental
Tar_incremental
Else
Mkdir-p $BAKPP/$ym
Tar_full
Fi
}

########### let ' s begin #############
# Check to see if $BAKPP exists and if not, create a fallback
if [-D $bakpp];then
Backup
Else
Mkdir-p $bakpp
Backup
Fi
#advice can create a file for put backup file, EG/USR/CCTCC
#crontab
# Mini hours Day Month Week Command
# */5 * * * */home/mmroot/zbb/aaa.sh
# 0 1 */1 * */home/mmroot/zbb/aaa.sh
# TAR-ZTF test.tar.gz View the files inside the backup file

Related Article

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.