代码拉取完成,页面将自动刷新
同步操作将从 SmallSaaS/jenkins_scripts 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/env bash
#1 cp local file to local file pass
#2 cp local file to local dir pass
#3 cp local dir to local dir pass
#4 cp remote file to local dir
#5 cp remote file to local file
#6 cp remote dir to local dir
#7 cp local file to remote file
#8 cp local file to remote dir
#9 cp local dir to remote dir
#10 cp remote file to remote file
#11 cp remote file to remote dir
#12 cp remote dir to remote dir
usage(){
echo 'scpifsum.sh [OPTION] <src:pi@kunsh04:/path/to/from> <tar:pi@kunsh05:/path/to/target>'
echo 'OPTION'
echo ' -s - sanity to ssh connect'
echo ' -r - same with scp -r'
exit -1
}
##############################
## etcd api
##############################
etcd_get(){
local key=$1
ETCDCTL_API=3 etcdctl --endpoints=http://etcd.webtools.io:2379 get $key
}
etcd_put(){
local key=$1
local val=$2
ETCDCTL_API=3 etcdctl --endpoints=http://etcd.webtools.io:2379 put $key $val
}
etcd_put_v2(){
local val=$1
local key=$2
ETCDCTL_API=3 etcdctl --endpoints=http://etcd.webtools.io:2379 put $key $val
}
etcd_del(){
local key=$1
ETCDCTL_API=3 etcdctl --endpoints=http://etcd.webtools.io:2379 del $key
}
etcd_get_checksum(){
if [ ! -f $path_key ];then
# echo "$path_key file not exist, remove the entry from etcd !" > /dev/stderr
etcd_del $path_key
return
fi
local path_key=$1
local checksum_log=$(etcd_get $path_key)
#for it in ${checksum[@]};do
# echo it=$it
#done
#echo $checksum_log > /dev/stderr
## if no checksum, init it
if [ -z "$checksum_log" ];then
checksum_log=$(md5sum "$path_key")
if [ ! -z "$checksum_log" ];then
etcd_put_v2 $checksum_log > /dev/null
checksum_log=$(etcd_get $path_key)
fi
fi
## get checksum
local checksum_list=($checksum_log)
echo ${checksum_list[1]}
}
#etcd_get_checksum /mnt/wingo/network_tools/local/bin/tar-snapshot.sh
log_sum(){
local name=$1
local line=$2
log_file=scpifsum-$name-$(date "+%Y-%m-%d").log
if [ ! -f $log_file ];then
echo $line > $log_file
else
echo $line >> $log_file
fi
}
## check options
from_opt=
target_opt=
#opts
check_sanity_opt=
cp_dir_opt=
#loop
for opt in $@;do
if [ "$opt" = "-h" ];then
usage
elif [ "$opt" = "-c" ];then
check_sanity_opt=$opt
elif [ $opt = '-r' ];then
cp_dir_opt=$opt
elif [ $from_opt ];then
target_opt=$opt
else
from_opt=$opt
fi
done
if [ ! $target_opt ];then
usage
fi
if [ -L $from_opt ];then
from_opt=$(readlink -f $from_opt)
fi
if [ -L $target_opt ];then
target_opt=$(readlink -f $target_opt)
fi
from_opt=${from_opt%/}
target_opt=${target_opt%/}
# echo from_opt=$from_opt, target_opt=$target_opt
## main
local_file_local_file(){
local from=$1
local target=$2
local log_name=$3
if [ ! -f "$from" ];then
echo $from is not a file ! > /dev/stderr
exit
fi
## if target not exits
if [ ! -f "$target" ];then
## get path
target_root=${target%/*}
if [ ! -d "$target_root" ];then
mkdir -p "$target_root"
fi
echo "=> $from $target"
cp "$from" "$target"
if [ $? = 0 ];then
checksum_n=$(etcd_get_checksum "$target")
log_sum $log_name "=> $from $target $checksum_n"
else
exit
fi
return
fi
checksum_log=$(md5sum "$from")
checksum_list=($checksum_log)
checksum=${checksum_list[0]}
# sum=${checksum%% *}
#checksum2=$(md5sum "$target")
checksum2=$(etcd_get_checksum "$target")
if [ "$checksum" = "$checksum2" ];then
## skip
# echo $sum > /dev/null
echo > /dev/null
else
echo ">> $from $target"
cp "$from" "$target"
if [ $? = 0 ];then
etcd_put "$target" $sum ## update new checksum
log_sum $log_name ">> $from $target $sum"
else
exit
fi
fi
}
##############################
## cp main
##############################
cp_local_file_local_file(){
local_file_local_file "$from_opt" "$target_opt"
}
cp_local_file_local_dir(){
checksum=$(md5sum "$from_path")
sum=${checksum% *}
filename=${from_path##*\/}
t_path="$target_path/$filename"
t_checksum=$(md5sum "$t_path")
t_sum=${t_checksum% *}
if [ "$sum" = "$t_sum" ];then
## skip
echo skip! $from_path $target_path
else
cp $from_path $target_path
fi
}
cp_local_dir_local_dir(){
local from_path=$1
local target_path=$2
local from_dir=${from_path%/}
from_dir=${from_dir##*/}
## for file with space
local real_it=
# echo from_path=$from_path, target_path=$target_path, from_dir=$from_dir
list=$(find $from_path -type f)
for it in ${list[@]};do
## check if part of file path, means file name with space
if [ ! -f "$real_it" ];then
real_it="$real_it $it"
## trim empty
real_it=${real_it% }
real_it=${real_it# }
if [ ! -f "$real_it" ];then
continue
fi
fi
it="$real_it"
relative_path=${it#$from_path}
relative_path=${relative_path#\/}
relative_path=$from_dir/$relative_path
new_path=${target_path%/}
new_path=${target_path%/$from_dir} ##remove the same dir from target
new_path="$new_path/$relative_path"
## cp file to file
#echo $relative_path $new_path
local log_name=$from_dir
# echo local_file_local_file "$it" "$new_path"
local_file_local_file "$it" "$new_path" $log_name
unset real_it
done
}
## cp file to file
# echo from_opt=$from_opt, target_opt=$target_opt
if [ -e $from_opt ];then
if [ -f $from_opt -a -f $target_opt ];then
cp_local_file_local_file
elif [ -f $from_opt -a -d $target_opt ];then
cp_local_file_local_dir
#elif [ -d $from_opt -a -d $target_opt ];then
elif [ -d $from_opt ];then
if [ ! -d $target_opt ];then
mkdir -p $target_opt
fi
cp_local_dir_local_dir "$from_opt" "$target_opt"
fi
exit
fi
exit
############################################
## below for remote sync
############################################
parse_ssh_path(){
line=$1
path=${line#*:}
echo $path
}
parse_ssh_host(){
line=$1
path=${line#*:}
host=${line%:*}
if [ "$host" = "$path" ];then
unset host
fi
echo $host
}
check_sanity(){
local from=$1
local target=$2
local host=$(parse_ssh_host $from)
if [ $host ];then
ssh $host ls
fi
## target
host=$(parse_ssh_host $target)
if [ $host ];then
ssh $host ls
fi
}
if [ ! $target_path ];then
usage
elif [ $check_sanity_opt ];then
check_sanity $from_path $target_path
exit
fi
print_checksum_files(){
line=$1
## dir or file
dir_or_not=$2
if [ -d $line ];then
dir_or_not=$line
fi
host=$(parse_ssh_host $line)
path=$(parse_ssh_path $line)
#check empty
if [ -z "$(ls $path)" ];then
return
fi
local run_bin=
if [ $dir_or_not ];then
## relative path
run_bin="cd $path && find -type f -not -path $path | xargs md5sum"
# TODO
# repl=$(echo $path | sed -e 's/\//\\\//g')
# find $path -type f | sed -e "s/$repl//"
elif [ -f $path ];then
## abs path, convert to realtive path
local t_path=${path%/*}
local t_file=${path##*/}
t_file="./$t_file"
cd $t_path
run_bin="md5sum $t_file"
fi
if [ $host ];then
# echo ssh $host "\"$run_bin\""
ssh $host "$run_bin"
else
eval $run_bin
fi
}
## declare two hash
declare -A from_hash
declare -A target_hash
## create the hash from input lines
create_from_hash(){
local _lines=("$@")
# for line in ${_lines[@]};do
# echo hash_line=$line
# done
local new_line=
for line in ${_lines[@]};do
if [ $new_line ];then
from_hash[$line]=$new_line
unset new_line
else
new_line=$line #checksum
fi
done
# for i in "${!from_hash[@]}";do
# echo "key : $i"
# echo "value: ${from_hash[$i]}"
# done
}
## create the hash from input lines
create_target_hash(){
local _lines=("$@")
# for line in ${_lines[@]};do
# echo hash_line=$line
# done
local new_line=
for line in ${_lines[@]};do
if [ $new_line ];then
target_hash[$line]=$new_line
unset new_line
else
new_line=$line #checksum
fi
done
# for i in "${!target_hash[@]}";do
# echo "key : $i"
# echo "value: ${target_hash[$i]}"
# done
}
# create hash from array
unset lines
for line in $(print_checksum_files $from_path $cp_dir_opt);do
lines+=($line)
done
create_from_hash ${lines[@]}
unset lines
for line in $(print_checksum_files $target_path $cp_dir_opt);do
lines+=($line)
done
create_target_hash ${lines[@]}
## print
# for i in "${!from_hash[@]}";do
# echo "key : $i"
# echo "value: ${from_hash[$i]}"
# done
# for i in "${!target_hash[@]}";do
# echo "key : $i"
# echo "value: ${target_hash[$i]}"
# done
sync_checksum(){
local syncing=$1
local checksum=$2
# TODO, check ssh
# echo scp $from_path/$syncing $target_path
cp "$syncing" "$target_path"
}
print_not_match_files(){
local checksum=
for i in "${!from_hash[@]}";do
checksum=${from_hash[$i]}
t_CHECKSUM=${target_hash[$i]}
if [ "$checksum" != "$t_CHECKSUM" ];then
sync_checksum $i $t_CHECKSUM
#else
#echo skip $i $checksum
fi
done
}
## final process checksum
# declare -p $from_hash
# declare -p $target_hash
print_not_match_files
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。