远程备份脚本

backup.sh

#!/bin/bash

 

source /shell/funclib.sh

ftpip=172.30.243.3

des=/tmp/soft

soft=inotify-tools-3.14.tar.gz

file1=funclib.sh

file2=sshrun.sh

file3=inotify-rsync.sh

 

 

#===1===

checkRPM rsync xinetd 

checkSer xinetd

mkdir -p /bak

 

cat << EOF > /etc/rsyncd.conf

motd file=/etc/rsyncd.motd

port=873

address=0.0.0.0

uid=nobody

gid=nobody

read only=no

 

[share]

path=/bak

EOF

 

#===2===

checkRPM openssh openssh-clients openssh-server 

keygen

 

#===3===

mkdir -p /bak/172.30.243.{1,2}

while read ip

   do

keyAuth $ip

   done < ip.txt 

 

#===4===

for i in $(cat ip.txt)

do

  ftpyum $i 

done 

 

#===5===

port 20 21 873

 

#===6===

for i in $(cat /shell/ip.txt)

do

  ssh $i "ls /usr/local/inotify/bin/inotify*"

  if (($?==0))

    then

        echo "$i : $soft has installed"

    else

        echo -e "\n$i : $soft not installed and is going to install..."

        ssh $i "mkdir -p $des"

        scp $soft $i:$des

        scp $file2 $i:$des

        ssh $i "$des/$file2 &>/dev/null &"

  fi

done

 

#===7===

for i in $(cat /shell/ip.txt)

do

  scp $file2 $file3 $i:$des

  ssh $i "$des/$file3 &>/dev/null &"

  ssh $i "echo '$des/$file3' >> /etc/rc.local" 

done

 

 

funclib.sh

 

#!/bin/bash

 

#================= check rpm packege installed or not ================

checkRPM(){

   soft=$(rpm -q $@ | awk '{print $2}')

   if [[ -z $soft ]]

   then 

          echo "all is installed"

   else

        echo "$(echo $soft | tr '\n' ' ') is not installed" 

        for i in $soft

        do 

            if ! rpm -q $i &> /dev/null 

            then 

                echo "please wait a moment,ready to install"

                yum install $i -y $>/dev/null

                if rpm -q $i

                  then 

                     echo "$i install ok"

                  else

                     echo "check problem"

                     exit 1

                fi

            fi

        done 

  fi

}    

 

 

#================= check service is open or close ======================

checkSer(){

for i in $@

   do 

     if ! ls /etc/init.d/$i 1>/dev/null;then  

echo "$i: unrecognized service"

     else

       if service $i status &>/dev/null

   then

        echo "Service $i is OK"

          else

        service $i start 1>/dev/null

        if service $i status &>/dev/null

           then

          echo "Service $i is OK"

           else

       echo "Service $i has problem!"

fi

         fi

    fi

done

}

 

#================== check the listening port ========================

port(){

for i in $@

do

  if lsof -i:$i &> /dev/null

    then

        lsof -i:$i |tail -n +2|tr -s ' '|cut -d' ' -f1,8|sort|uniq > file

        while read ser protocol 

        do

            echo "$ser is listening: $protocol $i port"

        done < file

rm -f file 

    else 

        echo "$i port hasn't been listening"

  fi

done

}

 

#===================== create ssh keys automatically ===============

 

keygen(){

if [[ -f /root/.ssh/id_rsa && -f /root/.ssh/id_rsa.pub ]];then

  echo "ssh key is ok"

else

  echo "ssh key is going to created..."

  /shell/key.exp && echo "create successful"

fi

}

 

 

#========= configure remote host key autentification ======

keyAuth(){

/shell/ssh.exp $1 2>/dev/null

if (($?==0))

   then

       echo -e "\nThere is going to ssh-copy-id\n"

       ./keyAuth.exp $1  

   else

       echo -e "Host $1 has authenticated\n"

fi

}

 

 

#========= configure remote ftp yum resource ===========

ftpyum(){

ssh $1 "ls /etc/yum.repos.d/ftp.repo" &>/dev/null

if (($?!=0));then

  ssh $1 "rm -f /etc/yum.repos.d/*"

  ssh $1 "cat << EOF > /etc/yum.repos.d/ftp.repo

[yum]

name=ftp

baseurl=ftp://$ftpip/yum/Server

enabled=1

gpgcheck=0

EOF"

fi

echo -n -e "$1\t" 

ssh $1 "ls /etc/yum.repos.d/ftp.repo"

}

 

 

#============ remote to install tar packedge ===========

tarInstall(){

soft=inotify-tools-3.14.tar.gz  

des=/tmp/soft

file=sshrun.sh

 

for i in $(cat /shell/ip.txt)

do

  ssh $i "ls /usr/local/inotify/bin/inotify*"

  if (($?==0))

    then

        echo "$i : $soft has installed"

    else 

        echo -e "\n$i : $soft not installed and is going to install..."

        ssh $i "mkdir -p $des"

        scp $soft $i:$des 

        scp $file $i:$des  

        ssh $i "$des/$file"

  fi

done 

 

#the text below is the $file

 

soft=inotify-tools-3.14.tar.gz

tar xvf /tmp/soft/$soft -C /tmp/soft 1>/dev/null 

 

rpm -q gcc gcc-c++ make 1>/dev/null

if (($?!=0))

  then 

      echo -n "compile environment is not normal"

      yum install gcc* make -y 1>/dev/null && echo ",install ok"

  else

      echo "compile environment is OK" 

fi   

 

cd /tmp/soft/inotify-tools-3.14/ && ./configure --prefix=/usr/local/inotify 1>/dev/null # Notice: if install other package to change directory 

 

if (($?==0))

  then

      echo "Make configure file Ok and is going to compiling"

  else

      echo "Make configure file failure" && exit 1

fi

 

make 1>/dev/null 

 

if (($?==0))

  then

      echo "Compiled OK"

  else 

      echo "Compiled failure,Check problem!!" && exit 1

fi

 

make install 1>/dev/null

 

if (($?==0))

  then 

      echo "Installed OK"

  else 

      echo "Installed failure,check problem!!" 

fi 

}

 

 

key.expe

#!/usr/bin/expect

spawn ssh-keygen

expect "(/root/.ssh/id_rsa):"

send "\r"

expect "(empty for no passphrase):"

send "\r"

expect "Enter same passphrase again:"

send "\r"

expect eof

 

 

keyAuth.expe

 

#!/usr/bin/expect

set ip [lindex $argv 0]

spawn ssh-copy-id root@$ip

expect "*yes/no*" {send "yes\r";exp_continue} "*password:" {send "aixocm\r"}

expect eof

 

 

ssh.expe

#!/usr/bin/expect

set ip [lindex $argv 0]

spawn ssh $ip ls

expect "*yes/no*" {send "yes\r";exp_continue} "*password:" {send "aixocm\r"}

expect eof

 

 

ssh.run

 

#!/bin/bash

 

soft=inotify-tools-3.14.tar.gz

 

tar xvf /tmp/soft/$soft -C /tmp/soft 1>/dev/null 

 

rpm -q gcc gcc-c++ make 1>/dev/null

if (($?!=0))

  then 

      echo -n "compile environment is not normal"

      yum install gcc* make -y 1>/dev/null && echo ",install ok"

  else

      echo "compile environment is OK" 

fi   

 

cd /tmp/soft/inotify-tools-3.14/ && ./configure --prefix=/usr/local/inotify 1>/dev/null 

 

if (($?==0))

  then

      echo "Make configure file Ok and is going to compiling"

  else

      echo "Make configure file failure" && exit 1

fi

 

make 1>/dev/null 

 

if (($?==0))

  then

      echo "Compiled OK"

  else 

      echo "Compiled failure,Check problem!!" && exit 1

fi

 

make install 1>/dev/null

 

if (($?==0))

  then 

      echo "Installed OK"

  else 

      echo "Installed failure,check problem!!" 

fi 

 

 

 

inotify-rsync.sh

 

#!/bin/bash

source /tmp/soft/funclib.sh

host=172.30.243.1

src=/backup

des=$(ifconfig eth0 |grep "inet addr"|tr -s ':' ' '|awk '{print $3}')

checkRPM rsync xinetd

checkSer xinetd

chkconfig rsync on

/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib $src | while read files  

do

/usr/bin/rsync -av --delete $src rsync://$host/share/$des

done 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值