当前位置: > CentOS > CentOS入门 >

Centos使用tar命令做增量备份

时间:2014-12-15 01:31来源:linux.it.net.cn 作者:IT

想给subversion做个自动备份的脚本,一看目录大小,已经有几十个G了。
天天做完整备份太费系统资源了,增量备份是一个很好的解决方案。
每周做一次完整备份,然后每天只做增量备份。

Centos做增量备份还是很容易的,tar命令就可以完全胜任。

在cron里设置,每周日晚执行(每周日全备份,其余时间增量备份)。

示例一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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.tar.gz" ]; then
    rm -rf snapshot
    tar -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

示例二: 

 
Shell
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# simple backup script. intended to run daily from crontab
# called "biedacula" after "bieda", which is 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 do 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 will 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

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容