服务器备份与还原脚本

来源:互联网 发布:亲和素淘宝店 编辑:程序博客网 时间:2024/06/06 09:58

#!/bin/bash
#
#this is a main() interface
#
#
#

stname=$(hostname | awk -F "." '{print $1}')

#the port of nginx
_pnginx=80
#the port of Chinese web site
_pwwworg=8080
#the port of English web site
_pwwwcom=8081
#the swich of nfs
_nflag=yes

#check log
_logfile=/tmp/${_hostname}chk$(date +%Y%m%d%H%M).log
echo "LogFileName:${_hostname}chk$(date +%Y%m%d%H%M).log">>$_logfile
echo "IP:">>$_logfile
ifconfig | grep Mask >>$_logfile
echo "">>$_logfile
echo "=====================================================">>$_logfile
echo "srv : check the status of server running!"
echo "bakngx : backup the control file of nginx!"
echo "baktomchn : create a image of chinese web site!"
echo "baktomeng : create a image of english web site!"
echo "bakusrchn : backup the datafile of chinese users!"
echo "bakusreng : backup the datafile of english users!"
echo "storesecene : protect scene!"
echo "crtsnap : create a web system snapshot!"
echo "restorescene : recovery web system scene!"
echo "restoresnap : recovery web system snapshot point!"
echo ""
echo ".............................................................">>$_logfile
echo "usage:">>$_logfile
echo "srv : check the status of server running!">>$_logfile
echo "bakngx : backup the control file of nginx!">>$_logfile
echo "baktomchn : create a image of chinese web site!">>$_logfile
echo "baktomeng : create a image of english web site!">>$_logfile
echo "bakusrchn : backup the datafile of chinese users!">>$_logfile
echo "bakusreng : backup the datafile of english users!">>$_logfile
echo "storescene : protect scene!">>$_logfile
echo "crtsnap : create a web system snapshot!">>$_logfile
echo "restorescene : recovery web system scene!">>$_logfile
echo "restoresnap : recovery web system snapshot point!">>$_logfile
echo ".............................................................">>$_logfile

_dfbkhome=/home/backup/
_bkprocn=$_dfbkhome/www/cn/
_bkproen=$_dfbkhome/www/en/
_bkngx=$_bkprocn/ngx/
_bktomcn=$_bkprocn/tomcat/manu/
_bktomcn=$_bkprocn/tomcat/manu/
_bkusrcn=$_bkprocn/usr/
_bkusrcn=$_bkprocn/usr/
_bkscenecn=$_bkprocn/scene/
_bksceneen=$_bkproen/scene/
_bksnapcn=$_bkprocn/tomcat/snap/
_bksnapen=$_bkproen/tomcat/snap/

#function
#Note: the usage function as flows:
#  _function_name  $1  $2  $3
#     $1 stands for the parameter which is needed
#     $2 stands for the parameter which is source path
#     $3 stands for the parameter which is destination path
#     $4 stands for the parameter which is logfile
#
function srv(){
 
}
function bakngx(){
   _ngxfilename=nginx$(date +%Y%m%d%H%M).conf
   echo "Nginx control file name is $_ngxfilename">>$4
   echo "start to backup nginx control file ...">>$4
   cp -rf $2/nginx.conf $3/$_ngxfilename >>$4
   if [[ $? -eq 0 ]] ; then
     echo "nginx backup successfully,the backup file : $3/$_ngxfilename">>$4
     echo "nginx backup finished!">>$4
     return 0
   else
     echo "nginx backup failed!">>$4
     echo "nginx backup finished!">>$4
     return 1
  fi
}
function baktomchn(){
   _tmcn=tomcatchinese$(date +%Y%m%d%H%M).dmp
   echo "Tomcat backup filename is $_tmcn">>$4
   echo "start to backup tomcat">>$4
   tar -czvf $3/$_tmcn $2>>$4
   if [[ $? -eq 0 ]] ; then
      echo "tomcat backup successfully,the backup file : $3/$_tmcn">>$4
      echo "tomcat backup finished!">>$4
      return 0
   else
      echo "tomcat backup failed!">>$4
      return 1
   fi
  
}
function baktomeng(){
   _tmen=tomcatenglish$(date +%Y%m%d%H%M).dmp
   echo "Tomcat backup filename is $_tmen">>$4
   echo "start to backup tomcat">>$4
   tar -czvf $3/$_tmen $2>>$4
   if [[ $? -eq 0 ]] ; then
      echo "tomcat backup successfully,the backup file : $3/$_tmen">>$4
      echo "tomcat backup finished!">>$4
      return 0
   else
      echo "tomcat backup failed!">>$4
      return 1
   fi
 
}
function bakusrchn(){
   _ucndata=usrcndatas$(date +%Y%m%d%H%M).udt
   echo "User data backup filename : $_ucndata">>$4
   echo "start to backup user datas">>$4
   tar -czvf $3/$_ucndata $2 >>$4
   if [[ $? -eq 0 ]] ; then
 echo "user data has been backup successfully, the backup file is $_ucndata">>$4
        echo "user datas backup finished!">>$4
      return 0
   else  
 echo "user datas backup failed!">>$4
     return 1
   fi
}
function bakusreng(){
   _uendata=usrendatas$(date +%Y%m%d%H%M).udt
   echo "User data backup filename : $_uendata">>$4
   echo "start to backup user datas">>$4
   tar -czvf $3/$_uendata $2 >>$4
   if [[ $? -eq 0 ]] ; then
 echo "user data has been backup successfully, the backup file is $_uendata">>$4
        echo "user datas backup finished!">>$4
      return 0
   else  
 echo "user datas backup failed!">>$4
     return 1
   fi

}
function storescene(){
   _scene=scene$(date +%Y%m%d%H%M).scn
   echo "the scene filename is $_scene">>$4
   echo "start to restore current scene">>$4
   tar -czvf $3/$_scene $2 >>$4
   if [[ $? -eq 0 ]] ; then
      echo "the scene has been restored successfully,the scene file is $_scene">>$4
     echo "scene has been restored finished!">>$4
        return 0
   else
    echo "scene has been restored failed!">>$4
   return 0
   fi
}
function crtsnap(){
   _snapshot=snapshot.scn$(date +%Y%m%d).$(date +%H%M).snp
   _ssd=snapshot.scn$(date +%Y%m%d)
   echo "the snapshot name is $_snapshot">>$4
   echo  "start to create snapshot">>$4
   if [[ -d /tmp/snapshot ]] ; then
      rm -rf /tmp/snapshot/* >>$4
      mkdir -p /tmp/snapshot/$_ssd>>$4
   else
      mkdir -p /tmp/snapshot/$_ssd >>$4
   fi
   cp -rf $2 /tmp/snapshot/$_ssd>>$4
   tar -czvf $3/$_snapshot /tmp/snapshot/$_ssd/* >>$4
   if [[ $? -eq 0 ]] ; then
 echo "snapshot created successfully,the file is $3/$_snapshot">>$4
        echo "snapshot create finished!">>$4
     return 0
   else
 echo "snapshot created failed!">>$4
  return 1
   fi
}
function restorescene(){
   case $1 in
 tomcat_cn)
      echo "start to restorescene..."
      echo "start to restorescene...">>$4
     rm -rf /usr/local/project/tomcat_cn>>$4
     tar -xzvf $2 -C /usr/local/project/>>$4
      tar -xzvf $2 -C $3 >>$4
     if [[ $? -eq 0 ]] ; then
  echo "restorescene successfully!">>$4
  return 0
     else
  echo "restorescene failed!">>$4
  return 1
     fi
 ;;
 tomcat_en)
      echo "start to restorescene..."
      echo "start to restorescene...">>$4
     rm -rf /usr/local/project/tomcat_cn>>$4
     tar -xzvf $2 -C /usr/local/project/>>$4
      tar -xzvf $2 -C $3 >>$4
     if [[ $? -eq 0 ]] ; then
  echo "restorescene successfully!">>$4
  echo "restorescene successfully!"
  return 0
     else
  echo "restorescene failed!">>$4
  echo "restorescene failed!"
  return 1
     fi
 ;;
 *)
     echo "usage : restorescene parameter[tomcat_cn|tomcat_en] source_bak_file destination" 
 ;;
   esac
}
function restoresnap(){
     
}


#

 

 

 


#main(string[] args)
_optype=$1
case $_optype in
srv)
  echo "check the status of server running!">>$_logfile
;;
bakngx)
  echo "backup the control file of nginx!">>$_logfile
;;
baktomchn)
   echo "create a image of chinese web site!">>$_logfile
;;
baktomeng)
   echo "create a image of chinese web site!">>$_logfile
;;
bakusrchn)
   echo " backup the datafile of chinese users!">>$_logfile
;;
bakusreng)
   echo " backup the datafile of english users!">>$_logfile
;;
storescene)
   echo "protect scene!">>$_logfile
;;
crtsnap)
   echo " create a web system snapshot!">>$_logfile
;;
restorescene)
   echo "recovery web system scene!">>$_logfile
;;
restoresnap)
   echo "recovery web system snapshot point!">$_logfile
;;
*)
   echo "check system status!"
;;
esac

原创粉丝点击