blob: d30c4fdce2ed5ce33e65c2c3c41499c87c7b21b0 (
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/usr/bin/env bash
set -e
if [ $# -lt 4 ]; then
echo "Usage: ssync KEY_FILE REMOTE REMOTE_DIR SRC_DIR"
exit 1
fi
KEY_FILE=$1
REMOTE=$2
REMOTE_DIR=$3
SRC_DIR=$4
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 $SSYNC_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
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"
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"
|