Remote Backup of Data scripts on multiple servers

Source: Internet
Author: User
Tags scp command

The scenario of backing up data scripts from multiple remote servers is as follows: there are more than 10 foreign servers and several domestic servers, deployment requires that you have to back up the previous version before ant code. After logging on to several machines in China in the morning, the people in Sichuan are suffering, my family also suffered several aftershocks, and people in the distance may be more worried. I tried to execute a remote command through ssh, but I had no choice but to have only the common user permissions. I wrote an sh script, and I could ssh to each machine for backup, but I had to enter the password every time, very depressed. Paste this backup script echo "start dump tomcat source" # config your web project nameproject_name = test # default you can rewrite date_str =$ (date + % Y % m % d) # config pathextdir = '/data/apache-tomcat/webapps/'CD $ extdir # defaultsudo tar-zcvf success $ project_nameecho "dump success the file at" $ extdir $ project_name $ date_str" .tar.gz "this will generate the backup file required by the deployment document in your project folder, then the scp command is used to download the file to 20 machines. An ssh command to execute the script, but every time you enter the password, it hurts a lot, so there is the following lazy script #! /Usr/bin/empty CT-fset time 30 set ipaddr "192.168.1.111" set ipport "212" set username "duplicatedcode" set password "duplicatedcode" set sh_path "/home/duplicatedcode/script/ dumpTomcatSource. sh "spawn/usr/bin/ssh-p $ ipport $ username @ $ ipaddr" sh $ sh_path "exist CT {" yes/no "{send" yes \ r "; exp_continue} "password:" {send "$ password \ r"} login CT "*] #" exit this time, a new problem occurs again. I need to change this file multiple times, ip address, port, user name, password, address, all have the following script dumpF Unction. sh #! /Usr/bin/login CT-fset time 30 set ipaddr [lindex $ argv 0] set ipport [lindex $ argv 1] set username [lindex $ argv 2] set password [lindex $ argv 3] set sh_path [lindex $ argv 4] spawn/usr/bin/ssh-p $ ipport $ username @ $ ipaddr "sh $ sh_path" regular CT {"yes/no" {send "yes \ r "; exp_continue} "password:" {send "$ password \ r"} expect CT "* from *" is actually a method encapsulated, passed in dynamic parameters, can be executed in batches, the following script #! /Bin/bash # config syscoflefile_path = dump. conf # config sh pathsh_dir =/home/duplicatedcode/script/dumpFunction. sh # I # ipaddrfor I in 'awk' {print $1} '$ file_path 'do # ipportj = 'awk-v I = "$ I"' {if (I = $1) print $2} '$ file_path' # usernamek = 'awk-v I = "$ I" '{if (I = $1) print $3} '$ file_path' # passwordl = 'awk-v I = "$ I" '{if (I = $1) print $4} '$ file_path' # sh_pathm = 'awk-v I = "$ I" '{if (I = $1) print $5} '$ file_path' expect CT $ sh_dir $ I $ j $ k $ l $ m done awk used to process files a long time ago, and now it is returned, this sh script must have room for optimization, but it is time-consuming. Let's start with it. Although it is cute, it is easy to read and generally means reading dump. conf file, and then break down the required parameters. A for loop code is executed. The configuration file is 192.168.162.1 222 username password/home/duplicatedcode/script/dumpTomcatSource. sh192.168.162.2 222 username password/home/duplicatedcode/script/dumpTomcatSource. sh192.168.162.3 222 username password/home/duplicatedcode/script/dumpTomcatSource. the format in the sh configuration file is ip port user name password script address, which is so simple, then I run dumpAll on a machine. sh all machines are backed up according to deployment specifications.

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.