当前位置: > shell编程 >

一个简单的文件增量备份shell脚本

时间:2014-09-10 22:38来源:linux.it.net.cn 作者:it

一个简单的文件增量备份shell脚本,有需要的朋友可以参考下。
 

复制代码代码如下:

#!/bin/bash
# Back files and dirs for full and increment.
# backup.sh [full|inc] file1 file2 dir1 dir2 hostname-[full|inc]-date.tgz

#================================ Check Envir ===============================
MYDATE=`date +%y%m%d-%H%M`
SRCFILE=`echo $@|cut -d' ' -f2-`
DSTFILE=/backup/`hostname`-$1-$MYDATE.tgz

if ! [ -d /backup ];then mkdir /backup;fi
if [ 1 -ge $# ];then echo "Usage: $0 [full|inc] dir file ...";exit;fi
for i in $SRCFILE;do
  if ! [ -f $i ];then echo "Error: File \"$i\" is not exist, please input the right files!";exit;fi
done

#================================ Backup Action ===============================
case $1 in
 full)
    rm -f /tmp/.snapshot
    tar --wildcards --exclude *.log -g /tmp/.snapshot -zcPf $DSTFILE $SRCFILE
 ;;
 inc)
    tar --wildcards --exclude *.log -g /tmp/.snapshot -zcPf $BSTFILE $SRCFILE
 ;;
 *)
    echo "Usage: $0 [full|inc] dir file ...";exit
 ;;
esac

#================================ Check Backup ===============================
if [ -f $DSTFILE ];then
  echo "Backup Sucsess!"
  echo "You have execut a $1 backup."
  echo "You are back \"$SRCFILE\" to $DSTFILE!"
else
  echo "Error: Backup faild, pleas check it!"
fi


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