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}
351 echo '###############################'
352 echo "eval ${executeEnd}"
361 echo removing $lockFile
370 # like a regular alien_find command
371 # output is a list with md5 sums and ctimes
372 executable="$ALIEN_ROOT/api/bin/gbbox find"
373 [[ ! -x ${executable% *} ]] && echo "### error, no $executable..." && return 1
374 [[ -z $logOutputPath ]] && logOutputPath="./"
376 maxCollectionLength=10000
378 export GCLIENT_COMMAND_MAXWAIT=600
379 export GCLIENT_COMMAND_RETRY=20
380 export GCLIENT_SERVER_RESELECT=4
381 export GCLIENT_SERVER_RECONNECT=2
382 export GCLIENT_RETRY_DAMPING=1.2
383 export GCLIENT_RETRY_SLEEPTIME=2
386 numberOfFiles=$maxCollectionLength
387 rm -f $logOutputPath/alien_find.err
388 while [[ $numberOfFiles -ge $maxCollectionLength && $iterationNumber -lt 100 ]]; do
390 offset=$((maxCollectionLength*iterationNumber-1));
391 [[ $offset -lt 0 ]] && offset=0;
392 $executable -x coll -l ${maxCollectionLength} -o ${offset} "$@" 2>>$logOutputPath/alien_find.err \
393 | while read -a fields;
395 nfields=${#fields[*]}
400 for ((x=1;x<=${nfields};x++)); do
401 field=${fields[${x}]}
402 if [[ "${field}" == "md5="* ]]; then
405 if [[ "${field}" == "turl="* ]]; then
408 if [[ "${field}" == "ctime="* ]]; then
409 eval ${field}" "${fields[((x+1))]}
411 if [[ "${field}" == "size="* ]]; then
412 eval ${field}" "${fields[((x+1))]}
415 ctime=$( date -d "${ctime}" +%s 2>/dev/null)
416 [[ -z $md5 ]] && md5="."
417 [[ -n "$turl" ]] && echo "${turl//"alien://"/} ${md5} ${ctime} ${size}" && ((numberOfFiles++))
419 ((iterationNumber++))
426 #split the search in sub searches in the subdirectories of the base path
429 subPathSelection=${3}
430 [[ -z ${subPathSelection} ]] && subPathSelection=".*"
431 gbbox ls ${basePath} 2>/dev/null | \
432 while read subPath; do
433 [[ ! ${subPath} =~ ${subPathSelection} ]] && continue
434 alien_find ${basePath}/${subPath} ${searchTerm}
438 listCollectionContents()
440 #find the xml collections and print the list of filenames and hashes
441 while read -a fields; do
442 nfields=${#fields[*]}
446 for ((x=1;x<=${nfields};x++)); do
447 field=${fields[${x}]}
448 if [[ "${field}" == "md5="* ]]; then
451 if [[ "${field}" == "turl="* ]]; then
454 if [[ "${field}" == "ctime="* ]]; then
455 eval "${field} ${fields[((x+1))]}"
458 ctime=$( date -d "${ctime}" +%s 2>/dev/null)
459 [[ -n "$turl" ]] && echo "${turl//"alien://"/} ${md5} ${ctime}"
460 done < <(catCollections $1 $2 2>/dev/null)
465 #print the contents of collection(s)
466 if [[ $# -eq 2 ]]; then
467 while read collection; do
468 [[ $collection != "/"*"/"?* ]] && continue
469 gbbox cat $collection
470 done < <(alien_find $1 $2)
471 elif [[ $# -eq 1 ]]; then
478 #only get a new token if the old one expires soon
480 [[ -z $maxExpireTime ]] && maxExpireTime=4000
481 [[ -z $ALIEN_ROOT ]] && echo "no ALIEN_ROOT!" && return 1
483 tokenExpirationTime=$($ALIEN_ROOT/api/bin/alien-token-info|grep Expires)
484 tokenExpirationTime=$(date -d "${tokenExpirationTime#*:}" "+%s")
485 secondsToExpire=$(( tokenExpirationTime-now ))
486 if [[ $secondsToExpire -lt $maxExpireTime ]]; then
489 echo "token valid for another $secondsToExpire seconds"
496 #copy the file $1 to $2 using a specified method
497 #uses the "timeout" command to make sure the
498 #download processes will not hang forever.
500 [[ -z $copyTimeout ]] && copyTimeout=600
501 [[ -z $copyTimeoutHard ]] && copyTimeoutHard=1200
502 src=${1//"alien://"/}
505 if [[ "$copyMethod" == "tfilecp" ]]; then
506 if which timeout &>/dev/null; then
507 echo timeout $copyTimeout root -b -q "$copyScript(\"$src\",\"$dst\")"
508 timeout $copyTimeout root -b -q "$copyScript(\"$src\",\"$dst\")"
510 echo root -b -q "$copyScript(\"$src\",\"$dst\")"
511 root -b -q "$copyScript(\"$src\",\"$dst\")"
514 if which timeout &>/dev/null; then
515 echo timeout $copyTimeout $ALIEN_ROOT/api/bin/alien_cp $src $dst
516 timeout $copyTimeout $ALIEN_ROOT/api/bin/alien_cp $src $dst
518 echo $ALIEN_ROOT/api/bin/alien_cp $src $dst
519 $ALIEN_ROOT/api/bin/alien_cp $src $dst
529 secondsToSuicide=$(( 10*3600 ))
530 localPathPrefix="${PWD}"
531 #define alienSync_localPathPrefix in your env to have a default central location
532 [[ -n ${alienSync_localPathPrefix} ]] && localPathPrefix=${alienSync_localPathPrefix}
538 #first, check if the config file is configured
539 #is yes - source it so that other options can override it
541 for opt in "${args[@]}"; do
542 if [[ ${opt} =~ configFile=.* ]]; then
544 [[ ! -f ${configFile} ]] && echo "configFile ${configFile} not found, exiting..." && return 1
545 echo "using config file: ${configFile}"
546 source "${configFile}"
551 #then, parse the options as they override the options from file
552 for opt in "${args[@]}"; do
553 if [[ ! "${opt}" =~ .*=.* ]]; then
554 echo "badly formatted option ${var}, should be: option=value, stopping..."
557 local var="${opt%%=*}"
558 local value="${opt#*=}"
559 echo "${var} = ${value}"
560 export ${var}="${value}"
563 #things that by default depend on other variables should be set here, after the dependencies
564 [[ -z ${logOutputPath} ]] && logOutputPath="${localPathPrefix}/alienSyncLogs"
572 [[ ! -f ${file} ]] && return 1
573 if which md5sum &>/dev/null; then
574 local tmp=($(md5sum ${file}))
576 elif which md5 &>/dev/null; then
577 local tmp=($(md5 ${file}))