新しい作業用サーバを建てたときに作業環境を真っ先に設置するためのスクリプトにmovein.sh
と言う便利なのがあります。
オリジナルのスクリプトがgistにあったので掲載します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# movein.sh - shamelessly stolen from O'Reilly's excellend "Linux Server Hacks" | |
# http://oreilly.com/pub/h/72 | |
# Version 0.2 - Added some comments and error checking | |
if [ -z "$1" ]; then | |
echo "Usage: `basename $0` hostname" | |
exit | |
fi | |
if [ ! -d ~/.skel ]; then | |
echo "No ~/.skel directory was found. Please create it and add symlinks (or copies) of any files you want to be included." | |
exit 1 | |
fi | |
# Here's the magic, this tars everything in ~/.skel up and pushes it out to the remote machine. | |
# Be careful to make sure there's nothing in ~/.skel that you don't want on the remote host! | |
cd ~/.skel | |
tar zhcf - . | ssh $1 "tar zpvxf -" |
元ネタはO’ReillyのLinux Server Hacks
~/.skel
に設定ファイルを用意しておいて、movein.sh SSHユーザ@SSHサーバ
を実行すれば接続先にコピーしてもらえると言うもの。~/.skel
は、シンボリックリンクでも実体に変換されるので今使っている.zshrc
とかをメンテナンスフリーでコピーできます。
ただ問答無用に上書きされてしまうので、rsync
つかって確認を出すように書き換えたのが以下のスクリプトです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
commands='scp grep rsync diff mktemp' | |
for c in $commands; do | |
hash $c || { echo "install $c"; exit 255; } | |
done | |
if [ -z "$1" ]; then | |
echo "Usage: $(basename $0) hostname [go|diff]" | |
exit | |
fi | |
if [ ! -d ~/.skel ]; then | |
echo "No ~/.skel directory was found. Please create it and add symlinks (or copies) of any files you want to be included." | |
exit 1 | |
fi | |
hostname=$1 | |
command=$2 | |
src=~/.skel/ | |
dest=${hostname}:./ | |
rsync_options="-rzPc --copy-links --executability --exclude .git" | |
case $command in | |
go) | |
rsync --progress $rsync_options $src $dest | |
;; | |
diff) | |
files=`rsync $rsync_options --dry-run $src $dest |grep -v "/$"` | |
for file in $files; | |
do | |
if [ -r $file ]; then | |
tempfile=$(mktemp) | |
touch ${tempfile} | |
scp ${dest}/${file} ${tempfile} 2>/dev/null | |
diff -uN ${tempfile} ${file} | |
rm ${tempfile} | |
fi | |
done | |
;; | |
*) | |
rsync $rsync_options --dry-run $src $dest |grep -v "/$" | |
while true; do | |
read -p "Do you wish to overwrite? [Y] " yn | |
case $yn in | |
[Yy]* ) rsync --progress $rsync_options $src $dest; break;; | |
* ) exit;; | |
esac | |
done | |
;; | |
esac |
使い方はオリジナルとほぼ同じ、違うのはDo you wish to overwrite? [Y] と聞かれたら差分確認してY
押すだけです。