-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpmirror
executable file
·123 lines (102 loc) · 2.41 KB
/
ftpmirror
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
#!/bin/bash
# Mirror local folder to remote folder
# Looks for ./ftpmirror.json in the current local pwd
# by default, or you can define another file like so:
# ftpmirror ~/Documents/Project/config.json
CONFIG_JSON=${1:-./ftpmirror.json}
declare -A CONFIG=(
[PROTOCOL]=""
[PORT]=""
[URL]=""
[LOCAL_DIR]=""
[REMOTE_DIR]=""
[USER]=""
[PASS]=""
[DELAY]=""
[MIRROR_OPTIONS]="--continue --reverse --delete --verbose=3"
[INOTIFYWAIT_OPTIONS]="--exclude (secret)|(.(swp|swx)$)"
)
CHECK_FOR_CONFIG() {
if [[ ! -e $CONFIG_JSON ]]; then
echo "Please specify a JSON configuration file (default: ftpmirror.json)"
echo "Here is an example of what it should look like:"
cat <<JSON
{
"PROTOCOL":"sftp",
"PORT":"2222",
"URL":"sftp.domain.com",
"LOCAL_DIR":"./project-directory/directory/",
"REMOTE_DIR":"/remote/path/directory/",
"USER":"username",
"PASS":"password",
"DELAY":10,
"MIRROR_OPTIONS":"--continue --reverse --delete --verbose=3",
"INOTIFYWAIT_OPTIONS":"--exclude (secret)|(.(swp|swx)$)"
}
JSON
exit 1
fi
}
CHECK_FOR_CONFIG
DEFINE_CONFIG_VARS() {
for i in "${!CONFIG[@]}"; do
VALUE=$(jq --raw-output .$i $CONFIG_JSON)
CONFIG[$i]=$VALUE
if [[ -z ${CONFIG[$i]} ]]; then
echo "Error: $i is not defined in config file"
exit 1
fi
done
}
DEFINE_CONFIG_VARS
MIRROR_LOCAL_TO_REMOTE() {
lftp ${CONFIG[PROTOCOL]}://${CONFIG[USER]}:${CONFIG[PASS]}@${CONFIG[URL]}:${CONFIG[PORT]}<<-MIRROR
cd ${CONFIG[REMOTE_DIR]}
!echo ""
!echo "$(date "+%d/%m/%Y-%T")"
!echo "Remote Directory Before Mirror:"
ls
!echo ""
mirror --reverse ${CONFIG[MIRROR_OPTIONS]} \
${CONFIG[LOCAL_DIR]} \
${CONFIG[REMOTE_DIR]}
!echo ""
!echo "$(date "+%d/%m/%Y-%T")"
!echo "Remote Directory After Mirror:"
ls
!echo ""
exit
MIRROR
if [ ! $? -eq 0 ]; then
echo "$(date "+%d/%m/%Y-%T") Cant mirror files. Make sure the credentials and server information are correct"
exit 1
fi
}
DELAY_TIMER() {
SECS=${1:-15}
echo ""
while [[ $SECS -gt 0 ]]; do
echo -ne "Waiting... $SECS\033[0K\r"
sleep 1
: $((SECS--))
done
echo ""
}
MIRROR_LOCAL_TO_REMOTE
while inotifywait ${CONFIG[INOTIFYWAIT_OPTIONS]} \
--recursive \
--event modify \
--event attrib \
--event close_write \
--event moved_to \
--event moved_from \
--event move \
--event create \
--event delete \
${CONFIG[LOCAL_DIR]}; do
echo ""
MIRROR_LOCAL_TO_REMOTE
echo ""
DELAY_TIMER ${CONFIG[DELAY]}
echo ""
done