Skip to content

Instantly share code, notes, and snippets.

@fukajun
Last active September 6, 2019 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukajun/762f55261e261ecf54c6a07f3e581eb0 to your computer and use it in GitHub Desktop.
Save fukajun/762f55261e261ecf54c6a07f3e581eb0 to your computer and use it in GitHub Desktop.

Setup

envchain --set isucon IKA_USER IKA_HOSTS

IKA_USER : fukajun

IKA_HOSTS : isu1,isu2,isu3

config

edit .ssh/config

Host isu1
  HostName 10.0.0.1
  User ec2-user
  IdentityFile ~/.ssh/id_rsa.isucon.pem
  Port 22
Host isu2
  HostName 10.0.0.2
  User ec2-user
  IdentityFile ~/.ssh/id_rsa.isucon.pem
  Port 22
Host isu2
  HostName 10.0.0.3
  User ec2-user
  IdentityFile ~/.ssh/id_rsa.isucon.pem
  Port 22

Usage

local and all hosts setup dir

envchain isucon ./bin/ika_sync init

upload to all hosts from local IKA_USER dir

envchain isucon ./bin/ika_sync up

download from all hosts to local IKA_USER dir

envchain isucon ./bin/ika_sync down

download from all hosts all user to local dir

envchain isucon ./bin/ika_sync down_all

First step

  1. init
  2. up
#!/bin/bash
check() {
if [ "${1}" == '' ]; then
echo "Please set env: export $2='$3'"
exit 1
fi
}
check "${IKA_USER}" "IKA_USER" "fukajun|negipo|0tany|etc"
check "${IKA_HOSTS}" "IKA_HOSTS" "isu1,isu2,isu3"
target="${IKA_USER}"
binpath=$(cd $(dirname $0) && pwd)
repopath=.
ssh_cmd="ssh -p 22"
cd $repopath
run(){
type=$1
host_no=$2
local_root="${repopath}/sync_${host_no}"
remote_dir="~/sync"
remote_root="${host_no}:${remote_dir}"
local="${local_root}/${target}"
remote="${remote_root}/${target}"
echo $local_root
echo $local
echo $remote_root
echo $remote
if [ "${type}" = "up" ]; then
rsync -avz -e "${ssh_cmd}" --delete "${local}/" "${remote}"
elif [ "${type}" = "init" ]; then
mkdir -p ${local}
$ssh_cmd $host_no "mkdir -p $remote_dir"
elif [ "${type}" = "down" ]; then
rsync -avz -e "${ssh_cmd}" --delete "${remote}/" "${local}"
elif [ "${type}" = "down_all" ]; then
rsync -avz -e "${ssh_cmd}" --delete "${remote_root}/" "${local_root}"
else
echo 'Specify up or down or down_all'
fi
}
hosts=`echo $IKA_HOSTS | sed 's/,/ /g'`
for host in $hosts;do
run "$1" $host
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment