天下网吧 >> 网吧天地 >> 网吧技术 >> 网吧系统 >> 正文

分析Linux下简单的备份的脚本

2013-1-6中国IT实验室佚名

  将backup放到PATH包含的目录里。最后几行注释只是给的一些建议,比如当想手动执行的时候,先创建个别名,当想周期执行等等…

  点击(此处)折叠或打开

  #!/bin/sh

  log_prefix=`which backup`

  log_prefix="${log_prefix%/*}"

  backup_log=$log_prefix/"backup.log"

  backup_dir="/var/backup"

  function help() {

  echo -e "Usage:\n\toption -h for help\n"\

  "\toption -a for add file\n"\

  "\toption -d for del file, eg -d n (n is the number of the entry, use -p get it\n"\

  "\toption -p for print backup file\n"\

  "\toption -c for set backup dir, default is $backup_dir"

  }

  function add() {

  pathname=`pwd`

  pathname=$pathname/$1

  echo $pathname 》$backup_log

  }

  function del() {

  total_line=`wc -l $backup_log | cut -d ' ' -f 1`

  if [ -z "${1//[0-9]/}" ]&&[ "$1" -le "$total_line" ]; then

  sed -i "${1}d" $backup_log >/dev/null 2>&1

  fi

  # sed -i "/$1/d" $backup_log >/dev/null 2>&1

  }

  function parse() {

  while getopts "a:d:c:ph" i;

  do

  case "$i" in

  a)

  filename=$OPTARG

  len=${#filename}

  c=${filename:len-1:len}

  if [ "$c" == '/' ]; then

  filename=${filename%/}

  fi

  add $filename

  [ "$?" == 0 ] && echo "add backup file successed" || echo "add backup file failed"

  exit

  ;;

  d)

  filenamenum=$OPTARG

  [ ! -f $backup_log ] && echo not "any backup info!!!" && help && exit

  del $filenamenum

  [ "$?" == 0 ] && echo "del backup file successed" || echo "del backup file failed"

  exit

  ;;

  c)

  if [ "$OPTARG" == "." ]; then

  OPTARG=`pwd`

  fi

  backup_dir=$OPTARG"/backup"

  ;;

  p)

  [ ! -f $backup_log ] && echo not "any backup info!!!" && help && exit

  cat -n $backup_log | awk '{printf("[%s]\t%s\n", $1, $2)}'

  exit

  ;;

  h)

  help && exit

  ;;

  *)

  help && exit

  ;;

  esac

  done

  }

  (

  flock -s 200

  parse $@

  [ ! -f "$backup_log" ] && echo "backup.log is empty!!!" && help && exit

  entry=`cat $backup_log`

  [ "$entry" == "" ] && exit

  [ ! -d "$backup_dir" ] && mkdir -p $backup_dir

  for i in $entry

  do

  rsync -azv $i $backup_dir

  done

  )200>/var/lock/abc

  #*/1 * * * * /usr/bin/backup >/dev/null 2>&1

  #alias backup='flock -s /var/lock/abc backup'

本文来源:中国IT实验室 作者:佚名

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系email:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧·网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行