summaryrefslogtreecommitdiff
path: root/ssync
blob: 5396be0ee90d403beac3586b04e56bbde430c19d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env bash
set -ex

if [ $# -lt 3 ]; then
  echo "Usage: ssync KEY_FILE REMOTE_DIR SRC_DIR"
  exit 1
fi

KEY_FILE=$1
REMOTE_DIR=$2
SRC_DIR=$3

LASTRAN_DIR=$HOME/.local/ssync
LASTRAN_FILE=$LASTRAN_DIR/.lastran
NEXT_RUN_DATE=$(date -Is)
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"

if [ ! -f $LASTRAN_FILE ]; then
  mkdir -p $LASTRAN_DIR
  echo $NEXT_RUN_DATE > $LASTRAN_FILE
  echo "No run existed marking next run for files newer than: $NEXT_RUN_DATE"
  exit 0;
fi

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"
  exit 0;
fi

echo "Syncing files since: $PREV_RUN_DATE"

rsync -e "ssh -i $KEY_FILE" \
  -av \
  --files-from=<(ssh -i $KEY_FILE billserver "find ${REMOTE_DIR} -newermt ${PREV_RUN_DATE} -exec realpath --relative-to ${REMOTE_DIR} {} \;") \
  billserver:${REMOTE_DIR} ${SRC_DIR}

echo $NEXT_RUN_DATE > $LASTRAN_FILE
echo "Done syncing"