gonzui


Format: Advanced Search

tkernel_2/etc/backup_copybare sourcepermlink (0.01 seconds)

Search this content:

    1: #! /bin/sh -e
    2: #
    3: # ----------------------------------------------------------------------
    4: #     T-Kernel 2.0 Software Package
    5: #
    6: #     Copyright 2011 by Ken Sakamura.
    7: #     This software is distributed under the latest version of T-License 2.x.
    8: # ----------------------------------------------------------------------
    9: #
   10: #     Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   11: #     Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12: #
   13: # ----------------------------------------------------------------------
   14: #
   15: 
   16: #       backup_copy
   17: #
   18: usage='usage: backup_copy [-p] [-t] [-c] [-d old_dir] src... dst_dir'
   19: 
   20: cp_opt=''
   21: pfx=''
   22: old_dir=''
   23: src=''
   24: dst=''
   25: cmp_test=''
   26: 
   27: while test $# -gt 1
   28: do
   29:         case $1 in
   30:           -p)  cp_opt="$cp_opt $1" ;;
   31:           -t)  pfx='#' ;;
   32:           -c)  cmp_test='t' ;;
   33:           -d)  old_dir="/$2" ; shift ;;
   34:           -*)  echo "$usage" 1>&2 ; exit 1 ;;
   35: 
   36:           *)   if test ! -f $1
   37:                 then
   38:                         echo "$usage" 1>&2
   39:                         exit 1
   40:                 fi
   41:                 src="$src $1"
   42:                 ;;
   43:         esac
   44:         shift
   45: done
   46: 
   47: case $# in
   48:   1)    dst=$1 ;;
   49:   *)    echo "$usage" 1>&2 ; exit 1 ;;
   50: esac
   51: 
   52: case $src in
   53:   '')   echo "$usage" 1>&2 ; exit 1 ;;
   54: esac
   55: 
   56: if test ! -d $dst
   57: then
   58:         echo "$usage" 1>&2 ; exit 1
   59: fi
   60: 
   61: if test ! -d $dst$old_dir
   62: then
   63:         mkdir $dst$old_dir
   64: fi
   65: 
   66: # backup file suffix
   67: tm1=`date +%y%m%d`
   68: tm2=`date +%H%M%S`
   69: 
   70: for i in $src
   71: do
   72:         fname=`basename $i`
   73: 
   74:         if test -f $dst/$fname
   75:         then
   76: 
   77:                 if test $cmp_test && cmp -s $i $dst/$fname
   78:                 then
   79:                         continue
   80:                 fi
   81: 
   82:                 # need to backup
   83: 
   84:                 tm=$tm1
   85:                 if test -f $dst$old_dir/$pfx$fname.$tm
   86:                 then # exist same file name
   87: 
   88:                         tm=$tm1.$tm2
   89:                         if test -f $dst$old_dir/$pfx$fname.$tm
   90:                         then # cannot backup file
   91:                                 echo "cannot backup file : $dst/$fname"
   92:                                 exit 1
   93:                         fi
   94:                 fi
   95: 
   96:                 # backup
   97:                 mv $dst/$fname $dst$old_dir/$pfx$fname.$tm
   98:                 # update file access time
   99:                 file $dst$old_dir/$pfx$fname.$tm > /dev/null
  100:         fi
  101: 
  102:         # file copy
  103:         echo "$i -> $dst/$fname"
  104:         cp $cp_opt $i $dst/$fname
  105: done
  106: 
  107: exit 0