Shell batch multiple server data copy

Source: Internet
Author: User
Tags rsync

  1. Experiment Server Two:

    192.168.77.189

    192.168.77.190

  2. Server-free key login settings:

    [email protected]_168_77_189 ~]# Ssh-keygen
    Generating public/private RSA key pair.
    Enter file in which to save the key (/ROOT/.SSH/ID_RSA):
    Enter passphrase (empty for no passphrase):
    Enter same Passphrase again:
    Your identification has been saved In/root/.ssh/id_rsa.
    Your public key has been saved in/root/.ssh/id_rsa.pub.
    The key fingerprint is:
    95:cf:19:a5:ff:d0:bc:80:f5:de:5a:4c:b5:63:ae:c6 [Email protected]_168_77_189
    The key ' s Randomart image is:
    +--[RSA 2048]----+
    |  . |
    | . o |
    |  o O. .|
    | . oo+.oo|
    | S. +.o=+|
    | ==+|
    |  . +=|
    | E.O |
    | ...  |
    +-----------------+
    [[Email protected]_168_77_189 ~]#ssh-copy-id-i/root/.ssh/id_rsa.pub 192.168.77.190
    [email protected] ' s password:
    Now try logging to the machine, with "ssh ' 192.168.77.190 '", and check in:

    . Ssh/authorized_keys

    To make sure we haven ' t added extra keys so you weren ' t expecting.

    [[Email protected]_168_77_189 ~]# ssh 192.168.77.190
    Last Login:sun Jul 5 23:16:20 from 192.168.77.100
    [Email protected]_168_77_190 ~]# Clear
    [[Email protected]_168_77_190 ~]#
    [[Email protected]_168_77_190 ~]#
    [Email protected]_168_77_190 ~]# exit

  3. Copy Script

#!/bin/bash#write by lixi#time by 2015-7-7  #run  by sh auto.shell.sh  /root /mntif    [ ! -f ip.txt ] ; thenecho - e  "\033[32mplease create ip.txt files...\033[0m" cat <<  eof192.168.149.128192.168.149.129eof        exitfiif  [  -z  "$"  ] ; then        echo -e  "\ 033[32musage $0 command,example{src_file|src_dir des_dir\033[0m} "         exitficount= ' Cat ip.txt | wc -l ' rm -rf ip.txt.swpi=0while    (i< $count) doi= ' expr  $i  + 1 ' sed  "${i}s/^/&${i} /g"    ip.txt >> ip.txt.swpip= ' awk -v i= ' $i   ' {if (i==$1) print $2} '   Ip.txt.swp ' Scp -r $1 [email protecTed]${ip}:$2#rsync -ap --delete $1 [email protected]${ip}:$2done 

The above script with rsync can also,,, rsync sync two files like,,, such as SH auto.shell.sh/opt//opt/keep two servers between this directory file like

4. Batch remote server execution commands

#!/bin/bash# auto run server commend# by lx 2015-7-7if [ ! - f ip.txt ]; then        echo -e  "\033[ 32mplease create ip.txt files...\033[0m "cat <<  eof192.168.149.128192.168.149.129eof        exitfiif          [ -z  "$*"  ] ; then         echo -e  "\033[32musage: $0 commend,example{rm /tmp/text.txt|mkdir  /tmp/text2}\033[0m "        exitfi         count= ' Cat ip.txt | wc -l '          rm -rf ip.txt.swp        i=0while  ((i< $count)) Do        i= ' Expr  $i  + 1 '         sed  "${i}s/^/&${i} /g"  ip.txt >>ip.txt.swp        ip= ' awk -v I= ' $i "  ' {if (i==$1) print $2} '  ip.txt.swp ' ssh -q -l root  $IP  " $*; echo  -e  '-----------------------\nthe  $IP  exec command:$* success ! '; Sleep 2 "Done
[Email protected]_168_77_189 ~]# sh auto_ssh.sh mkdir/tmp/text2-----------------------the 192.168.77.190 Exec command: MKDIR/TMP/TEXT2 Success!-----------------------the 192.168.77.189 Exec command:mkdir/tmp/text2 success!

5. Server before file synchronization:

#!/bin/bash
#by Lixi 2015.7.7

#run by SH auto_rsync.sh flush

Flush ()
{
If
[!-f rsync.list]; Then
Echo-e "\033[32mplease Create Rsync.list file...\033[0m"
Cat << EOF
192.168.149.128 Src_dir Des_dir
192.168.149.129 Src_dir Des_dir
Eof
Exit
Fi
RM-RF RSYNC.LIST.SWP; Cat Rsync.list | Grep-v "#" &GT;RSYNC.LIST.SWP
Count= ' Cat rsync.list.swp|wc-l '
Num=0
while ((${num} < $COUNT))
Do
num= ' expr $NUM + 1 '
Line= ' Sed-n "${num}p" Rsync.list.swp '
Src= ' echo $Line |awk ' {print $} '
Des= ' echo $Line |awk ' {print $} '
Ip= ' echo $Line |awk ' {print $} '
Rsync-ap--delete ${src}/[email protected]${ip}:${des}/
Done
}
Restart ()
{
RM-RF RESTART.LIST.SWP; Cat Restart.list | Grep-v "#" >> restart.list.swp
Count= ' Cat restart.list.swp|wc-l '
Num=0
while ((${num} < $COUNT))
Do
num= ' expr $NUM + 1 '
Line= ' Sed-n "${num}p" Restart.list.swp '
Command= ' echo $Line |awk ' {print $} '
Ip= ' echo $Line |awk ' {print $} '
Ssh-l root $IP "sh $Command; Echo-e '----------------\nthe $IP Exec commadn:sh $Command success! ' "
Done
}
Case $ in
Flush)
Flush
;;
Restart)
Restart
;;
* )
Echo-e "\033[32musage: $ command,example {flush|restart}\033[0m"
;;
Esac


This article is from the "Fluffy Duck" blog, please be sure to keep this source http://woshitieren.blog.51cto.com/2466034/1671863

Shell batch multiple server data copy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.