文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>a auto backup bashscritp(building)

a auto backup bashscritp(building)

时间:2005-04-13  来源:ywwlyc

    Nowadays,I debuged a program written by a professon.In addition to the contents of the programme,I'was also confused with the edition number of the piles,after all,there were so many backups.I can't directory find which one is the latest or newer.
    Now,I've written a script used to make a back,it's now just equiped with some simple questions,some sumplement functions will be added(but not now-:) because of having no time).
    Most frequently used script styles are included in this script except for the regular expression(it's the most interesting and powerful one).
    It'll be developed to be a powerful one sooner or later.

#!/bin/bash
#============================================================================
#This is a script used to automatically backup a certain directory
#============================================================================
#author:yuanweiwei([email protected])
#date:2005.4.10
#version:1.0
#============================================================================
#This function func_f is used to back a certain file,its first parameter must
#be a file name which is a absolute path name,and the second is the dest
#directory where the file will be backed
func_f()
{

#if wrong parameters ,exit
if [ $# != 3 ]
then
echo "only $# parameters,bad parameters ,exit! in func_f"

exit 1
fi

filename=$1
dest_directory=$2
log=$3

#if the first parameter,which should be a filename is not exist or is a
#directory,exit
if [ ! -e $filename -o -d $filename ]
then
echo "$filename is not a file,exit!"
exit 1
fi

#if dest directory doesn't exit
    if [ ! -e $dest_directory ]
        then
        echo "dest directory $dest_directory doesn't exist,creat?"
        read com
#note that at this point root privilege maybe needed and should be checked,
#and command value should be checked
        if [ "$com" == "Y" -o "$com" == "y" -o "$com == "YES" -o "$com == "yes" ]
        then
        echo "begin to make dir $dest_directory"
        mkdir -p  $dest_directory
        elif [ "$com" == "N" -o "$com" == "n" -o "$com == "NO" -o "$com == "no" ]
        then
        exit 0
        else
        echo "unknown command,exit"
    fi
#if dest directory is not a directory ,exit
    elif [ ! -d $dest_directory ]
        then
        echo "$dest_directory is not a directory,exit"
        exit 0
    fi

#now everything is fixed,begin -:)
#get current time as suffix of the name of backup files
    backtime=$(date +%Y_%m_%d_%H_%M)
    echo $backtime
    suffixname=$backtime
    dest=$dest_directory/${filename}_$suffixname
#if dest file exits,exit
    if [ -e $dest ]
    then
    echo "$dest exists,please wait one more minute"
    exit 0
    else
        cp $filename $dest
       if [ -e $log ]
       then
        echo "back $filename to $dest and exit status is $value" > $log
       else
        echo "back $filename to $dest and exit status is $value" >> $log
       fi    
   fi
}

#===========================================================================
#===========================================================================
#This function is used to backup a directory
#method:make a copy of a certain directory

func_d()
{
#if bad parameters ,exit
if [ $# != 1 -a $# != 2 -a $# != 3 ]
then
echo "bad parameters"
echo "usage:./execute sourcefile [dest_directory [log name]]"
exit 1
fi
default_dest=/usr/local/back/default
default_log=/usr/local/back/default/auto_back.log
source_directory=$1
#get the proper values for source directory dest directory and log file respetively
case $# in
1)
    dest_directory=$default_dest
    log_file=$default_log;;
2)
    dest_directory=$2
    log_file=$log;;
3)
    dest_directory=$2
    log_file=$3;;
esac

#if the source directory doestn't exist,exit
if [ ! -e $source_directory ]
then
echo "$source_directory doesn't exist,exit"
exit 1
elif [ ! -d $source_directory ]
then
echo "$source_directory is not a directory,exit"
exit 1
fi

#if the dest source directory doesn't exit,wait for another minute
if [ ! -e $dest_directory ]
then
echo "$dest_directory doesn't exist,create now?"
read comm
case $comm in
"y"|"Y"|"yes"|"YES")
mkdir -p $dest_directory;;
"n"|"N"|"NO"|"no")
exit 0;;
*)
echo "unknown command,exit"
exit 0;;
esac

#else,the dest_directory exist,but not a directory
elif [ ! -d $dest_directory ]
then
echo "$dest_directory is not a directory,pleace check it"
exit 0
fi

#now,the dest directory exists and is ok
backtime=$(date +%Y_%m_%d_%H_%M)
suffixname=$backtime
destination=${dest_directory}/${source_directory}_$suffixname
#to see whether the destnation name exists
if [ -e $destination ]
then
echo "this directory has just been backed,please wait one more minute to back"
exit 0
fi

#now everything is ok,begin-:)
pathnow=$(pwd)
cp -a $source_directory $destination
#if [ ! -e $log_file]
#then
#echo "back directory ${pathnow}/$source_directory  to $destation" > $log_file
#else
exec 6>&1
exec >> $log_file
echo "back directory ${pathnow}/$source_directory  to $destination"
exec 1>&6 6>&-

#fi
#everything is fixed now
}

#=====================================================================
#start
#=====================================================================
default_directory="/usr/local/back/default"
logfile=$default_directory/auto_back.log
#see how many arguments
#======================================================================
if [ $# != 1 -a $# != 2 ]
then
echo "wrong parameters!"
echo "usage:./auto_back source_dirctory dest_directory"
exit 0
fi
#======================================================================
if [ ! -e $1 ]
then
echo "$1 doesn't,please check it"
exit 1;
fi

case $# in
1)
    if [ -f $1 ]
    then
    func_f $1 $default_directory $logfile
    else
    func_d $1 $default_directory $logfile
    fi;;
2)                                                                                
      if [ -f $1 ]
        then
        func_f $1 $2 $logfile
        else
        func_d $1 $2 $logfile
        fi;;
esac
#everything fixed ,exit
exit 0

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载