-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtmutil-safe-nas-backup
executable file
·76 lines (67 loc) · 1.75 KB
/
tmutil-safe-nas-backup
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
#!/bin/bash
# Author: http://brablc.com/
# Repo: https://github.com/brablc/clit/blob/master/tmutil-safe-nas-backup
#
# Requirements: NoSleepCtrl from http://code.google.com/p/macosx-nosleep-extension/issues/detail?id=28#c8 - take Volanins built (see the issue)
function tmrunning() {
return $([ $(tmutil currentphase) != 'BackupNotRunning' ])
}
function echostate() {
DATE=$(date +"%Y-%m-%d %H:%M:%S")
STATE=$1
shift
OPT=""
if [ "$STATE" = "-n" ]; then
OPT=$STATE
STATE=$1
shift
fi
echo $OPT "-$STATE|$DATE|" $*
}
function tmrun() {
hash NoSleepCtrl 2>/dev/null
HASNOSLEEP=$?
if [ $HASNOSLEEP ]; then
NOSLEEPSAVESTATE=$(NoSleepCtrl -v 0 -g)
NoSleepCtrl -s 1
echostate I "Engaging NoSleep."
fi
echostate I "Starting backup."
tmutil startbackup
echostate -n I "Waiting for backup to come up ... "
MAXWAIT=180
for I in `seq 1 $MAXWAIT`; do
if tmrunning; then
break;
fi
echo -n "#"
sleep 1;
done
echo ""
if [[ $I == $MAXWAIT ]]; then
echostate E "Backup did not come up within 60 seconds."
else
while tmrunning; do
NEWPHASE=$(tmutil currentphase)
if [ "$PHASE" != "$NEWPHASE" ]; then
echostate I "Current phase: $NEWPHASE"
PHASE=$NEWPHASE
fi
sleep 10
done
echostate I "Finished backup."
fi
if [ $HASNOSLEEP ]; then
echostate I "Returning NoSleep to original state."
NoSleepCtrl -s $NOSLEEPSAVESTATE
fi
}
if tmrunning; then
echostate W "Backup is already running. Exiting ..."
exit 1
fi
if [[ $# == 0 ]]; then
caffeinate $0 RUN
else
tmrun
fi