summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xssync43
1 files changed, 34 insertions, 9 deletions
diff --git a/ssync b/ssync
index 6ac0496..d30c4fd 100755
--- a/ssync
+++ b/ssync
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
-set -ex
+set -e
if [ $# -lt 4 ]; then
echo "Usage: ssync KEY_FILE REMOTE REMOTE_DIR SRC_DIR"
@@ -11,12 +11,15 @@ REMOTE=$2
REMOTE_DIR=$3
SRC_DIR=$4
-LASTRAN_DIR=$HOME/.local/ssync
-LASTRAN_FILE=$LASTRAN_DIR/.lastran
+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)
if [ ! -f $LASTRAN_FILE ]; then
- mkdir -p $LASTRAN_DIR
+ mkdir -p $SSYNC_DIR
echo $NEXT_RUN_DATE > $LASTRAN_FILE
echo "No run existed marking next run for files newer than: $NEXT_RUN_DATE"
exit 0;
@@ -28,16 +31,38 @@ if [ $PREV_RUN_DATE == "" ]; then
exit 0;
fi
+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"
-rsync -e "ssh -i $KEY_FILE" \
- -av \
- --files-from=<(ssh -i $KEY_FILE $REMOTE "find ${REMOTE_DIR} -newermt ${PREV_RUN_DATE} -exec realpath --relative-to ${REMOTE_DIR} {} \;") \
- $REMOTE:${REMOTE_DIR} ${SRC_DIR}
+RUNID=$(openssl rand -hex 16)
+CURGET_FILE=$SSYNC_DIR/.$RUNID
+
+echo "Fetching files for $RUNID"
+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)
+
+if [ $COUNT -gt 0 ]; then
+ #
+ # Syncing
+ #
+ echo "Found ${COUNT} files to fetch"
+
+ cat $FETCH_FILE >> $FETCHED_FILE
+ echo "Wrote files to fetched files"
+
+ 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"
+fi
echo $NEXT_RUN_DATE > $LASTRAN_FILE
echo "Done syncing"