-
Notifications
You must be signed in to change notification settings - Fork 1
/
laptopbackup.sh
executable file
·316 lines (266 loc) · 11.1 KB
/
laptopbackup.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
########################################################################
#
# laptopbackup.sh - my personal laptop backup script
#
# written by Jason Baker (jason@onejasonforsale.com)
# on github: https://github.com/codercowboy/scripts
# more info: http://www.codercowboy.com
#
########################################################################
#
# UPDATES:
#
# 2017/05/02
# - Initial version
#
########################################################################
#
# Copyright (c) 2012, Coder Cowboy, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied.
#
########################################################################
if [ "" = "`env | grep MY_USER`" ]; then
echo "ERROR: MY_USER system var isn't set."
exit 1
fi
if [ "" = "`env | grep MY_SERVER`" ]; then
echo "ERROR: MY_SERVER system var isn't set."
exit 1
fi
MY_USER_HOME=/Users/${MY_USER}
SERVER_RSYNC_TARGET_DIR="${MY_USER}@${MY_SERVER}:/external/Backup/Laptop Backup"
LOCAL_BACKUP_DIR="${MY_USER_HOME}/Documents/Backup"
LOCAL_RSYNC_TARGET_DIR="${LOCAL_BACKUP_DIR}/Laptop Backup"
LOCAL_MUSIC_FOLDER="${MY_USER_HOME}/Documents/not time machine/Music"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}"
FLAG_BACKUP_LOCAL="false"
FLAG_BACKUP_CLEAN="false"
FLAG_BACKUP_REMOTE="false"
FLAG_BACKUP_USB="false"
if [ "${1}" = "LOCAL_ONLY" ]; then
FLAG_BACKUP_LOCAL="true"
FLAG_BACKUP_CLEAN="true"
elif [ "${1}" = "REMOTE_ONLY" ]; then
FLAG_BACKUP_REMOTE="true"
elif [ "${1}" = "USB_ONLY" ]; then
FLAG_BACKUP_USB="true"
elif [ "${1}" = "FULL" ]; then
FLAG_BACKUP_CLEAN="true"
FLAG_BACKUP_LOCAL="true"
FLAG_BACKUP_REMOTE="true"
elif [ "${1}" = "USB" ]; then
FLAG_BACKUP_CLEAN="true"
FLAG_BACKUP_LOCAL="true"
FLAG_BACKUP_USB="true"
elif [ "${1}" = "CLEAN" ]; then
FLAG_BACKUP_CLEAN="true"
else
echo
echo "USAGE: laptopbackup.sh [MODE]"
echo " MODE OPTIONS: FULL, USB, USB_ONLY, LOCAL_ONLY, REMOTE_ONLY"
echo
echo " FULL - LOCAL_ONLY + remote backup"
echo " LOCAL_ONLY - CLEAN + backup locally only"
echo " USB - LOCAL_ONLY + USB"
echo " REMOTE_ONLY - backup from pre-existing local backup to remote, don't backup to local first"
echo " USB_ONLY - backup from pre-existing local backup to usb, don't backup to local first"
echo " CLEAN - clean out maven targets"
echo
exit 1
fi
function my_rsync() {
rsync -vrthW --del --stats --progress --chmod=u=rwx "${@}"
}
#make for's argument seperator newline only
IFS=$'\n'
function run_backup_job() {
TARGET_DIR="${1}"
RSYNC_ARGS="${2}"
echo "Backing up to: ${TARGET_DIR}"
mkdir -p "${TARGET_DIR}"
my_rsync ${RSYNC_ARGS} "${LOCAL_BACKUP_DIR}/" "${TARGET_DIR}/backup/"
md5tool.sh CREATE "${MY_USER_HOME}/Pictures/"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Pictures/" "${TARGET_DIR}/Pictures/"
md5tool.sh CREATE "${MY_USER_HOME}/Music/GarageBand/"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Music/GarageBand/" "${TARGET_DIR}/GarageBand/"
md5tool.sh CREATE "${MY_USER_HOME}/Movies"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Movies/" "${TARGET_DIR}/Movies/"
md5tool.sh CREATE "${MY_USER_HOME}/Dropbox"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Dropbox/" "${TARGET_DIR}/Dropbox/"
md5tool.sh CREATE "${MY_USER_HOME}/Documents/win98vm"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Documents/win98vm/" "${TARGET_DIR}/win98vm/"
md5tool.sh CREATE "${MY_USER_HOME}/Library/Application Support/MobileSync"
my_rsync ${RSYNC_ARGS} "${MY_USER_HOME}/Library/Application Support/MobileSync/" "${TARGET_DIR}/MobileSync/"
#md5tool.sh CREATE "/Applications/Wine"
#my_rsync ${RSYNC_ARGS} "/Applications/Wine" "${TARGET_DIR}/wine/"
}
# arg 1 is target drive
function backup_music() {
TARGET_MUSIC_FOLDER=""
if [ ! -e "${LOCAL_MUSIC_FOLDER}" ]; then
echo "Not backing up music, can't find expected local music dir: ${LOCAL_MUSIC_FOLDER}"
return
fi
if [ -e "${1}/Music" ]; then
TARGET_MUSIC_FOLDER="${1}/Music"
elif [ -e "${1}/Archive/Garbage/Music" ]; then
TARGET_MUSIC_FOLDER="${1}/Archive/Garbage/Music"
else
echo "Not backing up music, can't find target music folder."
return
fi
echo "[Backing up music]"
echo " Source music Dir: ${LOCAL_MUSIC_FOLDER}"
echo " Target music dir: ${TARGET_MUSIC_FOLDER}"
my_rsync "${LOCAL_MUSIC_FOLDER}/" "${TARGET_MUSIC_FOLDER}/"
echo "[Finished Backing up music]"
}
if test ${FLAG_BACKUP_USB} = "true"; then
echo "[Starting USB Backup Step.]"
USB_DEST=""
for FILE in `find /Volumes -name "USB*" -maxdepth 1`; do
echo "Back up to ${FILE}? ('YES' to select, enter to skip)"
echo -n "> "
read ANSWER
if [ "${ANSWER}" = "YES" ]; then
USB_DEST="${FILE}"
break
fi
echo "Skipping this drive. You did not answer 'YES', you said: ${ANSWER}"
done
if [ "${USB_DEST}" = "" ]; then
echo "Skipping backup to USB, could not find valid USB target to backup to."
exit 1
else
echo "Backing up to ${USB_DEST}"
fi
fi #end usb dest selection section
# arg 1 is file
function remove_file() {
if [ -e "${1}" ]; then
echo "Removing file: ${1}"
rm -Rf "${1}"
fi
}
if [ "${FLAG_BACKUP_CLEAN}" = "true" ]; then
echo "[Starting Clean Backup Step.]"
for FILE in `find ${CODE} -type d -name target`; do
echo "Removing: ${FILE}"
rm -Rf "${FILE}"
done
for FILE in `find ${CODE} -type d -name node_modules -d 3`; do
echo "Removing: ${FILE}"
rm -Rf "${FILE}"
done
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/data.old"
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/data/log"
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/data/hyte/v4/cfg/b"
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/data/hyte/v4/sched"
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/data/hyte/v4/msg"
remove_file "${CONSOLE_HOME}/webapp-parent/webapp-itests/etc.old"
echo "[Finished Clean Backup Step.]"
fi
if [ "${FLAG_BACKUP_LOCAL}" = "true" ]; then
echo "[Starting Local Backup Step.]"
echo "backing up misc"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}/System/"
MISC_FOLDER="${LOCAL_RSYNC_TARGET_DIR}/System/misc"
rm -Rvf ${MISC_FOLDER}
mkdir -p ${MISC_FOLDER}
cp /etc/hosts ${MISC_FOLDER}/
cp /etc/profile ${MISC_FOLDER}/
cp /etc/paths ${MISC_FOLDER}/
cp ${MY_USER_HOME}/.bash_profile ${MISC_FOLDER}/
cp ${MY_USER_HOME}/.bash_logout ${MISC_FOLDER}/
cp ${MY_USER_HOME}/setupenv.sh ${MISC_FOLDER}/
cp ${MY_USER_HOME}/.gitconfig ${MISC_FOLDER}/
cp -r ${CODE}/tools/git ${MISC_FOLDER}/
cp -r ${MY_USER_HOME}/.ssh ${MISC_FOLDER}/
cp -r ${MY_USER_HOME}/.vnc ${MISC_FOLDER}/
cp -r ${MY_USER_HOME}/.subversion ${MISC_FOLDER}/
cp -r ${MY_USER_HOME}/.config/filezilla ${MISC_FOLDER}/
cp ${MY_USER_HOME}/.m2/settings.xml ${MISC_FOLDER}/maven.settings.xml
mkdir -p ${MISC_FOLDER}/sublimetext
cp -r "${MY_USER_HOME}/Library/Application Support/Sublime Text 3/Packages/User" "${MISC_FOLDER}/sublimetext"
rm ${MISC_FOLDER}/automator_services.zip
zip -r ${MISC_FOLDER}/automator_services.zip "${MY_USER_HOME}/Library/Services/"
brew leaves > ${MISC_FOLDER}/brewlist.txt
defaults read /Library/Preferences/com.apple.TimeMachine SkipPaths > ${MISC_FOLDER}/timemachine_excludes.txt
find /Applications -maxdepth 1 | sort > "${MISC_FOLDER}/apps.txt"
date > "${LOCAL_RSYNC_TARGET_DIR}/backupdate.txt"
echo "backing up code"
my_rsync "${MY_USER_HOME}/Documents/code" "${LOCAL_RSYNC_TARGET_DIR}/"
echo "backing up system stuff (thunderbird, mail, messages, voice memos, fonts)"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}/System/"
my_rsync "${MY_USER_HOME}/Library/Thunderbird" "${LOCAL_RSYNC_TARGET_DIR}/System/"
my_rsync "${MY_USER_HOME}/Library/Mail" "${LOCAL_RSYNC_TARGET_DIR}/System/"
my_rsync "${MY_USER_HOME}/Library/Messages" "${LOCAL_RSYNC_TARGET_DIR}/System/"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}/voicememos/"
my_rsync "${MY_USER_HOME}/Library/Application Support/com.apple.voicememos/" "${LOCAL_RSYNC_TARGET_DIR}/System/voicememos/"
my_rsync "${MY_USER_HOME}/Library/Fonts" "${LOCAL_RSYNC_TARGET_DIR}/System/"
echo "backing up downloads"
my_rsync "${MY_USER_HOME}/Downloads" "${LOCAL_RSYNC_TARGET_DIR}/"
echo "backing up games (minecraft, terraria, factorio)"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}/Games/"
mkdir -p "${LOCAL_RSYNC_TARGET_DIR}/Games/Minecraft"
my_rsync "${MY_USER_HOME}/Library/Application Support/minecraft/screenshots/" "${LOCAL_RSYNC_TARGET_DIR}/Games/Minecraft/screenshots/"
my_rsync "${MY_USER_HOME}/Library/Application Support/minecraft/saves/" "${LOCAL_RSYNC_TARGET_DIR}/Games/Minecraft/saves/"
my_rsync "${MY_USER_HOME}/Library/Application Support/Terraria" "${LOCAL_RSYNC_TARGET_DIR}/Games/"
my_rsync "${MY_USER_HOME}/Library/Application Support/factorio" "${LOCAL_RSYNC_TARGET_DIR}/Games/"
echo "Creating md5 in ${LOCAL_RSYNC_TARGET_DIR}"
md5tool.sh CREATE "${LOCAL_RSYNC_TARGET_DIR}"
echo "[Finished Local Backup Step.]"
else
echo "Skipping local backup step."
fi #end local backup section
if [ "${FLAG_BACKUP_REMOTE}" = "true" ]; then
echo "[Starting Remote Backup Step.]"
run_backup_job "${SERVER_RSYNC_TARGET_DIR}" "-e ssh"
echo "[Finished Remote Backup Step.]"
else
echo "Skipping rsync to remote step."
fi #end blunx backup section
if [ "${FLAG_BACKUP_USB}" = "true" ]; then
HOSTNAME=`hostname -s`
USB_BACKUP_DIR="${USB_DEST}/Laptop Backup/${HOSTNAME}"
echo "[Starting USB Backup Step.]"
echo "Backing up to USB drive: ${USB_BACKUP_DIR}"
mkdir -p "${USB_BACKUP_DIR}"
run_backup_job "${USB_BACKUP_DIR}" ""
backup_music "${USB_DEST}"
md5tool.sh CHECKALL "${USB_BACKUP_DIR}/"
echo "[Finished USB Backup Step.]"
else
echo "Skipping backup to usb step."
fi #end usb backup section
echo "fixing permissions"
chmod -R 700 "${LOCAL_RSYNC_TARGET_DIR}"
chown -R ${MY_USER} "${LOCAL_RSYNC_TARGET_DIR}"
BACKUP_SIZE=`du -h -d 0 "${LOCAL_RSYNC_TARGET_DIR}"`
echo "local backup size: ${BACKUP_SIZE}"
echo "local hd status: `df -H / | grep -v Capacity | awk '{print $4;}'`"
echo "finished in ${SECONDS} seconds at `date`"
exit 0