-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplica.sh
executable file
·257 lines (238 loc) · 8.24 KB
/
replica.sh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# _ _ #
# | |(_) Replica, a backup script #
# _ __ ___ _ __ | | _ ___ __ _ ------------------------ #
# | '__|/ _ \| '_ \ | || | / __|/ _` | Author: Techn0Viking #
# | | | __/| |_) || || || (__| (_| | Version: 1.0 #
# |_| \___|| .__/ |_||_| \___|\__,_| License: LGPL-3.0 #
# | | #
# |_| #
# #
# https://github.com/Gitoffomalawn/replica #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Variables (defaults)
src=~/TEMP # Source directory (-s)
dest=~/TEST # Destination directory (-d)
dir=`date +%Y%m%d` # Sub directory naming scheme
b=3 # Backup versions to keep (-b)
targz=~/$dir.tar.gz # Path to archive for offline backup (-t)
ip=`echo $SSH_CONNECTION | awk '{print $3}'` # Machine IP address (for scp) (-a)
port=`echo $SSH_CONNECTION | awk '{print $4}'` # Port used for SSH (for scp) (-p)
p=~/ # Path relativity (for scp)
B=0 # Switch for unlimited backups (-B)
A=0 # Switch for disabling archiving (-A)
#method=scp # Archive download method (-m)
# Functions
## Overwrite all files in target directory
function overwrite {
echo -e "\n\nOverwriting files in local backup directory: $dest/$dir"
rm -rf $dest/$dir
mkdir $dest/$dir
cp -r . $dest/$dir
}
## Update older files in destination with newer from source
function update {
echo -e "\n\nUpdating files in local backup directory: $dest/$dir"
cp -r -v -u . $dest/$dir
}
## Abort the script operation (execute no further commands)
function abort_normal {
echo -e "\nBackup script aborted.\n"
cd - > /dev/null
exit 0
}
function abort_abnormal {
exit 1
}
## Validate whether and IP is in valid format
function validFormatIP {
echo $ip | grep -w -E -o '^(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1]?[1-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' > /dev/null
if [ $? -eq 0 ]
then
return 0
else
echo -e "\n[Err: Invalid IP address]\n"
abort_abnormal
fi
}
## Validate port number range
function validNetPort {
if [ $port -ge 0 -a $port -le 65535 ]
then
return 0
else
echo -e "\n[Err: Invalid network port number]\n"
abort_abnormal
fi
}
## Validate whole numbers
function wholeNum {
re_isanum='^[0-9]+$'
if ! [[ $b =~ $re_isanum ]] ; then
echo -e "\n[Err: Number of backups must be a positive, whole number]\n"
abort_abnormal
elif [[ $b -eq "0" ]]; then
echo -e "\n[Err: Number of backups must be larger than 0]\n"
abort_abnormal
else
return 0
fi
}
## Validate .tar.gz
function archiveTar {
gz=`echo ${targz:(-7)}`
if [ $gz == ".tar.gz" ] ; then
return 0
else
echo -e "\n[Err: Supplied archive name must be a .tar.gz file]\n"
abort_abnormal
fi
}
## Print help text
function help {
echo -e "\nReplica is a script that will make a copy of a directory, and also create an\
\narchive of that directory for offline storage. Currently, only .tar.gz formats\
\nare supported. \
\n \
\nUsage: ./replica [OPTIONS] \
\nOptions: \
\n-a [IP ADDRESS] Proxy IP address. If you are using a proxy to login remotely\n\
to your machine, use this option input the IP address of the\n\
proxy for the purpose of SCP. \
\n-b [NUM] Amount of backup versions to keep. The default is 3. \
\n-d [DIRECTORY] Destination backup directory. This is where your directory\n\
will be copied to. The default is ~/TEST. \
\n-p [PORT] Proxy port number. If you are using a proxy to login remotely\n\
to your machine, use this option input the port used on the \n\
proxy for the purpose of SCP. \
\n-s [DIRECTORY] Source backup directory. This is the directory you wish to\n\
back up. The default is ~/TEMP. \
\n-t [DIRECTORY] Path to archive. This is the location for the creation of the\n\
.tar.gz archive. The default location is ~/. \
\n \
\n-A Disable the creation of a .tar.gz archive. \
\n-B Enable unlimited backup versions.\n"
exit 0
}
while getopts ":s:d:b:t:a:p:ABh" options; do
case "${options}" in
s)
src=${OPTARG}
;;
d)
dest=${OPTARG}
;;
b)
b=${OPTARG}
wholeNum
;;
t)
targz=${OPTARG}
archiveTar
;;
a)
ip=${OPTARG}
validFormatIP
;;
p)
port=${OPTARG}
validNetPort
;;
A)
A=1
;;
B)
B=1
;;
h)
help
;;
:)
echo -e "\n[Err: -${OPTARG} requires an argument]\n"
abort_abnormal
;;
*)
echo -e "\n[Err: Invalid option specified]"
echo -e "\nPlease use -h for an overview of usable options.\n"
abort_abnormal
;;
esac
done
# Change directory to source directory
cd $src
# Check if target directory already exists, and act accordingly
if [ -d $dest/$dir ]; then
### If the directory exists, ask user what to do
echo -e "\nThe directory $dest/$dir already exists."
diff=`diff -r $dest/$dir $src/` # Source/destination comparison
err=$? # diff exit code extraction
if [ $err == 0 ]; then
echo -e "\nNo changes have been detected between $dest/$dir and $src"
read -r -n 1 -t 60 -p "What would you like to do? [O]verwrite, \
[A]bort: " input
case $input in
[oO])
overwrite
;;
*)
abort_normal
;;
esac
elif [ $err == 1 ]; then
read -r -n 1 -t 60 -p "What would you like to do? [O]verwrite, \
[U]pdate, [A]bort: " input
case $input in
[oO])
overwrite
;;
[uU])
update
;;
*)
abort_normal
;;
esac
else
echo -e "\n[Err: Failed to execute diff]\n"
abort_abnormal
fi
else
### If target directory doesn't exist create it and copy files over
echo -e "\nCopying files to local backup directory: $dest/$dir"
mkdir -p $dest/$dir
cp -r . $dest/$dir
fi
# Create tar archive for offline backup
if [ "$A" == "0" ]; then
echo -e "\nCreating archive for download: $targz"
tar -czf $targz -C $dest/$dir .
fi
# Remove the oldest backup directories
match=`echo $dir | cut -c1-4`
d=`ls -l $dest | grep ^d | awk '{print $9}' | grep ^$match | wc -l` # Directory counter
while [ $d -gt $b -a "$B" == "0" ]
do
echo -e "\nRemoving old backup directory: $dest/\
`ls -ltr $dest | awk '{print $9}' | head -n2 | tail -1`"
rm -rf $dest/`ls -ltr $dest | awk '{print $9}' | head -n2 | tail -1`
d=$((d-1))
done
# Download insctructions
if [ "$port" == "22" -o "$port" == "" ]; then # Port checker
port=""
else
port="-P $port "
fi
if [ "$A" == "0" ]; then
echo -e "\n===============================================================\n"
echo -e "Please download `basename $targz` to another machine for backup.\n\
Use the following command on your local machine:\n\n\
$(tput setaf 2)$(tput setab 0) scp $port`whoami`@$ip:\
`realpath $targz --relative-to=$p` . $(tput sgr 0)"
echo -e "\n===============================================================\n"
else
echo -e "\r"
fi
# Return to original pwd
cd - > /dev/null