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…?
Mac – Enable/disable Time Machine depending on network connection type
automationNetworkscripttime-machine
Best Answer
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:
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 isstatus: active
, like this: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 the0
to the number next toen
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 saysmyusername
, substitute your own Mac username.⌘+s to save. In the Save property sheet, set the
File Format
asApplication
, and check the box forStay open after run handler
. Save it wherever you like - Desktop, or Applications Folder - it really doesn't matter, just know where you saved it.Step 3: Create the Shell Scripts
Next, open Terminal. Type the following commands:
Paste the following line in:
Press control+x, type y and press return to save and exit. Then run this command:
And in this file paste the following line:
Again, control+x, then y and return to save and exit.
Next, enter these commands:
Step 4: Setting Up
sudo
to Run Without a PasswordLetting 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:
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.Here, add the following line:
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):
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:
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).