blob: e19ee78f5fefaf84dbb5768c718a6668adc9b986 (
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#!/usr/bin/env sh
USAGE="ssync-index [options] [CONFIG_FILE]
OPTIONS
-k [KEY_FILE] override conigured key file
-o [OUTPUT_DIR] override configured index output dir
-l local only
-r remote only
-v verbose logging
-h print this message"
# HELPER METHODS
function verbose_log {
if [ ! -z "$VERBOSE_FLAG" ]; then
echo "$1"
fi
}
function lines {
wc -l $1 | cut -d' ' -f1
}
function get_default_index_dir {
local cache_path=ssync/index/
local cache_dir=
if [ ! -z "$XDG_CACHE_DIR" ]; then
cache_dir=$XDG_CACHE_DIR/$cache_path
else
cache_dir=/tmp/
fi
mkdir -p $cache_dir/$cache_path
echo $cache_dir/$cache_path
}
# GLOBAL VALUES
DEFAULT_INDEX_WINDOW=86400
LOCAL_ONLY_FLAG=
REMOTE_ONLY_FLAG=
VERBOSE_FLAG=
KEY_FILE_FLAG=
KEY_FILE_ARG=
OUTPUT_DIR_FLAG=
OUTPUT_DIR_ARG=
# OPTIONS PARSING
while getopts "hvlro:k:" opt; do
case "${opt}" in
h) echo "$USAGE"
exit 1
;;
o) OUTPUT_DIR_FLAG=1
OUTPUT_DIR_ARG="$OPTARG"
;;
k) KEY_FILE_FLAG=1
KEY_FILE_ARG="$OPTARG"
;;
l) LOCAL_ONLY_FLAG=1
;;
r) REMOTE_ONLY_FLAG=1
;;
v) VERBOSE_FLAG=1
;;
esac
done
shift $(($OPTIND -1))
if [ $# -eq 0 ]; then
echo "$USAGE"
exit 1
fi
CONFIG_FILE=$1
# CONFIG PARSING
if [ -z "$CONFIG_FILE" ]; then
echo "CONFIG_FILE is missing."
exit 1
fi
if [ ! -f $CONFIG_FILE ]; then
echo "$CONFIG_FILE does not exist."
exit 1
fi
source $CONFIG_FILE
# CONFIG VALIDATION
if [ -z "$remote_host" ]; then
echo "Config is missing remote_host"
exit 1
fi
if [ -z "$remote_root_dir" ]; then
echo "Config is missing remote_root_dir"
exit 1
fi
if [ -z "$keyfile" ]; then
echo "Config is missing keyfile"
exit 1
elif [ ! -f "$keyfile" ]; then
echo "Configured keyfile '$keyfile' does not exist"
exit 1
fi
if [ -z "$local_root_dir" ]; then
echo "Config is missing local_root_dir"
exit 1
elif [ ! -d "$local_root_dir" ]; then
echo "Configured local_root_dir '$local_root_dir' does not exist"
exit 1
fi
# VALUE SETTING
if [ -z "$index_window_s" ]; then
index_window_s=$DEFAULT_INDEX_WINDOW
fi
local_index_file=
remote_index_file=
if [ ! -z "$OUTPUT_DIR_FLAG" ]; then
index_dir=$OUTPUT_DIR_ARG
fi
if [ ! -z "$index_dir" ]; then
if [ ! -d "$index_dir" ]; then
echo "Configured index_dir '$index_dir' does not exist"
exit 1
fi
else
index_dir=$(get_default_index_dir)
fi
# RUN INDEXING
ts=$(date -u +%s)
local_index_file=${index_dir}/${ts}_local.idx
remote_index_file=${index_dir}/${ts}_remote.idx
newermt=$(date -d "$index_window_s seconds ago" -u -Is)
verbose_log "Targetting files from $index_window_s seconds ago: $newermt"
# index remote host relative to your fetch dir
if [ -z "$LOCAL_ONLY_FLAG" ]; then
verbose_log "Generating remote index from ${remote_root_dir} to ${remote_index_file}"
ssh -i $keyfile $remote_host \
"find ${remote_root_dir} -type f -newermt $newermt -exec realpath {} \;" > $remote_index_file
verbose_log "Indexed $(lines $remote_index_file) remote files"
fi
# index local filenames ONLY
if [ -z "$REMOTE_ONLY_FLAG" ]; then
verbose_log "Generate local index from ${local_root_dir} to ${local_index_file}"
find ${local_root_dir} -type f -exec basename {} \; > $local_index_file
verbose_log "Indexed $(lines $local_index_file) local files"
fi
|