Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
user="root"
source=""
dest=""
usage(){
echo "Usage is $0 [-u <ssh user ($user)> ] -s <local source> [-d <remote destination>] <server lists file >"
}
dest=""
while getopts u:d:s:h opt;do
case $opt in
u)
user=$OPTARG
;;
d)
dest=$OPTARG
;;
h)
usage
exit 0
;;
s)
source=$OPTARG
;;
esac
done
shift $((OPTIND -1))
if [ ! -e "$1" ];then
echo "## you must specify a valid list of servers file"
exit 1
fi
if [ ! -e "$source" ];then
echo "## you must specify an existing binary, \"$source\" doesn't exists"
exit 1
fi
list=`cat $1`
md5=`md5sum $source | cut -d ' ' -f 1`
base_source=`basename $source`
echo "* md5: $md5"
for s in $list;do
if [ -n "$dest" ];then
rmd5=`ssh $user@$s "md5sum $dest/$base_source|cut -d ' ' -f 1"`
else
rmd5=`ssh $user@$s "md5sum $base_source|cut -d ' ' -f 1"`
fi
if ! [ "$rmd5" == "$md5" ];then
scp $source $user@$s:$dest >& /dev/null &
echo "* copying $source in $user@$s:$dest id $! ..."
else
echo "* skipping copy an existing copy esists with the same MD5 $rmd5"
fi
done
error=0
for job in `jobs -p`;do
echo "* waiting finishing id $job"
wait $job || let "error+=1"
if [ $error != "0" ] ;then
echo "## error copying $error"
fi
done
if [ $error == "0" ] ;then
echo "* successfully copied"
else
exit 1
fi