diff options
author | Bill <bill@billserver.senders.io> | 2021-04-24 11:14:50 -0400 |
---|---|---|
committer | Bill <bill@billserver.senders.io> | 2021-04-24 11:14:50 -0400 |
commit | ddeab6ea9666ba0fee35fdd09724b7a663943f1a (patch) | |
tree | 70166071826beaa706599a2fb3b29ff112ce5a43 | |
parent | c283b236841114d452b1e4b4c08d1be58315325d (diff) |
Setup log function to make parallel runs trackable
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | ssync | 37 |
2 files changed, 23 insertions, 15 deletions
@@ -1 +1,2 @@ creds +ssync-cron @@ -1,33 +1,43 @@ #!/usr/bin/env bash set -e +RUNID=$(openssl rand -hex 16) +function log { + echo -e "${RUNID}\t$(date -Is)\t$@" +} + if [ $# -lt 4 ]; then echo "Usage: ssync KEY_FILE REMOTE REMOTE_DIR SRC_DIR" exit 1 fi +# opts KEY_FILE=$1 REMOTE=$2 REMOTE_DIR=$3 SRC_DIR=$4 +# constants PARALLEL=5 SSYNC_DIR=$HOME/.local/ssync FETCHED_FILE=$SSYNC_DIR/fetched FETCH_FILE=$SSYNC_DIR/fetch LASTRAN_FILE=$SSYNC_DIR/lastran NEXT_RUN_DATE=$(date -Is) - +CURGET_FILE=$SSYNC_DIR/.$RUNID if [ ! -f $LASTRAN_FILE ]; then mkdir -p $SSYNC_DIR echo $NEXT_RUN_DATE > $LASTRAN_FILE - echo "No run existed marking next run for files newer than: $NEXT_RUN_DATE" + log "No run existed marking next run for files newer than: $NEXT_RUN_DATE" exit 0; fi +PREV_RUN_DATE=$(cat $HOME/.local/ssync/.lastran) -if [ $PREV_RUN_DATE == "" ]; then +# main + +if [[ $PREV_RUN_DATE == "" ]]; then echo $NEXT_RUN_DATE > $LASTRAN_FILE - echo "No run existed marking next run for files newer than: $NEXT_RUN_DATE" + log "No run existed marking next run for files newer than: $NEXT_RUN_DATE" exit 0; fi @@ -35,15 +45,12 @@ if [ ! -f $FETCHED_FILE ]; then touch $FETCHED_FILE fi -PREV_RUN_DATE=$(cat $HOME/.local/ssync/.lastran) -echo "Failsafe - Running at: $NEXT_RUN_DATE - if failed to write use this timestamp in $LASTRAN_FILE" -echo "Syncing files since: $PREV_RUN_DATE" +log "Failsafe - Running at: $NEXT_RUN_DATE - if failed to write use this timestamp in $LASTRAN_FILE" +log "Syncing files since: $PREV_RUN_DATE" -RUNID=$(openssl rand -hex 16) -CURGET_FILE=$SSYNC_DIR/.$RUNID -echo "Fetching files for $RUNID" +log "Fetching files" ssh -i $KEY_FILE $REMOTE "find ${REMOTE_DIR} -newermt ${PREV_RUN_DATE} -exec realpath --relative-to ${REMOTE_DIR} {} \;" >> $CURGET_FILE comm -23 <(sort -u $CURGET_FILE) <(sort -u $FETCHED_FILE) > $FETCH_FILE COUNT=$(wc -l $FETCHED_FILE | cut -d' ' -f1) @@ -52,17 +59,17 @@ if [ $COUNT -gt 0 ]; then # # Syncing # - echo "Found ${COUNT} files to fetch" + log "Found ${COUNT} files to fetch" cat $FETCH_FILE >> $FETCHED_FILE - echo "Wrote files to fetched files" - + log "Wrote files to fetched files" + log "Syncing now" cat $FETCH_FILE | xargs -n1 -P$PARALLEL -I '{}' rsync -e "ssh -i $KEY_FILE" \ -av \ $REMOTE:${REMOTE_DIR}/'{}' ${SRC_DIR} else - echo "No files to sync" + log "No files to sync" fi echo $NEXT_RUN_DATE > $LASTRAN_FILE -echo "Done syncing" +log "Done syncing" |