远程同步与实现重启备份的shell脚本

发布时间:2020-04-19编辑:脚本学堂
分享一个shell脚本,实现远程同步,并可以重启备份等操作。不错的shell脚本,有需要的朋友,可以好好研究下。

1,远程同步脚本,代码如下:
 

复制代码 代码示例:
#!/bin/bash
#remote rsync
#by www.jb200.com
if [ "$1" == "" ];then
    echo "请输入你要进行的操作:{backup start stop restart rsync}"
elif ([ "$1" != "start" ] && [ "$1" != "stop" ] && [ "$1" != "restart" ] && [ "$1" != "backup" ] && [ "$1" != "rsync" ]);then
    echo "请输入你要进行的操作:{backup start stop restart}"
elif([ "$1" == "rsync" ]);then
    /usr/bin/rsync  -avzogP --stats --progress  --include="webapps"  --exclude="/*"  --password-file=/usr/local/rsync/backup.secrets /usr/local/tomcat/ rsync@192.168.8.100::webapps
else
   #echo "/var/www/tomcat_test $1" > /usr/local/tomcat/a
   echo "/var/www/tomcat_m_20021 $1" > /usr/local/tomcat/a
   /usr/bin/rsync   -avzogP --stats --progress  --password-file=/usr/local/rsync/backup.secrets --include='a' --exclude='/*'  /usr/local/tomcat/ rsync@192.168.8.100::rsync
fi

2,服务端重启备份脚本,代码如下:
 

复制代码 代码示例:
#!/bin/sh
#kill tomcat pid
#by www.jb200.com
Command_file="/usr/local/tomcat/a"
Project_name=`cat $Command_file |linuxjishu/13830.html target=_blank class=infotextkey>awk '{print $1}'`
Command=`cat $Command_file |awk '{print $2}'`
echo $Command
if  [ -n "$Command" ];then
case "$Command" in
start)
   export JAVA_HOME=/usr/local/java/jdk1.6.0_23
   cd $Project_name
   rm -rf work/*
   $Project_name/bin/startup.sh
   `/bin/cat /dev/null >$Command_file`
   echo "启动成功!"
   ;;
stop)
  pidlist=`ps -ef|grep $Project_name|grep -v "grep" |grep -v "tomcat_restart.sh" |awk '{print $2}'`
  echo "tomcat Id list :$pidlist"
  kill -9 $pidlist
  echo "KILL $pidlist:"
  echo "service stop success"
  `/bin/cat /dev/null >$Command_file`
  echo "stop success ! "
  ;;
restart)
  export JAVA_HOME=/usr/local/java/jdk1.6.0_23
  pidlist=`ps -ef|grep $Project_name|grep -v "grep" |grep -v "tomcat_restart.sh" |awk '{print $2}'`
  echo "tomcat Id list :$pidlist"
  kill -9 $pidlist
  echo "KILL $pidlist:"
  echo "service stop success"
  echo "start tomcat"
  cd $Project_name
  rm -rf work/*
  cd bin
  ./startup.sh
  `/bin/cat /dev/null >$Command_file`
  echo "重启成功!"
  ;;
backup)
  cd $Project_name
  `/bin/find  -type d  -name "webapps_[0-9]*" -mtime +10 | xargs rm -fr`
  my_Back="webapps_backup"
  datetime=`date "+%m-%d-%H-%M"` 
  echo $datetime 
  echo $my_Back
  if [ ! -d "$my_Back" ]; then
     `/bin/mkdir $my_Back`
  fi
  /bin/mv webapps  $my_Back/webapps_$datetime
  `/bin/cat /dev/null >$Command_file`
  echo "备份成功!“
  ;;
*)
 echo "Usage: $0 {start|stop|restart}"
 cat /dev/null >$Command_file
 exit 1
 esac
 exit 0
fi