简体   繁体   English

使用Shell脚本自动进行SCP网站文件传输

[英]Automate scp website file transfer using a shell script

i have 3 digital ocean droplets, how to automate one droplet files to another 2 droplets, so i was write a scp shell script. 我有3个数字海洋小滴,如何将一个小滴文件自动转换为另外2个小滴,所以我写了一个scp shell脚本。 guys do you have any other suggestions or easily file transfer between two droplets? 伙计们,您还有其他建议吗?还是可以轻松地在两个小滴之间传输文件?

#! /usr/bin/env bash

# Variables
SERVER1USER=root
SERVER1PSWD=killergirls
SERVER1IP=123.123.123.123
SERVER1PATH=/etc/var/html/www/

SERVER2USER=root
SERVER2PSWD=killerboys
SERVER2IP=121.121.121.121
SERVER2PATH=/etc/var/html/www/

echo -e "\n--- First Server files transferring now... ---\n"
sshpass -p "$SERVER1PSWD" scp -r /etc/var/html/www/ SERVER1USER@SERVER1IP:$SERVER1PATH

echo -e "\n--- Second Server files transferring now... ---\n"
sshpass -p "$SERVER2PSWD" scp -r /etc/var/html/www/ SERVER2USER@SERVER2IP:$SERVER2PATH

I prefer to program things like this in python. 我更喜欢在python中编写类似的程序。 Below I have a sample of one script that I use to deploy hadoop to a cluster. 下面有一个脚本示例,用于将hadoop部署到集群。 It can be expanded easily by adding more files, commands, or ips. 通过添加更多文件,命令或ips,可以轻松扩展它。

Note that I like being able to print everything that is going to be executed before I actually run it. 请注意,我喜欢能够在实际运行之前打印将要执行的所有内容。 This is done with the DEBUG variable. 这是通过DEBUG变量完成的。

Also note, this can be accomplished in bash, but since I am not an accomplished Bash programmer, Python is much easier and less error prone for me. 还要注意,这可以在bash中完成,但是由于我不是一个经验丰富的Bash程序员,所以Python对我来说更加容易,而且出错的可能性也较小。

DEBUG=1
import subprocess
call = subprocess.call
ips = []
for i in range(0,10):
    ips.append("10.0.0.%d"%(i))
commands = 'ls; echo "Hello"'
def hadoop_setup(ip):
        sshTemplate = "ssh hdfs@%s '%s;'"
        for eachCommand in commands.split(";"):
                sshCall = sshTemplate%(ip, eachCommand)
                if DEBUG: print sshCall
                else: call(sshCall, shell=True)


copyFileTemplate = 'scp %s hdfs@%s:/home/hdfs'
for file in ['.bashrc']:
    for ip in ips:
        copyFileCall = copyFileTemplate%(file,ip)
        call(copyFileCall, shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM