shell命令实现文件的备份和恢复

#!/bin/bash  
  
#备份目录函数  
backupdir()  
{
       
 dirtest  
 echo "Backupping..."  
 echo $DIRECTORY  
 #mkdir $newdir  
 #把DIRECTORY备份成为backup.tar.gz放在/tmp目录下  
 tar -zcvf /tmp/backup.tar.gz $DIRECTORY    
}  
  
  
  
#恢复目录函数  
restoredir()  
{
       
 dirtest  
 echo "Restoring..."  
 echo $DIRECTORY  
 tar -zxvf /tmp/backup.tar.gz  
}  
  
  
#验证目录函数  
dirtest()  
{
       
 echo "please enter the directory name of backup file:"  
 read DIRECTORY  
 #read newdir  
 if [ ! -d $DIRECTORY ] #判断一下是不是目录  
 then  
    echo "sorry,$DIRECTORY is not a directory!"  
        exit 1  
 fi  
  
 #cd $DIRECTORY  
}  
  
  
clear  
ANS=Y  
while [ $ANS = Y -o $ANS = y ]  
do   
        echo "========================"  
    echo "= Backup-Restore Mune  ="  
    echo "++++++++++++++++++++++++"  
    echo "+ 1:Backup Directory   +"  
    echo "+ 2:Restore Directory  +"  
    echo "+ 0:Exit               +"  
    echo "++++++++++++++++++++++++"  
    echo "Please enter a choice(0-2):"  
  
    read CHOICE  
    case "$CHOICE" in  
        1) backupdir;;  
        2) restoredir;;  
        0) exit 1;;  
        *) echo "Invalid Choice!"  
             exit 1;;  
    esac  
    if [ $? -ne 0 ]  #$?不等于0  
    then  
         echo "Program encounter error!" #程序遇到了错误  
           exit 2  
        else   
           echo "Operate successfully!"  
    fi  
      
  
    echo "Would you like to continue? Y/y to continue,any other key to exit:"  
    read ANS  
    clear  
done  

你可能感兴趣的:(操作系统学习之路)