【原】Linux shell 定时打包备份脚本

花二个小时学习了一下 shell 脚本, 做一个定时备份程序,把我的工作内容每天自动备份一次:

 

山寨代码如下:

 

#!/bin/sh
# Created at 2010-11-18 by Lv Chester
#
# This script is used to bakup a folter at 0'oclock every day

#bakup folter at 11 hour 11 minute 11 seconds
bak_time="235959"

#Init pre bakup time
pre_day="19970101"

# Loop and check the time
while [ 1 ] ; do
  cur_date=`date +%Y%m%d`
  cur_time=`date +%H%M%S`
  loop_time=1;
  echo $cur_time
  cha=`expr  $bak_time - $cur_time`

  if [ $cha > 30 ] ; then
     loop_time=28
  elif [ $cha > 15 ] ; then
     loop_time=12
  fi

  #Bakup when pre day not equeue current date and current time equeue bakup time
  if [ "$cur_date" != "$pre_day" ] ; then
      if [ "$cur_time" == "$bak_time" ]; then
        echo "bak it"
        pre_day=$cur_date
      fi
  fi

  echo " wait ${loop_time}s"
  sleep $loop_time
done

 

 

#!/bin/sh
# Created at 2010-11-18 by Lv Chester
#
# This script is used to bakup a folter at 0'oclock every day

filetime=`date +%Y%m%d-%H%M%S`
name="Cheser-${filetime}.bak.tar.gz"
echo $name
echo "====================BACK LOG==$filetime=================================\n"  >> baklog.txt
tar czvf $name baktest  >> baklog.txt

 

你可能感兴趣的:(Linux)