发新话题
打印

linux下tar做文件系统增量备份

linux下tar做文件系统增量备份

  想给项目的CVS做个自动备份的脚本,一看目录大小,已经有近20个G。天天做完整备份太费资源了,增量备份是一个解决方案了。计划每周末做一次完整备份,然后每天做增量备份。

  Linux做增量备份还是很容易的,tar命令就可以。

  在cron里设置,每周日晚执行(每周日全备份,其余时间增量备份)。下面是从网上找了两个示例的脚本。

  示例一http://www.cnblogs.com/coffee_cn/archive/2010/03/26/1697038.html)
代码:
#!/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 --exclude $sourceserver.log
  fi
else
  if [ ! -f "inc$today.tar.gz" ]; then
    tar -g snapshot -zcf "inc$today.tar.gz" $source --exclude $sourceserver.log
  fi
fi
示例二http://www.futuremedia.pl/pub/projekty/backup/backup)
代码:
#!/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.2.2
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
[via]

TOP

发新话题