29 February 2012

How to set up Box.com autosync on Ubuntu

I was one of the lucky ones who got my hands on a free 50 GB account on Box.com (former Box.net) with their campaign to launch their new Android app (the campaign runs until March 23, 2012). A similar campaign ran earlier with their IPhone/IPad app. Unfortunately, they don't offer a Linux sync app, so here I describe, step by step, how I did some ninja tricks in order to be able to automatically synchronize a certain directory on my harddrive with my Box.com account from my Ubuntu 11.10 (Oneiric Ocelot) installation.

Note: Use with caution and make a backup of your data before attempting this. Especially be careful if adding more than one computer to the same Box account. The last added computer might convince rsync that there are no files, and thus delete files that are already on your Box account.
If you don't feel like experimenting with this, I can recommend the fairly new similar service Copy.com which comes with a Linux sync app and gives you 15 GB for free (invite your friends and get up to 50 GB in total). If you follow this link, you'll get 20 GB initially.

The trick is this: Using WebDav you can mount your Box.com account as if it was a normal harddisc. Then, once mounted, set up an rsync script to synchronize your selected local directory with the WebDav mount. Personally, I use it to keep an always updated online backup of my family photograph directory, which tends to take up several gigabytes, and counting. Just remember, Box.com will only allow each file to take up a maximum of 100 MB for free accounts. Something similar to this procedure will also work for other services that offer WebDav connections, e.g. One.com.

This how-to might work on other versions of Ubuntu, too (maybe even on other flavours of Linux), perhaps with a few changes here and there, or it might not work for you at all, even though your setup is exactly the same as mine. Use with caution, and at your own risk. Be sure to backup your data before attempting this. But here goes...

A. Install and set up WebDav

  1. Open a terminal window, and install the WebDav file system package:
    sudo apt-get install davfs2
  2. Make sure you can mount as your normal user (not just root)
    sudo chmod u+s /sbin/mount.davfs
    sudo usermod -a -G davfs2 mylinuxusername
    Of course, change "mylinuxusername" into your linux user name.
  3. Tell the system how and where to mount the Box.com account. Edit the /etc/fstab file as root:
    sudo gedit /etc/fstab
    Add this line to the file:
    https://dav.box.com/dav /home/mylinuxusername/box.com davfs rw,user,auto 0 0
  4. Let the system know your Box.com user name and password to enable automatic mounting. Edit the /etc/davfs2/secrets file as root and add this line:
    https://dav.box.com/dav    myboxusername    myboxpassword
  5. Tell WebDav not to use locks, as this won't work with Box.com. Edit the /etc/davfs2/davfs2.conf file as root and add this line:
    use_locks 0
  6. Make the mountpoint (an empty directory) for the Box.com account, and mount for the first time:
    cd ~
    mkdir box.com
    mount box.com

B. Create a sync script and make it execute once an hour

  1. Create a script that synchronizes your local directory of choice with the Box.com account. Create a new text file somewhere which will contain the command(s) to do the sync. I suggest ~/bin/boxcomsync . Add the following lines to the file:
    #!/bin/bash
    rsync -avr --delete /home/mylinuxusername/directorytosync/ /home/mylinuxusername/box.com/
    This will put the files in the root of your Box.com account. You can also create a directory in Box.com and sync the files to there instead. If you want several local directories synced, you can repeat the rsync line for every directory.
  2. Make the script executable:
    chmod u+x ~/bin/boxcomsync
  3. Test your script to see if everything works as expected. Also, if you have a lot of data to sync, it is probably best to do the first run manually, so that the automated runs will only handle changes to the synced files.
    ~/bin/boxcomsync
  4. Set up a cronjob to run the sync script every hour. Run the command:
    crontab -e
    A text editor will appear (probably nano). Add the following line to the file which has been opened, save, and exit the editor.
    0 * * * * /home/mylinuxusername/bin/boxcomsync
    See e.g. this guide with examples of how to set other times. Don't set it too often, though, as one run must have time to finish before another is executed.

24 comments:

  1. fantastic guide worked perfectly... much appreciated

    ReplyDelete
  2. Thanks a lot!!
    Is working perfect and for me is very usefull. Thanks again!.

    Tota

    ReplyDelete
  3. Is this work offline or we must connected to internet to access the file on box folder..?

    ReplyDelete
    Replies
    1. The sync script in the last part downloads your box files to your local harddrive and keeps them synchronized so that you can access your files even without being online. Of course, you need to be online to run the syncscript, though.

      Also remember to backup your box files before following this guide, just in case.

      Delete
  4. nice tutorial, I used it here in brasil, thanks
    I wold like to know, how can I set up de speed of download and up load, it is automatic?

    thanks again

    ReplyDelete
    Replies
    1. The script/tutorial on this page does not take transfer speed into account. That is, it uses the full speed available to it, though of course not faster than the slowest speed on the route from your computer to the Box.com server containing your files. So, if e.g. the Box.com servers are currently slow, so will your up/download be.

      Delete
  5. I had to add the "c" parameter to rsync:

    rsync -avrc --delete /home/mylinuxusername/directorytosync/ /home/mylinuxusername/box.com/

    This causes rsync to synchronize by checksum, not by modification time. Without that, it would re-upload every file on every synchronization.

    The downside: it needs longer to find the files that must be synchronized.

    ReplyDelete
    Replies
    1. I had tried this. But, it gets stuck at "sending incremental file list"

      Delete
  6. Im trying to setup a remote repo on my box.com account and this is exactly what i was looking for.Many thanks!

    ReplyDelete
  7. Gracias por la ayuda funcionó muy bien en Mint 13.

    thanks for you help, it works very well in Linux Mint 13

    ReplyDelete
  8. You may want to add a file to the Box.com mount then check for it's existence before running the rsync. I'm doing the following:

    first:

    touch /mnt/Box.com/MOUNTED

    Then edit ~/bin/boxcomsync to look like this


    #!/bin/bash

    if [ -f /mnt/Box.com/MOUNTED ];
    then
    echo "Box WEBDAV is mounted."
    rsync -avrc --delete /mnt/Box.com/somedirectory /home/linuxusername/Box/somedirectory

    else
    echo "Box WEBDAV is not mounted."
    fi

    ReplyDelete
  9. This works fine if you only use one machine, but if the file is not in the synced directory it DELETES it from box

    ReplyDelete
    Replies
    1. Thank you for mentioning this. I put a warning in the text above.

      Delete
  10. Great solution... but... does this works both ways? If I put some new information into my Box account from another device (Cell or PC) will it refresh my local folder?

    ReplyDelete
    Replies
    1. To be honest, I haven't tested this, as my needs only includes syncing in one direction. A quick Google search reveals that it might not, though. Perhaps this can be solved using something like this suggestion: http://goo.gl/7cN5V ?

      Delete
  11. Someone wrote a comment about how to revert this change. Unfortunately I accidentally removed the comment. He/she wasn't able to boot the computer after following this guide.

    As far as I can see this must be caused by the system trying to unsuccessfully mount the box drive at boot time. To disable this, you need to remove the added line from /etc/fstab. This is of course hard to do if you can't boot the computer, but it's not impossible.

    Booting into Ubuntu's Recovery Mode (see e.g. http://goo.gl/7k3fr) should allow you to do this. If not, try booting the computer as long as it gets to, then see if you can ssh into it from another computer. Otherwise you might need to physically connect the harddisk to a working computer and do the change from there.

    ReplyDelete
  12. Works perfectly for me!
    Thanks :)

    ReplyDelete
  13. ohhh Fine info

    Also visit my homepage - analytics

    ReplyDelete
  14. Server webdav address has changed. You must replace:

    https://www.box.com/dav

    for this

    https://dav.box.com/dav

    ReplyDelete
  15. give me also for dropbox

    ReplyDelete
  16. Hi-

    Works for me!

    But then, How to send a email out with the URL to the file location from the Ubuntu server..

    Any inputs on this.. ?

    ReplyDelete
  17. Looks like the Box URL has changed to https://dav.box.com/dav, if you wish to keep this post current. Thanks!

    ReplyDelete
  18. Please give Box URL . https://dav.box.com/dav URL not working.
    When i tried to login with credentials, i got the XML with exception given below :


    Sabre_DAV_Exception_NotImplemented
    GET is only implemented on File objects


    ReplyDelete
  19. i was getting an error, the way i solved it is...
    sudo chmod 600 /etc/davfs2/secrets
    i couldnt mount the box.com place...

    ReplyDelete