Skip to content

Commit 29303b8

Browse files
committed
Time Machine Safe Backup to NAS using caffeinate and NoSleepCtrl
1 parent 5ce9ef5 commit 29303b8

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tmutil-safe-nas-backup

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
function tmrunning() {
4+
return $([ $(tmutil currentphase) != 'BackupNotRunning' ])
5+
}
6+
7+
function echostate() {
8+
DATE=$(date +"%Y-%m-%d %H:%M:%S")
9+
STATE=$1
10+
shift
11+
OPT=""
12+
if [ "$STATE" = "-n" ]; then
13+
OPT=$STATE
14+
STATE=$1
15+
shift
16+
fi
17+
echo $OPT "-$STATE|$DATE|" $*
18+
}
19+
20+
function tmrun() {
21+
hash NoSleepCtrl 2>/dev/null
22+
HASNOSLEEP=$?
23+
if [ $HASNOSLEEP ]; then
24+
NOSLEEPSAVESTATE=$(NoSleepCtrl -v 0 -g)
25+
NoSleepCtrl -s 1
26+
echostate I "Engaging NoSleep."
27+
fi
28+
echostate I "Starting backup."
29+
tmutil startbackup
30+
echostate -n I "Waiting for backup to come up ... "
31+
32+
MAXWAIT=60
33+
for I in `seq 1 $MAXWAIT`; do
34+
if tmrunning; then
35+
break;
36+
fi
37+
echo -n "#"
38+
sleep 1;
39+
done
40+
echo ""
41+
42+
if [[ $I == $MAXWAIT ]]; then
43+
echostate E "Backup did not come up within 60 seconds."
44+
else
45+
while tmrunning; do
46+
NEWPHASE=$(tmutil currentphase)
47+
if [ "$PHASE" != "$NEWPHASE" ]; then
48+
echostate I "Current phase: $NEWPHASE"
49+
PHASE=$NEWPHASE
50+
fi
51+
sleep 10
52+
done
53+
54+
if [ $HASNOSLEEP ]; then
55+
echostate I "Returning NoSleep to original state."
56+
NoSleepCtrl -s $NOSLEEPSAVESTATE
57+
fi
58+
59+
echostate I "Finished backup."
60+
fi
61+
}
62+
63+
if tmrunning; then
64+
echostate W "Backup is already running. Exiting ..."
65+
exit 1
66+
fi
67+
68+
if [[ $# == 0 ]]; then
69+
caffeinate $0 RUN
70+
else
71+
tmrun
72+
fi

0 commit comments

Comments
 (0)