3 # - script to sync a group of files on alien with a local cache
4 # downloads only new and updated files
5 # - by default it mirrors the directory structure in a specified local location
6 # (the local chache location and paths can be manipulated.)
7 # - needs a configured config file (by default alienSync.config)
8 # and a working alien environment (token and at least $ALIEN_DIR or $ALIEN_ROOT set)
9 # - can be also used without a config file
11 # run the script without argument to see the examples
13 # origin: Mikolaj Krzewicki, mikolaj.krzewicki@cern.ch
15 if [ ${BASH_VERSINFO} -lt 4 ]; then
16 echo "bash version >= 4 needed, you have ${BASH_VERSION}, exiting..."
22 if [[ $# -lt 1 ]]; then
23 echo "Usage: ${0##*/} configFile=/path/to/config"
24 echo "expert: ${0##*/} alienFindCommand=\"alien_find /some/path/ file\" [opt=value]"
25 echo " ${0##*/} alienFindCommand=\"alien_find /some/path/ file\" localPathPrefix=\${PWD}"
27 echo "by default files are downloaded to current dir, or \${alienSync_localPathPrefix}, if set."
28 echo "At least specify alienFindCommand, either on command line or in the configFile."
29 echo "the logs go by default to localPathPrefix/alienSyncLogs"
33 #be nice and allow group members access as well (002 will create dirs with 775 and files with 664)
36 # try to load the config file
37 #[[ ! -f $1 ]] && echo "config file $1 not found, exiting..." | tee -a $logFile && exit 1
38 if ! parseConfig "$@"; then return 1; fi
40 [[ -z ${alienFindCommand} ]] && echo "alienFindCommand not defined!" && return 1
42 #if not set, use the default group
43 [[ -z ${alienSyncFilesGroupOwnership} ]] && alienSyncFilesGroupOwnership=$(id -gn)
46 [[ ! -d $logOutputPath ]] && echo "logOutputPath not available, creating..." && mkdir -p $logOutputPath && chgrp ${alienSyncFilesGroupOwnership} ${logOutputPath}
47 [[ ! -d $logOutputPath ]] && echo "could not create log dir, exiting..." && exit 1
48 dateString=$(date +%Y-%m-%d-%H-%M)
49 logFile=$logOutputPath/alienSync-$dateString.log
50 echo "$0 $@"|tee -a $logFile
51 echo ""|tee -a $logFile
55 lockFile=$logOutputPath/runningNow.lock
56 [[ -f $lockFile && ${allowConcurrent} -ne 1 ]] && echo "locked. Another process running? ($lockFile)" | tee -a $logFile && exit 1
58 [[ ! -f $lockFile ]] && echo "unable to create lock. exiting..." | tee -a $logFile && exit 1
60 #redirect all output to a log
61 if [[ $allOutputToLog -eq 1 ]]; then
66 newFilesList=$logOutputPath/"newFiles.list"
69 redoneFilesList=$logOutputPath/"redoneFiles.list"
70 rm -f $redoneFilesList
71 touch $redoneFilesList
72 updatedFilesList="${logOutputPath}/updatedFiles.list"
73 failedDownloadList="${logOutputPath}/failedDownload.list"
74 touch ${failedDownloadList}
78 [[ -z $alienFindCommand ]] && echo "alienFindCommand not defined, exiting..." && exitScript 1
79 [[ -z ${localPathPrefix} ]] && echo "localPathPrefix not defined, exiting..." && exitScript 1
80 [[ -z $logOutputPath ]] && echo "logOutputPath not defined, exiting..." && exitScript 1
81 [[ -z $secondsToSuicide ]] && echo "setting default secondsToSuicide of 10 hrs..." && secondsToSuicide=$(( 10*3600 ))
84 [[ -z $ALIEN_ROOT && -n $ALIEN_DIR ]] && ALIEN_ROOT=$ALIEN_DIR
85 #if ! haveAlienToken; then
86 # $ALIEN_ROOT/api/bin/alien-token-destroy
87 $ALIEN_ROOT/api/bin/alien-token-init $alienUserName
89 #if ! haveAlienToken; then
90 # if [[ $allOutputToLog -eq 1 ]]; then
93 # echo "problems getting token! exiting..." | tee -a $logFile
96 #ls -ltr /tmp/gclient_env_$UID
97 #cat /tmp/gclient_env_$UID
98 source /tmp/gclient_env_$UID
100 #set a default timeout for grid access
101 [[ -z $copyTimeout ]] && copyTimeout=600
102 export GCLIENT_COMMAND_MAXWAIT=$copyTimeout
104 localAlienDatabase=$logOutputPath/localAlienDatabase.list
105 localFileList=$logOutputPath/localFile.list
107 alienFileListCurrent=$logOutputPath/alienFileDatabase.list
108 [[ ! -f $localFileList ]] && touch $localFileList
109 candidateLocalFileDatabase=$logOutputPath/candidateLocalFileDatabase.list
111 #here we produce the current alien file list
112 if [[ -n ${useExistingAlienFileDatabase} && -f ${localAlienDatabase} ]]; then
114 echo "using ${localAlienDatabase} instead of full alien search"
115 echo cp -f ${localAlienDatabase} ${alienFileListCurrent}
116 cp -f ${localAlienDatabase} ${alienFileListCurrent}
119 echo "eval $alienFindCommand > $alienFileListCurrent"
120 eval "$alienFindCommand" > $alienFileListCurrent
123 echo "number of files in the collection: $(wc -l $alienFileListCurrent)"
124 #create a list of candidate destination locations
125 #this is in case there are more files on alien trying to get to the same local destination
126 #in which case we take the one with the youngest ctime (later in code)
127 if [[ -n ${destinationModifyCommand} ]]; then
128 echo eval "cat $alienFileListCurrent | ${destinationModifyCommand} | sed \"s,^,${localPathPrefix},\" > ${candidateLocalFileDatabase}"
129 eval "cat $alienFileListCurrent | ${destinationModifyCommand} | sed \"s,^,${localPathPrefix},\" > ${candidateLocalFileDatabase}"
132 #logic is: if file list is missing we force the md5 recalculation
133 [[ ! -f $localAlienDatabase ]] && forceLocalMD5recalculation=1 && echo "forcing local MD5 sum recalculation" && cp -f $alienFileListCurrent $localAlienDatabase
135 #since we grep through the files frequently, copy some stuff to tmpfs for fast access
136 tmp=$(mktemp -d 2>/dev/null)
137 if [[ -d $tmp ]]; then
138 cp $localAlienDatabase $tmp
139 cp $localFileList $tmp
140 cp $alienFileListCurrent $tmp
141 [[ -f ${candidateLocalFileDatabase} ]] && cp ${candidateLocalFileDatabase} ${tmp}
146 echo "starting downloading:"
150 downloadedFileCounter=0
151 while read -r alienFile md5alien timestamp size
155 #sometimes the md5 turns out empty and is then stored as a "." to avoid problems parsing
156 [[ "$md5alien" =~ "." ]] && md5alien=""
158 [[ -n $timeStampInLog ]] && date
159 [[ $SECONDS -ge $secondsToSuicide ]] && echo "$SECONDS seconds passed, exiting by suicide..." && break
160 [[ "$alienFile" != "/"*"/"?* ]] && echo "WARNING: read line not path-like: $alienFile" && continue
161 ((alienFileCounter++))
162 destination=${localPathPrefix}/${alienFile}
163 destination=${destination//\/\///} #remove double slashes
164 [[ -n ${destinationModifyCommand} ]] && destination=$( eval "echo ${destination} | ${destinationModifyCommand}" )
165 destinationdir=${destination%/*}
166 [[ -n $softLinkName ]] && softlinktodestination=${destinationdir}/${softLinkName}
167 tmpdestination="${destination}.aliensyncTMP"
169 #if we allow concurrent running (DANGEROUS) check if somebody is already trying to process this file
170 if [[ -f ${tmpdestination} && ${allowConcurrent} -eq 1 ]]; then
171 echo "$tmpdestination exists - concurrent donwload? skipping..."
175 if [[ -n ${destinationModifyCommand} ]]; then
176 #find the candidate in the database, in case there are more files trying to go to the same
177 #place due to $destinationModifyCommand which alters the final path, find the one
178 #with the largest ctime (3rd field in the database list) and check if that is the current one
180 #echo grep -n ${destination} $candidateLocalFileDatabase | sed "s/:/ /" | sort -rk4
181 #grep -n ${destination} $candidateLocalFileDatabase| sed "s/:/ /" | sort -rk4
182 #this guy contains: index of the original entry, local file name, md5, ctime
183 candidateDBrecord=($(grep -n ${destination} $tmp/${candidateLocalFileDatabase##*/}| sed "s/:/ /" | sort -rk4|head -n1 ))
184 originalEntryIndex=${candidateDBrecord[0]}
185 [[ $lineNumber -ne $originalEntryIndex ]] && continue
189 if [[ -f ${destination} ]]; then
190 #soft link the downloaded file (maybe to provide a consistent link to the latest version)
191 if [[ -n $softlinktodestination ]]; then
192 echo ln -sf ${destination} ${softlinktodestination}
193 ln -sf ${destination} ${softlinktodestination}
195 ((localFileCounter++))
197 localDBrecord=($(grep $alienFile $tmp/${localAlienDatabase##*/}))
198 md5local=${localDBrecord[1]}
200 #sometimes the md5 turns out empty and is then stored as a "." to avoid problems parsing
201 [[ "$md5local" =~ "." ]] && md5local=""
203 if [[ $forceLocalMD5recalculation -eq 1 || -z $md5local ]]; then
204 md5recalculated=$(checkMD5sum ${destination})
205 [[ "$md5local" != "$md5recalculated" ]] && echo "WARNING: local copy change ${destination}"
206 md5local=${md5recalculated}
208 if [[ "$md5local" == "$md5alien" && -n $md5alien ]]; then
209 echo "OK ${destination} $md5alien"
210 if ! grep -q ${destination} $tmp/${localFileList##*/}; then
211 echo ${destination} >> $localFileList
215 if [[ -z $md5alien ]]; then
216 if ! grep -q ${destination} $tmp/${localFileList##*/}; then
217 echo ${destination} >> $localFileList
219 echo "WARNING: missing alien md5, leaving the local file as it is"
222 echo "WARNING: md5 mismatch ${destination}"
223 echo " $md5local $md5alien"
227 [[ -f $tmpdestination ]] && echo "WARNING: stale $tmpdestination, removing" && rm $tmpdestination
229 mkdir -p ${destinationdir} && chgrp ${alienSyncFilesGroupOwnership} ${destinationdir}
230 [[ ! -d $destinationdir ]] && echo cannot access $destinationdir && continue
233 #if ! haveAlienToken; then
234 # $ALIEN_ROOT/api/bin/alien-token-init $alienUserName
235 # #source /tmp/gclient_env_$UID
241 export copyTimeoutHard
242 echo copyFromAlien "$alienFile" "$tmpdestination"
243 [[ $pretend -eq 1 ]] && continue
244 copyFromAlien $alienFile $tmpdestination
245 chgrp ${alienSyncFilesGroupOwnership} $tmpdestination
247 # if we didn't download remove the destination in case we tried to redownload
249 [[ ! -f $tmpdestination ]] && echo "file not downloaded" && rm -f ${destination} && continue
252 #verify the downloaded md5 if available, validate otherwise...
253 if [[ -n $md5alien ]]; then
254 md5recalculated=$(checkMD5sum ${tmpdestination})
255 if [[ ${md5alien} == ${md5recalculated} ]]; then
256 echo "OK md5 after download"
259 echo "failed verifying md5 $md5alien of $tmpdestination"
265 #handle zip files - check the checksums
266 if [[ $alienFile =~ '.zip' && $downloadOK -eq 1 ]]; then
267 echo "checking integrity of zip archive $tmpdestination"
268 if unzip -t $tmpdestination; then
275 if [[ $downloadOK -eq 1 ]]; then
276 echo mv $tmpdestination ${destination}
277 mv $tmpdestination ${destination}
278 chgrp ${alienSyncFilesGroupOwnership} ${destination}
279 ((downloadedFileCounter++))
280 if [[ -n $softlinktodestination ]]; then
281 echo ln -s ${destination} $softlinktodestination
282 ln -s ${destination} $softlinktodestination
284 [[ -z $redownloading ]] && echo ${destination} >> $newFilesList
285 [[ -n $redownloading ]] && echo ${destination} >> $redoneFilesList
286 if ! grep -q ${destination} $tmp/${localFileList##*/}; then
287 echo ${destination} >> $localFileList
289 [[ -n ${postCommand} ]] && ( cd ${destinationdir}; eval "${postCommand}" )
290 if grep -q ${alienFile} ${failedDownloadList}; then
291 echo "removing ${alienFile} from ${failedDownloadList}"
292 grep -v ${alienFile} ${failedDownloadList} >tmpUpdatedFailed
293 mv tmpUpdatedFailed ${failedDownloadList}
296 echo "download not validated, NOT moving to ${destination}..."
297 echo "removing $tmpdestination"
298 rm -f $tmpdestination
299 echo ${alienFile} >> ${failedDownloadList}
303 [[ -f $tmpdestination ]] && echo "WARNING: tmpdestination should not still be here! removing..." && rm -r ${tmpdestination}
305 if [[ $unzipFiles -eq 1 ]]; then
306 echo unzip -o ${destination} -d ${destinationdir}
307 unzip -o ${destination} -d ${destinationdir}
311 done < ${alienFileListCurrent}
313 [[ $alienFileCounter -gt 0 ]] && mv -f $alienFileListCurrent $localAlienDatabase
317 if [[ $allOutputToLog -eq 1 ]]; then
321 cat ${newFilesList} ${redoneFilesList} > ${updatedFilesList}
323 echo alienFindCommand:
324 echo " $alienFindCommand"
326 echo "files on alien: $alienFileCounter"
327 echo "local files before: $localFileCounter"
328 echo "files downloaded: $downloadedFileCounter"
340 #output the list of failed files to stdout, so the cronjob can mail it
341 echo '###############################'
342 echo "failed to download from alien:"
344 local tmpfailed=$(mktemp)
345 [[ "$(cat ${failedDownloadList} | wc -l)" -gt 0 ]] && sort ${failedDownloadList} | uniq -c | awk 'BEGIN{print "#tries\t file" }{print $1 "\t " $2}' | tee ${tmpfailed}
347 [[ -n ${MAILTO} ]] && echo $logFile | mail -s "alienSync ${alienFindCommand} done" ${MAILTO}
349 if [[ -n ${executeEnd} ]]; then
352 echo '###############################'
353 echo "eval ${executeEnd}"
363 echo removing $lockFile
372 # like a regular alien_find command
373 # output is a list with md5 sums and ctimes
374 executable="$ALIEN_ROOT/api/bin/gbbox find"
375 [[ ! -x ${executable% *} ]] && echo "### error, no $executable..." && return 1
376 [[ -z $logOutputPath ]] && logOutputPath="./"
378 maxCollectionLength=10000
380 export GCLIENT_COMMAND_MAXWAIT=600
381 export GCLIENT_COMMAND_RETRY=20
382 export GCLIENT_SERVER_RESELECT=4
383 export GCLIENT_SERVER_RECONNECT=2
384 export GCLIENT_RETRY_DAMPING=1.2
385 export GCLIENT_RETRY_SLEEPTIME=2
388 numberOfFiles=$maxCollectionLength
389 rm -f $logOutputPath/alien_find.err
390 while [[ $numberOfFiles -ge $maxCollectionLength && $iterationNumber -lt 100 ]]; do
392 offset=$((maxCollectionLength*iterationNumber-1));
393 [[ $offset -lt 0 ]] && offset=0;
394 $executable -x coll -l ${maxCollectionLength} -o ${offset} "$@" 2>>$logOutputPath/alien_find.err \
395 | while read -a fields;
397 nfields=${#fields[*]}
402 for ((x=1;x<=${nfields};x++)); do
403 field=${fields[${x}]}
404 if [[ "${field}" == "md5="* ]]; then
407 if [[ "${field}" == "turl="* ]]; then
410 if [[ "${field}" == "ctime="* ]]; then
411 eval ${field}" "${fields[((x+1))]}
413 if [[ "${field}" == "size="* ]]; then
414 eval ${field}" "${fields[((x+1))]}
417 ctime=$( date -d "${ctime}" +%s 2>/dev/null)
418 [[ -z $md5 ]] && md5="."
419 [[ -n "$turl" ]] && echo "${turl//"alien://"/} ${md5} ${ctime} ${size}" && ((numberOfFiles++))
421 ((iterationNumber++))
428 #split the search in sub searches in the subdirectories of the base path
431 subPathSelection=${3}
432 [[ -z ${subPathSelection} ]] && subPathSelection=".*"
433 gbbox ls ${basePath} 2>/dev/null | \
434 while read subPath; do
435 [[ ! ${subPath} =~ ${subPathSelection} ]] && continue
436 alien_find ${basePath}/${subPath} ${searchTerm}
440 listCollectionContents()
442 #find the xml collections and print the list of filenames and hashes
443 while read -a fields; do
444 nfields=${#fields[*]}
448 for ((x=1;x<=${nfields};x++)); do
449 field=${fields[${x}]}
450 if [[ "${field}" == "md5="* ]]; then
453 if [[ "${field}" == "turl="* ]]; then
456 if [[ "${field}" == "ctime="* ]]; then
457 eval "${field} ${fields[((x+1))]}"
460 ctime=$( date -d "${ctime}" +%s 2>/dev/null)
461 [[ -n "$turl" ]] && echo "${turl//"alien://"/} ${md5} ${ctime}"
462 done < <(catCollections $1 $2 2>/dev/null)
467 #print the contents of collection(s)
468 if [[ $# -eq 2 ]]; then
469 while read collection; do
470 [[ $collection != "/"*"/"?* ]] && continue
471 gbbox cat $collection
472 done < <(alien_find $1 $2)
473 elif [[ $# -eq 1 ]]; then
480 #only get a new token if the old one expires soon
482 [[ -z $maxExpireTime ]] && maxExpireTime=4000
483 [[ -z $ALIEN_ROOT ]] && echo "no ALIEN_ROOT!" && return 1
485 tokenExpirationTime=$($ALIEN_ROOT/api/bin/alien-token-info|grep Expires)
486 tokenExpirationTime=$(date -d "${tokenExpirationTime#*:}" "+%s")
487 secondsToExpire=$(( tokenExpirationTime-now ))
488 if [[ $secondsToExpire -lt $maxExpireTime ]]; then
491 echo "token valid for another $secondsToExpire seconds"
498 #copy the file $1 to $2 using a specified method
499 #uses the "timeout" command to make sure the
500 #download processes will not hang forever.
502 [[ -z $copyTimeout ]] && copyTimeout=600
503 [[ -z $copyTimeoutHard ]] && copyTimeoutHard=1200
504 src=${1//"alien://"/}
507 if [[ "$copyMethod" == "tfilecp" ]]; then
508 if which timeout &>/dev/null; then
509 echo timeout $copyTimeout root -b -q "$copyScript(\"$src\",\"$dst\")"
510 timeout $copyTimeout root -b -q "$copyScript(\"$src\",\"$dst\")"
512 echo root -b -q "$copyScript(\"$src\",\"$dst\")"
513 root -b -q "$copyScript(\"$src\",\"$dst\")"
516 if which timeout &>/dev/null; then
517 echo timeout $copyTimeout $ALIEN_ROOT/api/bin/alien_cp $src $dst
518 timeout $copyTimeout $ALIEN_ROOT/api/bin/alien_cp $src $dst
520 echo $ALIEN_ROOT/api/bin/alien_cp $src $dst
521 $ALIEN_ROOT/api/bin/alien_cp $src $dst
531 secondsToSuicide=$(( 10*3600 ))
532 localPathPrefix="${PWD}"
533 #define alienSync_localPathPrefix in your env to have a default central location
534 [[ -n ${alienSync_localPathPrefix} ]] && localPathPrefix=${alienSync_localPathPrefix}
540 #first, check if the config file is configured
541 #is yes - source it so that other options can override it
543 for opt in "${args[@]}"; do
544 if [[ ${opt} =~ configFile=.* ]]; then
546 [[ ! -f ${configFile} ]] && echo "configFile ${configFile} not found, exiting..." && return 1
547 echo "using config file: ${configFile}"
548 source "${configFile}"
553 #then, parse the options as they override the options from file
554 for opt in "${args[@]}"; do
555 if [[ ! "${opt}" =~ .*=.* ]]; then
556 echo "badly formatted option ${var}, should be: option=value, stopping..."
559 local var="${opt%%=*}"
560 local value="${opt#*=}"
561 echo "${var} = ${value}"
562 export ${var}="${value}"
565 #things that by default depend on other variables should be set here, after the dependencies
566 [[ -z ${logOutputPath} ]] && logOutputPath="${localPathPrefix}/alienSyncLogs"
574 [[ ! -f ${file} ]] && return 1
575 if which md5sum &>/dev/null; then
576 local tmp=($(md5sum ${file}))
578 elif which md5 &>/dev/null; then
579 local tmp=($(md5 ${file}))