歡迎您光臨本站 註冊首頁

利用tar做增量備份的shell

←手機掃碼閱讀     火星人 @ 2014-03-25 , reply:0

轉載自http://hi.baidu.com/mywebhome/bl ... 3384db2e2e2113.html

其實就是我寫的。呵呵……

買了塊新硬碟,太大了,500G 。個人用怎麼都用不完,所以,乾脆就劃出了一個分區來做備份分區。這是在安裝linux之前就划好了的。並沒有加入LVM中。安裝完畢后,修改/etc /fstab,把這個分區自動掛載到了/mnt/backup上。下面就是這個shell了,名字就叫backup.sh

#!/bin/bash


#############################################################################
####Date : 2009.11.15
####
####Wirter : King
####
####Description
####這是一個簡單的,利用tar進行增量備份的shell
####兩次備份,第一次備份一些比較小需要備份的目錄
####第二次備份usr,lib目錄
#######################################################################


##*********************************************************************


##設置本地shell變數####################################################
date_time=`date +%Y%m%d%H%M%S`
date_month=`date +%Y%m`
echo -e "Local current time is \t `date +%Y/%m/%d` `date +%H:%M:%S`"

backup_path=/mnt/backup/bak/
backup_mastername="$backup_path"master.tar.gz
backup_minorname="$backup_path"minor.tar.gz

incremental_path="$backup_path""$date_month"/
incremental_mastername="$incremental_path"master_"$date_time".tar.gz
incremental_minorname="$incremental_path"minor_"$date_time".tar.gz

snapshot="$incremental_path""date_month"
logfile="$backup_path"bak.log
########################################################################


###*******************************************************************


##檢查原始完全備份路徑與增量備份路徑是否存在##########################
##check backup path
##this function check backup path exist or not ,if not then creat the backup pat
h now


chk_back_path()
{
if [ -d $backup_path ]
then
echo "完全備份路徑已存在"
else
mkdir -p $backup_path
fi
}

chk_incremental_path()
{
if [ -d $incremental_path ]
then
echo "增量備份路徑已存在"
else
mkdir -p $incremental_path
fi
}
####################################################################


##*******************************************************************


##檢查測試logfile是否存在#############################################
##check log file
##this function check log file exist or not ,if not then create log file now
chk_log()
{
if [ -f "$backup_path"bak.log ]
then
echo "log文件已創建"

else
touch "$logfile"
echo -e "Begin time \t\t Create file \t\t\t End time" >> $logfil
e
fi


if [ -r "$backup_path"bak.log -a -w "$backup_path"bak.log ]
then
echo "log文件可讀寫"
else
chmod 644 "$backup_path"bak.log
fi

chattr +a "$backup_path"bak.log
}
####################################################################


##*********************************************************************


##檢查測試完全備份是否存在#####################################################
##check fullbackup
##this function check fullbackup file exist or not ,if not then create fullbacku
p now
chk_full()
{
if [ -f "$backup_mastername" ]
then
echo "原始主備份文件已存在"
else
echo -e "`date +%Y/%m/%d_%H:%M:%S` \t `basename $backup_masterna
me` \t\t\t\c" >> $logfile
tar zcPpvf $backup_mastername /bin /dev /home /sbin /srv /var /b
oot /etc /root /selinux /sys
echo "`date +%Y/%m/%d_%H:%M:%S`" >> $logfile

fi

if [ -f "$backup_minorname" ]
then
echo "原始次備份文件已存在"
else
echo -e "`date +%Y/%m/%d_%H:%M:%S` \t `basename $backup_minornam
e` \t\t\t\c" >> $logfile
tar zcPpvf $backup_minorname /usb /lib
echo "`date +%Y/%m/%d_%H:%M:%S`" >> $logfile
fi
}
######################################################################


##********************************************************************


##測試增量備份是否存在#######################################################
##check incremental backup
##this is function check incremental backup file exist or not ,if not then creat
e incremental backup file now

chk_incremental()
{
if [ -f "$incremental_mastername" ]
then
echo "主增量備份文件已存在"
else
echo -e "`date +%Y/%m/%d_%H:%M:%S` \t `basename $incremental_mas
tername` \t\c" >> $logfile
tar -g `dirname $backup_mastername`snapshot -zcPpvf $backup_mast
ername /bin /dev /home /sbin /srv /var /boot /etc /root /selinux /sys
echo "`date +%Y/%m/%d_%H:%M:%S`" >> $logfile
fi

if [ -f "$incremental_minorname" ]
then

echo "次增量備份文件已存在"
else
echo -e "`date +%Y/%m/%d_%H:%M:%S` \t `basename $incremental_min
orname` \t\c" >> $logfile
tar -g `dirname $backup_minorname`snapshot -zcPpvf $backup_minor
name /usb /lib
echo "`date +%Y/%m/%d_%H:%M:%S`" >> $logfile

fi
}
######################################################################


##********************************************************************


#######################################################################
chk_back_path
chk_incremental_path
chk_log
chk_full
chk_incremental
########################################################################



這個shell我自己試用了一下,效果還不錯。如果開機時間固定的,可以把它加入到計劃任務中去。我的機器備份一次,大概需要25分鐘左右。另外,需要注意的就是,在備份的時候,盡量不要開啟頻繁讀取數據的程序,比如向VirtualBox之類的。它會提醒你,備份的數據正在改變。

[火星人 ] 利用tar做增量備份的shell已經有520次圍觀

http://coctec.com/docs/linux/show-post-159630.html