-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpBackups.sh
executable file
·188 lines (175 loc) · 5.44 KB
/
cpBackups.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
#!/bin/bash
#
## @file cpBackups.sh
## @brief Copy/Move/Remove Time Machine backups with bypass command
## @author Ronald Joe Record (rr at ronrecord dot com)
## @copyright Copyright (c) 2014, Ronald Joe Record, all rights reserved.
## @date Written 28-Mar-2015
## @version 1.0.1
##
## cpBackups - Copy Time Machine backups with bypass command
## mvbackups - Move Time Machine backups with bypass command
## rmBackups - Remove Time Machine backups with bypass command
##
## Depending on how this command is invoked (cpBackups, mvBackups, or rmBackups)
## or what arguments are supplied on the command-line, this will copy, move,
## or remove Time Machine backups using the bypass command
##
## A simple "mv ..." or even "sudo mv ..." wouldn't work on Time Machine
## backups as I got many many "operation not permitted" failures.
## Same for attempted copies and removal. Use of the bypass command avoids this.
#
OLD_BACKUP_DB="/Volumes/My_Book_Studio/Backups.backupdb"
NEW_BACKUP_DB="/Volumes/LaCie_8TB/Backups.backupdb"
BYPASS="/System/Library/Extensions/TMSafetyNet.kext/Contents/Helpers/bypass"
SHOWME=
SHOW_USAGE=
CMD=`basename $0`
COMMAND=
COMWORD=
CP_CMD="cp -a"
MV_CMD="mv"
RM_CMD="rm -rf"
## @fn usage()
## @brief Display command line usage options
## @param none
##
## Exit the program after displaying the usage message and example invocations
usage() {
printf "\nUsage: $CMD [-cmr] [-n] [-b path-to-bypass]\n"
printf "\t[-s path-to-source-db] [-d path-to-destination-db] [-u]\n"
printf "\nWhere:\n"
printf "\t-c indicates to copy backup database\n"
printf "\t-m indicates to move backup database\n"
printf "\t-r indicates to remove backup database\n"
printf "\t-n indicates no execution, just tell me what you would do\n"
printf "\t-b path-to-bypass specifies the location of the bypass command\n"
printf "\t-s path-to-source-db specifies the previous backup location\n"
printf "\t-d path-to-destination-db specifies the new backup location\n"
printf "\t-u prints this usage message and exits\n"
printf "\nWith these arguments I would execute the following command:\n"
printf "\nsudo ${BYPASS} ${COMMAND} ${OLD_BACKUP_DB} ${NEW_BACKUP_DB}\n\n"
exit 1
}
while getopts b:cd:mrs:nu flag; do
case $flag in
b)
BYPASS="$OPTARG";
;;
c)
[ "${COMMAND}" ] && {
printf "\nYou can only specify one of -c, -m, and -r"
printf "\nExiting.\n"
usage
}
COMMAND="${CP_CMD}";
COMWORD="copy"
;;
d)
NEW_BACKUP_DB="$OPTARG";
;;
m)
[ "${COMMAND}" ] && {
printf "\nYou can only specify one of -c, -m, and -r"
printf "\nExiting.\n"
usage
}
COMMAND="${MV_CMD}";
COMWORD="move"
;;
r)
[ "${COMMAND}" ] && {
printf "\nYou can only specify one of -c, -m, and -r"
printf "\nExiting.\n"
usage
}
COMMAND="${RM_CMD}";
COMWORD="remove"
;;
s)
OLD_BACKUP_DB="$OPTARG";
;;
n)
SHOWME=1;
;;
u)
SHOW_USAGE=1;
;;
esac
done
shift $((OPTIND - 1));
[ "${COMMAND}" ] || {
# Default to how we were invoked
case $CMD in
cpBackups)
COMMAND="${CP_CMD}";
COMWORD="copy"
;;
mvBackups)
COMMAND="${MV_CMD}";
COMWORD="move"
;;
rmBackups)
COMMAND="${RM_CMD}";
COMWORD="remove"
;;
esac
}
[ "${COMMAND}" ] || {
printf "\nMust be invoked as cpBackups, mvBackups, or rmBackups."
printf "\nExiting.\n"
usage
}
# Check location of bypass command
[ -x "${BYPASS}" ] || {
# Uh oh, maybe Apple moved it again. Try to locate bypass.
POSSIBLE=`locate /bypass | grep -v xml | grep -v html`
[ -x "${POSSIBLE}" ] || {
printf "\n\n$BYPASS does not exist or is not executable.\nExiting.\n"
usage
}
BYPASS="${POSSIBLE}"
}
# Check location of backup directory to move
[ -d "${OLD_BACKUP_DB}" ] || {
printf "\n\n$OLD_BACKUP_DB does not exist or is not a directory."
printf "\nExiting.\n"
usage
}
# Check location of backup directory destination (mv or cp only)
[ "${COMMAND}" = "${RM_CMD}" ] || {
[ -d "${NEW_BACKUP_DB}" ] && {
printf "\n\n$NEW_BACKUP_DB already exists."
printf "\nExiting.\n"
usage
}
}
[ "${SHOW_USAGE}" ] && usage
printf "\nUsing ${BYPASS}\nto ${COMWORD} Time Machine backups at:"
printf "\n\t${OLD_BACKUP_DB}\n"
[ "${COMMAND}" = "${RM_CMD}" ] || {
printf "to\n\t${NEW_BACKUP_DB}\n"
}
[ "$SHOWME" ] || {
while true
do
read -p "Are you sure you want to proceed ? (y/n) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) printf "\nExiting.\n"; usage;;
* ) echo "Please answer yes or no.";;
esac
done
}
[ "$SHOWME" ] && {
printf "\n\nWould execute the following command:\n"
printf "\nsudo ${BYPASS} ${COMMAND} ${OLD_BACKUP_DB} ${NEW_BACKUP_DB}\n\n"
exit 0
}
printf "\nWill ${COMWORD} Time Machine backups at:"
printf "\n\t${OLD_BACKUP_DB}\n"
[ "${COMMAND}" = "${RM_CMD}" ] || {
printf "to\n\t${NEW_BACKUP_DB}\n"
}
printf "\nThis make take a while.\n"
sudo ${BYPASS} ${COMMAND} "${OLD_BACKUP_DB}" "${NEW_BACKUP_DB}"