最終的には Wi-Fi LAN、というか LAN 接続(ネットワークマウント)が途切れたら再接続して、Time Machine を再起動させたいがここは簡便に、10分おきに launchd で Time Machine が動いてるかチェックして必要なリソースをマウントして Time Machine を再起動させることにする。
とにかくいつ止まるか分からないバックアップは、たとえ手動で再開できても骨が折れるものです。
#!/bin/bash
#set -vx
#set -e
trap '/bin/rm /tmp/watchTimeMachin;exit 2' 2 3 9 15
machinead='192.168.200.26'
sharedir='TTMD%20\(usb\)'
mountpoint='/Volumes/Data'
bacupmachine='Tk2MBP'
timemachine_name='Time Machine Backups'
logs='/tmp/watchTimeMachine.log'
flag='/tmp/watchTimeMachine'
if test -e $flag; then
  /bin/echo "$0 is Running. exit" >> $logs 2>&1
  /bin/rm $flag
exit 1
else
  /usr/bin/touch $flag
fi
if test ! -e $logs; then
  /usr/bin/touch $logs
  /bin/chmod 666 $logs
  /bin/date >> $logs
fi
status=`/usr/bin/tmutil status | /usr/bin/grep -c "Running = 1"`
if test $status -ne 0; then
  /bin/echo "status is $status. Time Machine is Running. exit" >> $logs
  /bin/rm $flag
  exit $status
else
  if test -d "/Volumes/$timemachine_name"; then
    /usr/bin/tmutil startbackup --destination `/usr/bin/tmutil destinationinfo | /usr/bin/awk -F : '/ID/ {print $2}'` >>$logs 1>&2
    /bin/rm $flag
    exit 0
  fi
fi
if test ! -d $mountpoint; then
  /bin/mkdir $mountpoint
fi
/sbin/mount | grep "$machinead/TTMD" | /usr/bin/grep smbfs >/dev/null
if test $? -eq 1; then
# ここは shell 変数を使ってやるとどうしてもクォートで囲まれてうまくいかないので、直書きにした
  /sbin/mount_smbfs //GUEST:@192.168.200.26/TTMD%20\(usb\) /Volumes/Data 1>>$logs 2>&1
fi
/sbin/mount | grep $bacupmachine > /dev/null
if test $? -eq 1; then
  /usr/bin/hdiutil attach -noverify "$mountpoint/$bacupmachine.sparsebundle" 1>>$logs 2>&1
fi
err=$?
if test $err -ne 0; then
  /bin/echo "mount was failed. error is $err" >>$logs 2>&1
  /bin/rm $flag
exit $err
fi
/bin/date >> $logs
volume_stat=`/usr/bin/tmutil status | grep 'DestinationMountPoint = "/Volumes/$timemachine_name"'`
if test $volume_stat -ne 0; then
  /bin/echo "invalid volume mounting" >>$logs 1>&2
  /bin/rm $flag
  exit $volume_stat
else
# Thanks http://d.hatena.ne.jp/daidai7/20130225/1361806444
  sudo tmutil setdestination -a "/Volumes/$timemachine_name"
fi
/usr/bin/tmutil startbackup --destination `/usr/bin/tmutil destinationinfo | /usr/bin/awk -F : '/ID/ {print $2}'` >>$logs 1>&2
echo "status is $status. TimeMachine is Starting." >> $logs 1>&2
/bin/rm $flag
exit 0
確認は、
$ launchctl list link.bye-bipolar.watchTimeMachine.plist
{
 "Label" = "link.bye-bipolar.watchTimeMachine.plist";
  "LimitLoadToSessionType" = "Aqua";
  "OnDemand" = true;
  "LastExitStatus" = 0;
  "TimeOut" = 30;
  "StandardOutPath" = "/var/watchTimeMachine.log";
  "StandardErrorPath" = "/var/watchTimeMachine_error.log";
  "ProgramArguments" = (
  "/bin/bash";
  "/Users/yj2t/tools/watchTimeMachine.sh";
  );
};
で。「"OnDemand" = true;」を指定しているので、load されるや否や登録したプログラムが起動される。
終了は、
$ launchctl unload /Library/LaunchDaemons/link.bye-bipolar.watchTimeMachine.plist
これで Time Machine でバックアップしてるのを忘れて他の作業ができるようになりました!
ただ、頻繁にWi-Fi LAN が切断(?Time Machine がアンマウント)されるので、それを知る方法は宿題にする。
↓
- 参考にしたサイト
- MAC PERSON
- daidai7の日記
- sound of rudeness
- jmblog.jp
※コメント投稿者のブログIDはブログ作成者のみに通知されます