#!/bin/bash if [ "$(id -un)" != "root" ]; then echo "ERROR : In order to function correctly, this script need to be run as root" exit 1 fi conf=/usr/local/etc/send2archive.conf function usage () { cat < Use devrepo for files/dirs in the development repository Use prodrepo for files/dirs in the production repository Use develop for files/dirs in the development directory Use apps-core for apps core dirs in the development directory Use any valid shell pattern for filenames including *, ? and [] Valid options are "-q" to be quiet "-y" to answer "Y" when prompting for confirmation of file deletion (dangerous with arbitrary patterns) "-ns" to suppress syncing of build servers after removal of files "-f " to read patterns from file "-h" to see this help Assure to have configuration of "archive_server" in $conf EOT } if [ ! -r $conf ]; then echo "ERROR : Cannot read configuration in $conf" usage exit 1 else source $conf fi if [ "x$archive_server" == "x" ]; then echo "ERROR : archive_server not set" usage exit 1 fi if ! ping -c 1 -W 2 $archive_server &> /dev/null; then echo "ERROR : Cannot reach archive server on $archive_server" echo " Need to check configuration in $conf" exit 1 fi quiet=false # be quiet? allyes=false # answer Y on all prompts? nosync=false # do not sync build servers? from_file=false # patterns from file? while [ "${1:0:1}" == "-" ]; do # having options in cmd line case "${1,,}" in "-q") quiet=true shift ;; "-y") allyes=true shift ;; "-ns") nosync=true shift ;; "-f") pat_file="$2" if [ ! -r $pat_file ]; then echo "ERROR : Cannot read pattern file : $pat_file " usage exit 1 fi shift 2 ;; "-h") usage exit 0 ;; *) echo "ERROR : Unknown option '$1'" usage exit 99 ;; esac done case "$1" in "devrepo" | "prodrepo" | "develop" | "apps-core") : # all fine till here ;; *) echo "ERROR : Need to define \"devrepo\" or \"prodrepo\" or \"develop\" or \"apps-core\"" usage exit 1 ;; esac [ $# -lt 2 ] && usage && exit 1 target=$1 shift pattern="$@" $allyes && ans="Y" || ans="N" declare -a pids msg="Sending following files to archive: $pattern Do you want to remove those when done (Y/N) or Exit this (E) ? [N] " ! $allyes && read -p "$msg" ans [ "${ans^^}" == "E" ] && exit 0 $quiet && exec > /dev/null cur_dir=$(pwd) for dir in $(ls -d $pattern); do np=$(echo "$target $(basename $dir)" | nc ${archive_server} 49010) echo "Sending $dir to ${archive_server}:$np" cd $(dirname $dir) tar cz $(basename $dir) | nc ${archive_server} $np & pids[${#pids[*]}]=$! cd $cur_dir sleep 5 # give server time to get ready for next connection done echo -n "Waiting till all transfers are done " while [ ${#pids[@]} -gt 0 ]; do echo -n "." for pid in $(seq 0 $(expr ${#pids[@]} - 1 )); do if ! ps ax | grep "${pids[$pid]}" | grep "nc ${archive_server}" | grep -vq grep; then unset pids[$pid] pids=(${pids[@]}) # remove empty fields # break fi done sleep 5 done echo if [ "${ans^^}" == "Y" ]; then echo "Removing $pattern" for dir in $(ls -d $pattern); do [ "$dir" != "/" ] && rm -rf $pattern done ! $nosync && [ "$target" != "prodrepo" ] && /usr/local/bin/sync_servers else echo "All $pattern transferred. You might want to remove them now and then synchronize the build servers" fi echo "INFO : Reindexing $target on archive server ${archive_server}" echo "REINDEX $target" | nc ${archive_server} 49010