How to automatically mount and umount Apple Time Capsule on Linux

How to automatically mount and umount Apple Time Capsule on Linux

Even though you may not own a Macbook, an iPhone/iPad or any other Apple device, you might still be the owner of a Time Capsule. I bought it when I was using a Macbook Pro. Time Capsule is wonderfully integrated with the various Apple devices and machines. Unfortunately, it becomes a pain to use Time Capsule under you boot GNU/Linux.

It is possible to manually mount a Time Capsule Volume on a directory. It is uncomfortable, isn’t it? Using Time Machine clones such as Déjà Dup, flyback or TimeVault becomes difficult. They may be automatically started by your Desktop Environment. However, they may give errors because of the still unmounted Time Capsule Volume.
Additionally,

  • What if the mount point (e.g. /mnt/timecapsule) exists but it is empty because of the non-mounted Time Capsule? Such backup programs would backup in our hard disk!
  • We could create a script th7at mounts Time Capsule on login.
    What if we don’t always use our laptop at home? There must be a way to discover if a Time Capsule is present on the current network and mount it.
  • The Time Capsule Volume must be unmounted when we logout, otherwise there will be an error if we are connected to a Wireless Network. Network volumes are sometimes unmounted after Network Manager is stopped, under some configurations.

I decided to handle these issues with a script, called timecapsule-handler (Download, gzip).

How to Download, Configure, and Run

I wrote this section especially for those unfamiliar with the GNU/Linux console. It is written keeping Ubuntu as reference distribution.
Experienced users may use any GNU/Linux distribution and they only need to know that:

  1. cifs-utils is needed in order to use the script
  2. The script should be under your $PATH and be invoked with root privileges (sudo)
  3. The script must be called just after network setup and before network teardown. Time Capsule likes clean umount.

First, download the script.

Edit the configuration variables with a text-editor. For your convenience, here is how to edit it with Ubuntu default graphical text editor:

You need to set values for the first three variables:

Save the file, give it execution permissions and move it in a directory under your $PATH (e.g., /usr/local/bin).

Install the required cifs-utils package.

That’s it. Here is how to use the script.
To mount Time Capsule, run:

To un-mount Time Capsule, run

Yes, it automatically detects everything.

How does it work?

Let’s observe it:

At line 31, we simply look if Time Capsule is already mounted, by calling the mount command. Typically, it will return something like:

See the last line of the command? In line 31 of the script we search in the output of the mount command the string $MOUNT_POINT (e.g, /mnt/timecapsule).

Then, between line 34 and line 36, if Time Capsule Volume is mounted, we umount it and delete the mount point using rmdir to be sure to protect data if the umount is unsuccessful.

Otherwise, between line 37 and line 42, we use the smbclient program to do a sort of ping of the device on the Network. If there is someone at the given IP, and this someone replies with something like “I am Time Capsule!”, we are sure to mount it. This is a typical output of the smbclient program:

The program gives a result within a second, that’s because the script is so fast in doing its job.
There may still be issues with my script. However, it would not do anything harmful to the system and fail silently in case of errors.

I hope that you may find it useful. Please report your experience in this post comments.

If for any reason the script does not work, I encourage you to try this older version.

45 Comments

  1. Hey man.
    Thanks for the script! I’m going to test it out tomorrow.
    I’ll let you know how it goes!

    Reply
  2. You accidently gzipped the file twice. After running gunzip on the download, I needed to append “.gz” and run gunzip again.

    Reply
    • Thank you for the report. This is quite strange, because

      $ wget /wp-content/uploads/2011/10/timecapsule-handler.gz
      --09:18:17-- /wp-content/uploads/2011/10/timecapsule-handler.gz
      =>
      timecapsule-handler.gz'
      Resolving ineed.coffee... 94.75.235.227
      Connecting to ineed.coffee[94.75.235.227]:80... connected.
      HTTP request sent, awaiting response... 200 OK
      Length: 893 [text/plain]

      100%[========================================>] 893 --.--K/s

      09:18:17 (8.52 MB/s) - timecapsule-handler.gz' saved [893/893]

      aeshma:tm dgraziotin$ gunzip timecapsule-handler.gz
      aeshma:tm dgraziotin$ ls
      timecapsule-handler
      aeshma:tm dgraziotin$ file timecapsule-handler
      timecapsule-handler: Bourne-Again shell script text executable

      Reply
  3. Thanks much, pal! your script worked like a charm, excellent and very simple!

    Reply
  4. Brilliant!

    Reply
  5. That’s pretty cool.
    I’m probably going to steal this for auto mounting usb drives without a DE as well :)
    Just have to find out what kind of events are fired on usb plugging/unplugging.
    Anyway; a nice read, thanks.

    Reply
    • Sure you can steal it. Just remember to respect its license ;)

      I guess the most simple plan is to learn some udev rules.
      See this related superuser question.

      If you come out with a nice idea, share it with the rest of the world. Good luck!

      Reply
      • Thanks.
        I’ll be sure to put it up here if I make anything worthwhile :)

        Reply
  6. Thanks for this script!! I can’t believe Apple left us high and dry.

    I can’t get it to work though. I keep getting messages saying

    “sh timecapsule-handler
    mount error(13): Permission denied
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)”

    why does it hate me?

    Reply
    • Are you calling the script either as root user or by appending sudo before it?

      Reply
      • Hey.

        Nice work, though I’m also having this issue. Appending sudo before script call.

        Any ideas?

        Reply
        • I Have the same problem? Do you have any solution?

          Thanks.

          Reply
  7. Great script man! Thanks! Care putting it on GitHub or so where it will be persistent for generations of users to come :-)?

    Reply
  8. This is a great and much needed project, but I’m in some hot water over it.

    At first I ran it and the program seemed to work, though I could never get it to mount the time capsule. My disk was located at /10.0.1.1/Disk, which is what I indicated in the file. After some time, I realized that though I could not seem to access the time capsule within Linux, after running timecapsule-handler I could see it in my Windows virtual machines that run within linux. So that was great news.

    The probablem now is that I restarted the computer and it stops in the middle of booting every time now with a black screen. Nothing happens unless I press a function key, at which point it says

    mount.cifs: bad UNC (/10.0.1.1/Disk)
    mountall: mount /media

    and that’s it. I’ve tried about 100 things to recover, including SystemRescueCD, SuperGrubDisk 2, and Rescatux…

    Reply
    • I have never tested this script on a virtual machine. Is this error still happening on the virtual machine?
      According to your description, I image the following settings:

      Is this what you are using?
      Are you running the script after the network has been setup? Are you also running it on shutdown before the network is taken down?
      What happens if you unplug your time capsule and try to boot Linux?

      Hope this helps

      Reply
  9. Hello, I’m trying to make this work on my ubuntu (so i can get rid of w7 for good) but i can’t get it done. I filled my TIme Caspule configs and tried to run sudo timecapsule-handler – it tells me “command not found” or if i i’m under root it tells “Permission denied”.

    I’m a newbie in linux… could anyone give a little help?

    Reply
    • Hello, did you install cifs-utils?

      Reply
      • Yes, after that i stucked.

        Actually i forgot to mention two things: first, the link for the script is broken (it doesn’t extract), i had to download fro github and then i could write de configs. Two: I’m using wubi under windows 7 (it’s a shame, i know)

        Thanks in advance

        Reply
      • I did it! I was reading around about linux terminal commands, and about the ctrl+alt+F1 shortcut. So i went to the “text-only” mode and tried again with sudo… After that, i just opened mnt and there it is! Beautiful…

        Thanks for the script man, it’s THAT good!

        Reply
        • Glad it worked for you as well!
          Regarding the script hosted on my website: it works for me, strange..

          Reply
  10. I’m having the same problem Joao had. I followed all of the instructions and when I run “sudo timecapsule-handler”, “sudo: timecapsule-handler: command not found” is returned. I’m new to linux and am unsure how to get this working. Thanks for the help in advance. I did install cifs-utils.

    Reply
    • Now I get it! I was not clear enough in my post. I assumed that people reaching the post would have been familiar with the terminal. The issue is not having the script in your $PATH.

      The script must have execution permission. Run the following command while being in the same folder where timecapsule-handler is.

      In order to use the script just by calling its file name (the sudo timecapsule-handler command), you must move timecapsule-handler in a directory included in your $PATH (e.g., /usr/bin/ and /usr/local/bin).

      Otherwise, to run the script you have to be in the same directory where the script is and run:

      You can also provide the full path where the script is

      I updated the post to clarify this. Hope it fixes your issue.

      Reply
      • Thanks for the help! That fixed it. People like you are why I’m a linux user now.

        Reply
  11. thanks dgraziotin for the scipt , it work for me in ubuntu 12.10 but when I tried to used it in ubuntu 13.04 the script didnt work. so I add a sec=ntlm to the script and works fine in 13.04
    so the last four line of the modified script are:

    I take a while figure it out for me looking in the web. hope this will help others.

    Reply
    • Franco,

      You are correct, the sec=ntlm helped me out. Without that I can’t write to the folder on fedora 16.

      Thanks!
      Gary

      Reply
  12. “The script must be called just after network setup and before network teardown. Time Capsule likes clean umount.”

    How would I accomplish this part?

    Reply
  13. Man, this is great! You saved me hours of work.

    I was running into an issue when while trying to mount my time capsule as a NAS, but for some reason Ubuntu Server 12.04 kept throwing the error:

    Retrying with upper case share name
    mount error(6): No such device or address
    Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

    The way I was trying to mount it was this:
    sudo mount.cifs //192.168.1.1/Data/ /media/capsule -o pass=password

    Your script probably does something else to make this work. I’ll have to have a closer look at it later. Anyway, thanks for this awesome script!

    Reply
  14. Just an FYI. This script does not work for Fedora 19

    Reply
    • Thank you for reporting this. You could be even of more help if you provide eventual error messages you receive.

      Reply
      • I had to add the above mentioned “sec=ntlm” after the password in the mount command for it to work in fedora 19. There were no other issues.

        Thanks for the script!

        Reply
  15. Very nice man.

    Working on Ubuntu 13.04, just need to add “sec=ntlm” on the line of the mount, like that:

    echo “mount.cifs $TIMECAPSULE_PATH $MOUNT_POINT -o pass=$TIMECAPSULE_PASSWORD,file_mode=0777,dir_mode=0777,sec=ntlm” | /bin/bash

    Reply
  16. Thanks wershlak and Roberto for the addition!

    Reply
  17. You also need smbclient. With Ubuntu 12.04 this is not installed with cifs-utils.

    apt-get install smbclient

    Reply
  18. The addition of “,sec=ntlm” to the options is also required in more recent 12.04 versions.

    In case you can’t find the volume name, start the air-port utility on your mac and look under ‘disks’.

    Proper quoting prevents the need for escaping of special characters. Here’s my version of the script:


    TIMECAPSULE_IP="" # e.g. "192.168.1.100"
    TIMECAPSULE_VOLUME="/Time Capsule" # also try "/Data"
    export PASSWD='YOURPASSWORDHERE' # No need to escape anything (except "'")
    MOUNT_POINT="/mnt/timecapsule" # no need to create the directory

    IS_MOUNTED=mount 2> /dev/null | grep "$MOUNT_POINT" | cut -d' ' -f3
    TIMECAPSULE_PATH="//$TIMECAPSULE_IP$TIMECAPSULE_VOLUME"

    if [[ "$IS_MOUNTED" ]] ;then
    umount $MOUNT_POINT
    rmdir $MOUNT_POINT
    else
    CHECK_TIMECAPSULE=smbclient --no-pass -L "$TIMECAPSULE_IP" 2>&1 > /dev/null | grep -m1 -i apple
    if [[ "$CHECK_TIMECAPSULE" =~ "Apple" ]] ;then
    mkdir "$MOUNT_POINT"
    mount -t cifs "$TIMECAPSULE_PATH" "$MOUNT_POINT" -o file_mode=0777,dir_mode=0777,sec=ntlm
    fi
    fi
    unset PASSWD

    Reply
    • Thank you for providing this more elegant solution!

      Reply
  19. Works on Slackware 14.1 : thank you very much !

    Reply
    • I have just switched to Slackware 14.1 as well. Happy to hear that it works for you!

      Reply
  20. Wow, thanks!
    I am embarrassed to say how much time I chased this problem….
    You saved me.

    Reply
  21. what did I do wrong?

    [root@centos bin]# timecapsule-handler
    mkdir: cannot create directory /mnt/timecapsule': File exists
    /bin/bash: line 1: unexpected EOF while looking for matching

    /bin/bash: line 2: syntax error: unexpected end of file
    [root@centos bin]#

    Reply
  22. config file:

    TIMECAPSULE_IP=”192.168.1.2″ # e.g. “192.168.1.100″
    TIMECAPSULE_VOLUME=”PEDRO’s Time Capsule/Data” # also try “/Data”
    TIMECAPSULE_PASSWORD=”passw” # prefix special characters, e.g. \!
    MOUNT_POINT=/mnt/timecapsule # no need to create the directory

    IS_MOUNTED=mount 2> /dev/null | grep "$MOUNT_POINT" | cut -d' ' -f3
    TIMECAPSULE_PATH=”//$TIMECAPSULE_IP$TIMECAPSULE_VOLUME”

    if [[ "$IS_MOUNTED" ]] ;then
    umount $MOUNT_POINT
    rmdir $MOUNT_POINT
    else
    CHECK_TIMECAPSULE=smbclient --no-pass -L $TIMECAPSULE_IP 2>&1 > /dev/null | grep -m1 -i apple
    if [[ "$CHECK_TIMECAPSULE" =~ "Apple" ]] ;then
    mkdir $MOUNT_POINT
    echo “mount.cifs $TIMECAPSULE_PATH $MOUNT_POINT -o pass=$TIMECAPSULE_PASSWORD,file_mode=0777,dir_mode=0777,sec=ntlm” | /bin/bash
    fi
    fi

    Reply
    • Try to escape PEDRO’s -> PEDRO\’s.

      Otherwise, try to rename your Time Capsule to something simpler, e.g. “TIMECAPSULE” and change the config file to use the simpler name.

      Reply
  23. Hi, nice job and thank you very much for this script! I have just one more problem: run this script automatically at the login and logout. Is it possible?

    Reply
    • Sure! Just follow any tutorial regarding running programs and scripts at login/logout for your desktop environment. For example, see autostart for KDE here. Just remember that the script needs root privileges when run. See this as an example.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">