Sync Hacks: How to sync without data loss using BTSync & Raspberry Pi

278672002_160acfd80c_bSource

In Sync Hacks, we spotlight cool uses of Sync from the creative minds of our users. Sync is our free, unlimited, and secure file-syncing application. If you have an interesting use or how-to, shoot us an email at sync[at]bittorrent.com. Can’t wait to hear what you guys cook up.

In this week’s edition of Sync Hacks, Jack Minardi shows us how to “create your own Dropbox” using BitTorrent Sync and Raspberry Pi. He needed to keep his notes synced safely – without worrying about data loss. So, he turned to BitTorrent Sync: the safest way to keep files up to date across devices. Read on to see how he did it!

Or visit his original post here.

sync-hacks-2

The Problem

I like to take long running notes, jotting down an idea here and there when
one pops up. I never know which devices I will have with me, so I like to have
these notes synced across all of them.

Previously I had been using the Chrome plugin QuickNote which is a light weight interface on top of the diigo cloud service. I had been using it for about two years and never had a problem until a few days ago. I somehow managed to lose about three months of edits. I still have not quite shaken that feeling of despair knowing I will never get my data back, but it did finally force me to beef up my note-syncing system. While I will primarily be using this setup to sync notes, it is by no means limited to only text files.

Enter BitTorrent Sync

BitTorrent Sync is a free utility that uses the bittorrent protocol to keep folders in sync across devices. It can be used with OS X, Windows, Android and Linux. It is not however open source, which might be a deal breaker for some. But if this isn’t too big a pill for you to swallow, with a little bit of work you can use btsync as a free syncing solution.

While Dropbox requires you to keep a copy of your data on their servers, btsync never requires your data to inhabit a device you do not own. This is nice for security, but it means to sync between two devices they both need to be online at the same time. That is where the Raspberry Pi comes in. You can use the Pi as a node in your sync network, so a change to a file on any device will sync with the Pi and then other devices will get the change when they come online.

First we will set up the Pi as the central server and then we will connect our devices to it. (It is important to note however that the Pi is not a special “server” node, it is just a regular node like any other device.)

Installing BitTorrent Sync on the Raspberry Pi

NOTE: I am using Raspbian Wheezy, but I suspect these ideas will translate to
other operating systems.

You need to grab the ARM
build
of
BitTorrent Sync:

mkdir ~/.btsync && cd ~/.btsync
wget http://btsync.s3-website-us-east-1.amazonaws.com/btsync_arm.tar.gz
tar -xfv btsync_arm.tar.gz

Unless you have a large SD in your Pi you will probably want to use an external
drive for your sync location. I will be using a USB thumb drive. You may need
to format your thumb drive as EXT4 if you are having issues. (WARNING! This
will erase all data on your drive
)

df -h                               # find your drive here, e.g. `sda1`
sudo umount /dev/sda1               # replace sda1 with your drive name !
sudo mkfs.ext4 /dev/sda1 -L BTSync  # replace sda1 with your drive name !

Now all you have to do is launch the btsync application and you will be up and
running!

cd ~/.btsync
sudo ./btsync  # can be killed with `sudo killall btsync`

If you see the following output btsync is properly running.

BitTorrent Sync forked to background. pid = 3003

Navigate your browser to Your-Pi-IP-Address:8888/gui to manage the btsync
process. To add the thumb drive select “Add Folder” and navigate to
/media/BTSync. You will need to generate a secret as this is the first time
you are adding the folder.

btsync-web-interface
add-folder

Other Devices

Now go download the Android app and/or the desktop app and connect them using the secret you just generated It’s that simple! Any change on any device will be synced across all online devices. If you keep your Pi online it will store and push the most up to date content as your other devices go on and off line.

Extra Credit

Start at Boot

You may want to set btsync to start when you boot your Raspberry Pi. To do that
we will place a script in /etc/init.d/ and then register it with
update-rc.d.

sudo nano /etc/init.d/btsync

Paste the following code in the script

#! /bin/sh
# /etc/init.d/btsync
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
    /home/pi/.btsync/btsync
    ;;
stop)
    killall btsync
    ;;
*)
    echo "Usage: /etc/init.d/btsync {start|stop}"
    exit 1
    ;;
esac

exit 0

Then change the permissions, test, and register it to run at boot:

sudo chmod 755 /etc/init.d/btsync
sudo /etc/init.d/btsync start       # test that the script starts
sudo /etc/init.d/btsync stop        # test that the script stops
sudo update-rc.d btsync defaults

Password Protect Web Interface

If you expose your web interface to the outside world (or if you don’t trust
your friends) you are going to want to password protect it. This can be done
with a btsync config file passed to the executable at runtime with the
--config flag. First, use btsync to generate a sample config file, modify it
to fit your needs, and restart the process.

cd ~/.btsync
./btsync --dump-sample-config > btsync.conf
# browse the sample config file and change what you want
sudo killall btsync
sudo ./btsync --config btsync.conf

HINT: Use jsonlint to validate your config file if
btsync complains. Also make sure to modify the /etc/init.d/btsync script to
use the config file as well.

Questions? Hit me up on twitter (@jackminardi)

Jack Minardi (@jackminardi) is a scientific software developer and hardware hacker from Austin, TX. He is interested in product development, user experience design, and expanding the human-computer interface. You can find his personal blog here: jack.minardi.org

Lou
Written by: Lou

Lou is a product marketing manager at BitTorrent, Inc., where he works on BitTorrent Sync, a free app that allows you to sync and share large files with anybody via secure, distributed technology.

 Related Posts:
  • Pingback: Roll Your Own Dropbox Clone With A Raspberry Pi And BitTorrent Sync | Lifehacker Australia

  • http://kamensk.ws 1ka3

    In Raspmbc not have “killall”, but have “killall5″.
    Help, plz, how me setup btsync on boot?

    • John

      BTSync syncs in real time. So it wouldn’t need a “Sync at boot” command per say, it would just start syncing automatically.

  • Plukkert

    Thank you…! Found another really easy one today… http://www.rasptorial.com/storage/bittorrent-sync

  • Seairth Jacobs

    I encountered a conflict during update-rc with minidlna. I fixed it by adding the following tags to the btsync script:

    ### BEGIN INIT INFO
    # Provides: btsync
    # Required-Start: $local_fs $network
    # Required-Stop: $local_fs $network
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Startup script for BitTorrent Sync
    # Description: Startup script for BitTorrent Sync
    ### END INIT INFO

    • http://tiki50s.blogspot.com.es/ Nacho

      I solved the conflict during update-rc thanks the tags of Seaith but now minidlna is not compatible with btsync. When I am viewing a movie wiht DLNA the movie stop in about 5 minutes. If I stop the script it works well.

  • http://zechdc.com Zechdc

    In the tutorial it says

    “If you keep your Pi online it will store and push the most up to date content as your other devices go on and off line.”

    I thought every drive being synced to had to be the same size/have the enough space available. If you place a 1GB thumb drive on the Pi and you are syncing 5GB from other devices, won’t that be an issue. I know in this case you are only syncing notes, so maybe you don’t need much space. So will it actually “push the most up to date content as your other devices go on and off line” or is it just syncing everything like Dropbox would?