-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsole-sync.sh
More file actions
executable file
Β·177 lines (146 loc) Β· 4.55 KB
/
sole-sync.sh
File metadata and controls
executable file
Β·177 lines (146 loc) Β· 4.55 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
####################################################
# Configuration
####################################################
# Paths
LOCAL_PATH=./
# Source directory (can be overriden with command args)
src=
# Default preset file location (can be overriden with command args)
preset="${LOCAL_PATH}sync-preset"
# Default excludes file location (can be overriden with command args)
exclude="${LOCAL_PATH}sync-exclude"
# Default values
MODE=TEST
RSYNC_OPTIONS=rvc
OPT_DELETE=
OPT_DRY_RUN=--dry-run
OPT_EXCLUDES=
OPT_KEY=
OPT_PORT=
OPT_SSH=
# Colors
ESC_SEQ="\x1b["
C_END=$ESC_SEQ"39;49;00m"
C_RED=$ESC_SEQ"31;01m"
C_GREEN=$ESC_SEQ"32;01m"
C_YELLOW=$ESC_SEQ"33;01m"
C_BLUE=$ESC_SEQ"34;01m"
C_MAGENTA=$ESC_SEQ"35;01m"
C_CYAN=$ESC_SEQ"36;01m"
####################################################
# Setup
####################################################
# Script input parameters
# https://brianchildress.co/named-parameters-in-bash/
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2"
fi
shift
done
####################################################
# Help
####################################################
__HELP="
Usage: $(basename $0) [OPTIONS]
Options:
--delete Remove unmatching files in the destination (default: false)
--exclude [filepath] Relative path to exclude file (default: ${exclude})
--help Looks like you've already found it!
--key [filepath] Optional path to ssh key file
--live Don't do a dry run, do it for R E A L (default: false)
--port [number] Specify a non-standard ssh port
--preset [filepath] Relative path to preset file (default: ${preset})
--pull Pull from the remote (default: push)
--src [path] Local source directory (default: ${src})
--<var> [string] Any preset variables not defined in presets (host, path, user)
NOTE: Any of the options other than preset or help may be defined in your preset file(s).
"
if [ -n "${help+set}" ]; then
echo -e "$__HELP";
exit 1
fi
####################################################
# Check that must have vars are set
####################################################
# Check preset file
if [ -e ${preset} ]; then
. ${preset}
echo "Loading preset file from $preset"
fi
# Ensure required preset variables are set
if [ -z "$host" ] || [ -z "$path" ] || [ -z "$user" ]; then
echo -e "${C_RED}π« Define host, path, and user in arguments or the preset file ${preset}${C_END}"
exit 1
fi
# Inculde the exclude file if specified
if [ -e ${exclude} ]; then
echo "Loading excludes from $exclude"
OPT_EXCLUDES="--exclude-from=${exclude}"
fi
####################################################
# Option configuration based on settings
####################################################
# SSH Key
# Port
if [ -n "${key+set}" ]; then
OPT_KEY="-i $key"
fi
# SSH Port
if [ -n "${port+set}" ]; then
OPT_PORT="-p $port"
fi
# SSH Options
if [[ ! -z "$OPT_KEY" || ! -z "$OPT_PORT" ]]; then
OPT_SSH="-e 'ssh $OPT_KEY $OPT_PORT'"
fi
# Dry Run (--live)
# Don't do a dry run, do it for realsies
if [ -n "${live+set}" ]; then
MODE=LIVE
OPT_DRY_RUN=
fi
# Delete (--delete)
# Remove files that don't match in the destination
if [ -n "${delete+set}" ]; then
OPT_DELETE=--delete
fi
# Pull (--pull)
# Defaults to push (local > remote)
if [ -n "${pull+set}" ]
then
SOURCE=$user@$host:$path$src
DESTINATION=$LOCAL_PATH$src
else
SOURCE=$LOCAL_PATH$src
DESTINATION=$user@$host:$path$src
fi
####################################################
# RSYNC execution
####################################################
CMD="rsync -$RSYNC_OPTIONS $OPT_SSH $OPT_DELETE $OPT_DRY_RUN $OPT_EXCLUDES $SOURCE $DESTINATION"
# Display the mode being run
if [ $MODE == "TEST" ]
then
echo -e "${C_YELLOW}π Beginning $MODE sync...${C_END}"
else
echo -e "${C_GREEN}π Beginning $MODE sync...${C_END}"
fi
# Execute it and handle any rsync errors
eval $CMD
if [ "$?" -eq "0" ]
then
echo '--------------------------------------------------------------------------'
echo -e "${C_GREEN}β
All done with $MODE run.${C_END}"
echo "$CMD"
echo '--------------------------------------------------------------------------'
# Live reminder
if [ $MODE != "LIVE" ]; then
echo -e "If everything looks good, add the ${C_CYAN}--live${C_END} argument to run for realsies"
fi
else
echo -e "π« ${C_RED}Error while running rsync: See output above ππΌ${C_END}"
echo -e "COMMAND: ${C_YELLOW}$CMD${C_END}"
fi