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

发布时间:2020-12-25编辑:脚本学堂
一个简单的文件增量备份shell脚本,有需要的朋友可以参考下。

一个简单的文件增量备份shell/ target=_blank class=infotextkey>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