9

I've not got a great Wi-Fi setup here at home and it sometimes results in failed Time Machine backups. Is there a way of specifying that Time Machine only backs up under certain circumstances - i.e. Ethernet connection is active...?

  • Would manually running time machine be an option? – Thorbjørn Ravn Andersen Aug 25 '13 at 15:43
  • I'm not sure what 'manually running' means - if it's enabled it will run anyway, I need to turn it on / off according to network configs, ideally. – babelmonk Aug 26 '13 at 15:50
  • The Time Machine icon in the status bar has a "Backup now" menu entry when right clicked. – Thorbjørn Ravn Andersen Aug 26 '13 at 17:13
6

This is an alternative to the other script I posted. This one runs in the background and tests the network connection every two minutes to determine if it is using an Ethernet connection or wireless. If on Ethernet, it enables Time Machine; when the connection switches to wireless it disables Time Machine.

Step 1: System Check

While connected to Ethernet, you will need to run one command manually to confirm which interface Ethernet is assigned to. Run this command:

ifconfig

It should output a screen full of information. What you are looking for is a section labeled en with a number after it, and whose last line is status: active, like this:

en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
    ether 00:25:00:ef:30:3c 
    inet6 fe80::225:ff:feef:303c%en1 prefixlen 64 scopeid 0x5 
    inet 192.168.1.68 netmask 0xffffff00 broadcast 192.168.1.255
    media: autoselect (100baseTX <full-duplex>)
    status: active

Note the number next to en at the beginning - this is the interface that your Ethernet connection is running on.

Step 2: Create the AppleScript Application

In the script below, where it says set wired_interface to "0", change the 0 to the number next to en in the above output. (It should be 0; if you are on a Mac Pro, it may be 1.) Also in the below script, at the top where it says myusername, substitute your own Mac username.

-- Edit variables here --
global current_username
set current_username to "myusername"
global wired_interface
set wired_interface to "0"

-- DO NOT EDIT BELOW THIS LINE --
global last_connection
set last_connection to "wireless"

on idle
    if (do shell script "ifconfig en" & wired_interface & " | awk '/inet/ {print $2}'") is not equal to "" then
        -- on ethernet
        if last_connection is equal to "wireless" then
            -- turn TM on
            -- else do nothing, we're still on ethernet
            set last_connection to "ethernet"
            do shell script "sudo /Users/" & current_username & "/TMSwitch/TM_On.csh"
        end if
    else
        -- on wireless
        if last_connection is equal to "ethernet" then
            -- turn tm off
            -- else do nothing, we're still on wireless
            set last_connection to "wireless"
            do shell script "sudo /Users/" & current_username & "/TMSwitch/TM_Off.csh"
        end if
    end if
    return 120
end idle

+s to save. In the Save property sheet, set the File Format as Application, and check the box for Stay open after run handler. Save it wherever you like - Desktop, or Applications Folder - it really doesn't matter, just know where you saved it.

AppleScript Editor Save dialog box showing the File Format set to Application, and box checked for Stay open after run handler

Step 3: Create the Shell Scripts

Next, open Terminal. Type the following commands:

cd ~/
mkdir TMSwitch
cd TMSwitch
pico TM_On.csh

Paste the following line in:

defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool TRUE

Press control+x, type y and press return to save and exit. Then run this command:

pico TM_Off.csh

And in this file paste the following line:

defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool FALSE

Again, control+x, then y and return to save and exit.

Next, enter these commands:

chmod 4555 TM_O*.csh
chown root TM_O*.csh

Step 4: Setting Up sudo to Run Without a Password

Letting the Terminal command sudo run without a password can be very dangerous. That's why the steps above created the shell scripts in their own directory, so what can actually be run is limited.

Enter the following command in Terminal:

sudo pico /etc/sudoers

Then enter your administrator password when prompted.

This may bring you to a (mostly) blank screen, or it may have some text in it. If it's blank - that's fine. You'll just paste the below line at the top. If text already exists, that's also fine; use your down arrow to go right below the lines already in the # User privilege specification section, as seen in the below screenshot.

Terminal screenshot showing the sudoers file open in Pico

Here, add the following line:

<yourusername> ALL = NOPASSWD: /Users/<yourusername>/TMSwitch/*

In both places where <yourusername> appears, replace it with your Mac username. Press control + x, type y and press return to save and exit.

Test that these files turn Time Machine on and off by running the following command (assuming Time Machine is currently on):

sudo ./TM_Off.csh

After a moment the Time Machine icon in the menubar should turn gray, indicating Time Machine is turned off. (You may need to click the icon for it to reflect the change). Assuming this works, run this command:

sudo ./TM_On.csh

And Time Machine should be re-enabled.

And Off You Go

Run the application you created in AppleScript Editor above, and it will remain open, enabling and disabling Time Machine as your connection switches from Ethernet to wireless and back. To disable switching, simply close the AppleScript application (right-click on the icon in the Dock and choose Quit).

  • Wow, this is amazing - I'll be sure to look into it and paste my results here - I may even stick it in GitHub (with due credit of course)! – babelmonk Aug 27 '13 at 18:10
  • There's a race condition here: when you connect to WiFi you have a two-minute window in which TM might start before the script notices and turns it off. – Reid Aug 27 '13 at 19:48
  • 1
    Rather than triggering this every 2 minutes, you can trigger it whenever there is a change in the network configuration. You just have to use launchd and tell it to look for any changes to the /Library/Preferences/SystemConfiguration/ folder. I have an example of this available at github.com/tjluoma/onnetworkchange. – TJ Luoma Aug 28 '13 at 16:20
6

This AppleScript will check if the Ethernet connection is active, and then run a Time Machine backup if so.

First, disable Time Machine in System Preferences. The below method will run it automatically.

While connected to Ethernet, you will need to run one command manually to confirm which interface Ethernet is assigned to. Run this command:

ifconfig

It should output a screen full of information. What you are looking for is a section labeled en with a number after it, and whose last line is status: active, like this:

en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
    ether 00:25:00:ef:30:3c 
    inet6 fe80::225:ff:feef:303c%en1 prefixlen 64 scopeid 0x5 
    inet 192.168.1.68 netmask 0xffffff00 broadcast 192.168.1.255
    media: autoselect (100baseTX <full-duplex>)
    status: active

Note the number next to en at the beginning - this is the interface that your Ethernet connection is running on. In the script below, where it says en9, replace 9 with the number from above. (If you have a Mac Pro, it will most likely be en0 or en1. Otherwise, it will likely be en0.)

if (do shell script "ifconfig en9 | awk '/inet/ {print $2}'") is not equal to "" then
    do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper &"
end if

Save this as EthernetTimeMachine.scpt into your home folder (/Users/<yourusername>/).

Next open up Terminal and type the following command:

pico ~/crontab

This will open up a text editor to allow you to schedule this script to run on an hourly basis, just like Time Machine does by default. Paste this line:

@hourly osascript ~/<yourusername>/EthernetTimeMachine.scpt

Press control+X then type y and press return to save and exit.

To disable it, go back into Terminal, again use the command:

pico ~/crontab

Then delete the line you added above and use the same steps to save and exit.

  • Ah - this is awesome - thanks so much! One question - do I need to disable time machine? Or have it enabled? Surely if it's enabled it'll still run on its own schedule? – babelmonk Aug 26 '13 at 15:46
  • A thought on similar lines (sorry for double post) - could you not run something like this: lwgat.blogspot.co.uk/2013/02/… to enable / disable time machine, and run this rather than @hourly but on network change event? – babelmonk Aug 26 '13 at 15:51
  • You would want to disable Time Machine in its settings before doing this. As far as utilizing a network change event and/or enabling/disabling Time Machine, that's definitely doable. I'll take a look tonight and throw something together in another answer. – tubedogg Aug 26 '13 at 18:22
  • The one disadvantage to detecting a network change is you have to have something constantly running in the background to detect it. – tubedogg Aug 26 '13 at 21:49
-1

You can open you Network Prefs and make sure Ethernet is the top connection in the service order. You can see what I mean in this screenshot, I hope.

Then, if Ethernet is connected you should always use it as your primary connection.

See here for how to set the service priority: http://osxdaily.com/2013/06/15/set-network-connection-priority-mac-os-x/

  • Thanks, I do understand that piece. The problem is I explicitly need to disable backups when ethernet is not connected. I guess I could write an AppleScript to en/disable it (something like this: hints.macworld.com/article.php?story=20040222210724156 grepping from ifconfig en0)... – babelmonk Aug 25 '13 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .