Last active
August 29, 2015 14:22
-
-
Save sennajox/1a5a649ff64f28513666 to your computer and use it in GitHub Desktop.
同步任务的示例脚本
This file contains hidden or 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/bash | |
# | |
# 该脚本简单封装了UFile客户端工具filemgr的增量上传功能 | |
# | |
if [ $# -lt 2 ];then | |
echo "usage:$0 bucket syncdir [prefix]" | |
exit 1 | |
fi | |
bucket="$1" | |
syncdir="$2" | |
# 使用sync功能时,被上传的文件默认以相对syncdir的路径作为key(存储空间中的文件名称)。若有特殊需求,可以增加prefix参数构造对应的key | |
prefix="" | |
if [ $# -ge 3 ];then | |
prefix=$3 | |
fi | |
# | |
# filemgr工具"增量上传"基本功能参数说明—— | |
#./filemgr --action sync --bucket bucketname --dir syncdir [--speedlimit speedlimit] [--prefix prefix] [--excludeptn] | |
# | |
# 参数说明: | |
# --bucket : 需要同步至远端的 bucket 名称 | |
# --dir : 需要同步的本地文件夹 | |
# --speedlimit : 上传限速(byte/s) | |
# --prefix : 生成文件的 key 时使用的前缀 | |
# --excludeptn: 需要排除上传的文件模式,支持 POSIX 正则表达式 | |
# | |
# 更详细的说明可参看http://docs.ucloud.cn/ufile/tools.html#id7,"增量上传"部分 | |
# | |
function do_print() { | |
echo "[pid:$$] $1" | |
} | |
function do_sync() { | |
tool="filemgr-linux64" | |
args="--action sync --bucket $bucket --dir $syncdir" | |
if [ -n "$prefix" ];then | |
args="$args --prefix $prefix" | |
fi | |
# 避免同一目录被同时重复执行 | |
pscount=`ps -ef | grep "$tool $args" | egrep -v "grep|vi|less|more|cat|tail|rm" | awk '{print $2}' | wc -l` | |
if [ $pscount -gt 0 ];then | |
do_print "someone is syncing the dir: $syncdir, prefix: $prefix" | |
return 1 | |
fi | |
output=`./$tool $args | egrep 'total\| succ\| fail\|' -A2` | |
# 检查命令结果 | |
# FIXME: 由于$?为grep的返回值,故当文件无变更,工具返回全部已同步时$?也会为非零,暂时忽略这个误判 | |
if [ $? -ne 0 ];then | |
do_print "exec $tool error" | |
return 1 | |
fi | |
# 检查工具输出是否有文件上传失败 | |
fail=`echo $output | awk -F'|' '{printf("%d", $8);}'` | |
if [ $fail -gt 0 ];then | |
do_print "failed count: $fail" | |
return 1 | |
fi | |
do_print "sync all files" | |
return 0 | |
} | |
do_print "`date +%Y%m%d-%H:%M:%S`" | |
do_sync | |
if [ $? -eq 0 ];then | |
exit 0 | |
fi | |
# 等待一段时间后重新执行 | |
sleep 10 | |
do_print "`date +%Y%m%d-%H:%M:%S`" | |
do_sync | |
if [ $? -eq 0 ];then | |
exit 0 | |
fi | |
# 等待一段时间后重新执行 | |
sleep 10 | |
do_print "`date +%Y%m%d-%H:%M:%S`" | |
do_sync | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment