1:上传时,文件备份按其原来的目录结构,备份到其他目录下面($bakdir)
#!/bin/sh#定义目录:bakdir备份文件的目录 zipfiledir是zip打包的目录 rootdir 上传的根目录, upload解包目录
bakdir='/root/b/'
zipfiledir='/root/zip/'
upload='/root/upzip/'
rootdir='/root/a/'tmpdir='/root'
datetime=`date +%Y%m%d%H%M` #定义输出时间格式dateroll=`date +%Y%m%d` zipfile1=$1zipfile=`echo ${zipfile1%.zip}`function ccopy(){echo " enter y|Y to confirm "read confirmdcase $confirmd iny|Y)cd $uploadif [[ -d $zipfile ]]thenrm -rf $zipfilemkdir $zipfileelse mkdir $zipfilefiuprealdir=$upload$zipfile'/'cd $uprealdirunzip $zipfiledir$zipfilefind ./ -name '*' >$tmpdir/filedircp $tmpdir/filedir $tmpdir/souredirsed -i 's//.g' $tmpdir/souredirfilenum=`wc -l $tmpdir/filedir|awk '{print $1}'`i=1while [[ "$i" -le "$filenum" ]];dosourename=`sed -n "$i"p $tmpdir/souredir`#备份文件夹下面有没有这个上传文件的目录,没有的话,就新建目录if [[ -f $rootdir$sourename ]]thenhuozui=`dirname $sourename`if [[ ! -d $bakdir$huozui ]]thenmkdir -p $bakdir$huozuifi#移动文件到备份目录#mv $rootdir$sourename $rootdir$sourename$datetime$zipfilemv $rootdir$sourename $bakdir$sourename$datetime$zipfile.bakcp $uprealdir$sourename $rootdir$sourenameecho "$rootdir$sourename success" #如果文件或目录不存在的时候elif [[ ! -e $rootdir$sourename ]]then #如果是目录则新建目录if [[ -d $uprealdir$sourename ]]then mkdir -p $rootdir$sourenameecho $rootdir$sourename directory create""#如果不是目录,因为在上面已经新建目录了。则只需copy就可以else cp $uprealdir$sourename $rootdir$sourenameecho "$rootdir$sourename success"fi else echo ""fii=$(($i+1))done;;*)echo "No action";;esac}function croll(){echo " enter y|Y to confirm "read confirmdcase $confirmd iny|Y)uprealdir=$upload$zipfile'/'cd $uprealdirfind ./ -name '*' >$tmpdir/filedircp $tmpdir/filedir $tmpdir/souredirsed -i 's//.g' $tmpdir/souredirfilenum=`wc -l $tmpdir/souredir|awk '{print $1}'`i=1while [[ "$i" -le "$filenum" ]];dosourename=`sed -n "$i"p $tmpdir/souredir`lastname=`sed -n "$i"p $tmpdir/souredir|awk -F / '{print $NF}'`if [[ -f $rootdir$sourename ]]thencurrdir=`dirname $bakdir$sourename`if [[ -d $currdir ]]thencd $currdirelse currdir=`dirname $rootdir$sourename`cd $currdirfi #ffstat 代表需要恢复文件的备份文件的个数 1:为一个备份。0:说明没有,该上传文件是新文件ffstat=`find ./ -name "$lastname*$zipfile*"|wc -l`if [[ "$ffstat" == 1 ]]then #newbakname=`ls $rootdir$sourename*$zipfile`newbakname=`ls $bakdir$sourename*$zipfile*`echo " start roll $rootdir$sourename"rm -f $rootdir$sourenamemv $newbakname $rootdir$sourenameecho "$rootdir$sourename success"elif [[ "$ffstat" == 0 ]]then filestat=`ls $rootdir$sourename*|wc -l`if [[ "$filestat" == 1 ]]then echo "$rootdir$sourename is new file "rm -f $rootdir$sourenameecho "$rootdir$sourename roll success"else "$rootdir$sourename error"fielse "$rootdir$sourename error"fifii=$(($i+1))done;;*)echo "No action";;esac}case "$2" inupload)ccopy;;roll)croll;;*)echo $"Usage: $upload zipfile {upload|roll}"exit 1esac红色为修改部分