├── LICENSE ├── README.md ├── btsync.sh └── configuration-files ├── btsync └── config.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Moritz Dietz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![BitTorrent Sync loves Raspberry Pi](https://i.imgur.com/0lTAi8l.png) 2 | Raspberry Pi + Sync Script 3 | ======= 4 | 5 | This is a script to manage your [BitTorrent Sync](https://www.getsync.com/) installation on your [Raspberry Pi](https://www.raspberrypi.org/). 6 | 7 | ### Why should I be using this script? 8 | 9 | **EDIT:** You shouldn't! Please check [help.getsync.com](https://help.getsync.com) for information on how to add the repository and install Sync using apt. 10 | 11 | There used to be a non-official repository people have been using to install BitTorrent Sync on their machines. 12 | The creator and maintainer of that repository has moved on and left his project to the community trying to fix bugs themselves. 13 | It was a headache for everybody involved since things didn't work as expected and nobody was really digging into it. 14 | 15 | Since I was testing and troubleshooting BitTorrent Sync a lot and had to re-install and update it on my machines, I wanted to have it a little bit nice and automated. 16 | People on the forum have frequently been asking for advice on how to setup, install and maintain BitTorrent Sync on their Raspberry Pis. Especially Linux beginners didn’t really know how to do that. This gave me a good reason to publish my script for all to share and use. 17 | 18 | ### Does BiTorrent Inc. host an official repository? 19 | Yes, they do. They have been working on it for a while and now they have finally published a blog post telling users that they can use their official repository. Make sure to check out their blogpost [here](http://blog.getsync.com/2016/02/18/official-linux-packages-for-sync-now-available). 20 | 21 | ### What will this script do? 22 | 23 | This script will do the following things when you run it: 24 | * Ask if you want to install, update, backup or remove an already existing installation 25 | * Update, backup and remove only work when BitTorrent Sync was installed with this script 26 | * Download the latest public available version of the ARM binary from BitTorrent Inc.'s website 27 | * Make sure to have ```curl``` installed 28 | * Extract the downloaded binary to a predefined location ```/usr/bin/btsync``` 29 | * Create an ```/etc/init.d/btsync``` script so you can easily start/stop it 30 | * Create a configuration file for it to run from ```/etc/btsync/config.json``` 31 | * Create a data directory for it's databases etc. in ```/home//.btsync/``` 32 | 33 | Backups will be created in ```~//``` directory using this format ```btsync_backup_dd-mm-yyyy_hh-mm-ss.tar.gz``` 34 | 35 | ### Will this work on Raspbian Jessie? 36 | As of right now this script only works on Wheezy since it uses init.d and does not support systemd. 37 | There is an error message after you run it on Raspbian Jessie 38 | ```Failed to start btsync.service: Unit btsync.service failed to load: No such file or directory.``` 39 | Feel free to fork it if you want to enhance support! I also love seeing PRs :) 40 | 41 | 42 | ### Instructions 43 | 44 | * SSH into your Raspberry Pi 45 | * Download the script to a folder where you keep all your scripts or just ```~/``` 46 | ``` 47 | curl -O https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/btsync.sh 48 | ``` 49 | * Make the script executable 50 | ``` 51 | chmod +x btsync.sh 52 | ``` 53 | * Run the script 54 | * If you are running the script as a different user than ```pi```, which is the default, the script will ask you if you want to install it as that user instead 55 | ``` 56 | ./btsync.sh 57 | ``` 58 | You can also provide a direct link to an ARM binary that has been posted from the Administrators on the forum, but is not yet available through the https://getsync.com website. 59 | For example ``` ./btsync.sh https://download-cdn.getsync.com/2.3.2/linux-arm/BitTorrent-Sync_arm.tar.gz ``` 60 | Please be mindful: by using other URLs/links I can not garantuee that the script will work like expected! 61 | Only use links provided by the Sync Staff on the forums or from the official BitTorrent Inc. website. 62 | 63 | BitTorrent Sync will use a default configuration. Please make sure that it will work in your environment. 64 | [Here](http://help.getsync.com/hc/en-us/articles/204762689-Running-Sync-in-configuration-mode) and [here](http://help.getsync.com/hc/en-us/articles/207371636-Power-user-preferences) are help articles from the [Sync Help Center](http://help.getsync.com/) that will guide you through the configuration options. 65 | 66 | After the script is done, you want to have it automatically start after you reboot your Raspberry Pi. 67 | To do that enter the following:```sudo update-rc.d btsync defaults``` 68 | 69 | ##### Where can I find information on fixes and features? 70 | Please take a look at the [releases section](https://github.com/moritzdietz/pi-syncscript/releases) to see where this little project is headed and currently at. 71 | 72 | If you have any kind of productive contribution to this project, create a pull request! 73 | I would love to see people improving it. 74 | 75 | **Disclaimer** 76 | 77 | This site is not affiliated, endorsed or supported by BitTorrent Inc. in any way. The use of information and software provided on this website may be used at your own risk. The information and software available on this website are provided as-is without any warranty or guarantee. By visiting this website you agree that: (1) We take no liability under any circumstance or legal theory for any software, error, omissions, loss of data or damage of any kind related to your use or exposure to any information provided on this site; (2) All software are made “AS AVAILABLE” and “AS IS” without any warranty or guarantee. All express and implied warranties are disclaimed. Some states do not allow limitations of incidental or consequential damages or on how long an implied warranty lasts, so the above may not apply to you. 78 | -------------------------------------------------------------------------------- /btsync.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | red=`tput setaf 1` 4 | green=`tput setaf 2` 5 | yellow=`tput setaf 3` 6 | reset=`tput sgr0` 7 | stat_y=${yellow}\?${reset} 8 | stat_x=${red}X${reset} 9 | stat_ok=${green}OK${reset} 10 | err_cmd="" 11 | 12 | if [ "$(id -u)" == "0" ]; then 13 | echo "[$stat_x] Error: This script cannot run as root" 14 | exit 1 15 | fi 16 | # 17 | # Don't alter the variables below. If you don't want to install the script as pi the script will ask you if you want to install it as the currently logged in user. 18 | # Default is pi 19 | # 20 | if [ "$(whoami)" != "pi" ]; then 21 | echo "[$stat_x] Non-Default user $(whoami) detected. Default user to install script as is pi" 22 | read -r -p "[$stat_y] Do you want to install the script as the user $(whoami) [yes/no]: " response 23 | if [[ $response =~ ^([yY][eE][sS])$ ]]; then 24 | user="$(whoami)" 25 | echo "[$stat_ok] User variable set to $(whoami)" 26 | fi 27 | else 28 | user="pi" 29 | fi 30 | btsdir="/home/$user/.btsync/data/.syncsystem" 31 | 32 | fp= 33 | if [ ! -z $1 ];then 34 | dllink="$1" 35 | elif [ $(uname -m) == "armv7l" ]; then 36 | fp=hf 37 | fi 38 | 39 | default_dllink="https://download-cdn.getsync.com/stable/linux-arm$fp/BitTorrent-Sync_arm$fp.tar.gz" 40 | 41 | function stop_sync { 42 | echo "[$stat_y] Trying to stop a running BitTorrent Sync instance" 43 | sleep 0.5 44 | syncpid="$(ps aux | grep btsync | grep -v grep | grep -v /bin/bash | awk '{print $2}')" 45 | if [ -z $syncpid ]; then 46 | echo "[$stat_ok] There is no instance of BitTorrent Sync running " 47 | sleep 0.5 48 | else 49 | err_cmd=$(sudo pkill -15 -x btsync 2>&1 >/dev/null) 50 | if [ $? -ne 0 ]; then 51 | echo "[$stat_x] Error: There was an error stopping the BitTorrent Sync instance" 52 | if [ ! $err_cmd=="" ]; then 53 | echo "[$stat_x] $err_cmd" 54 | fi 55 | sleep 0.5 56 | exit 1 57 | else 58 | echo "[$stat_ok] The running instance of BitTorrent Sync has been stopped (PID: $syncpid)" 59 | sleep 0.5 60 | fi 61 | fi 62 | } 63 | 64 | function install_preperations { 65 | if [ ! -d "$btsdir" ]; then 66 | echo "[$stat_ok] Trying to create installation folder ($btsdir)" 67 | sleep 0.5 68 | err_cmd=$(mkdir -p $btsdir 2>&1 >/dev/null) 69 | if [ $? -ne 0 ]; then 70 | echo "[$stat_x] Error: Could not create installation folder $btsdir" 71 | echo "[$stat_x] $err_cmd" 72 | exit 1 73 | fi 74 | echo "[$stat_ok] BitTorrent Sync installation folder has been created ($btsdir)" 75 | sleep 0.5 76 | else 77 | echo "[$stat_y] BitTorrent Sync installation folder $btsdir already exists" 78 | sleep 0.5 79 | read -r -p "[$stat_y] Delete all files and sub-folders inside $btsdir? [Y/N]: " response 80 | if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then 81 | echo -ne "[$stat_y] Deleting all files in $btsdir\r" 82 | err_cmd=$(rm -r $btsdir 2>&1 >/dev/null) 83 | if [ $? -ne 0 ]; then 84 | echo "[$stat_x] Error: Could not remove $btsdir/btsync_arm.tar.gz" 85 | echo "[$stat_x] $err_cmd" 86 | sleep 0.5 87 | exit 1 88 | fi 89 | echo "[$stat_ok] Deleteted all files and sub-folders from $btsdir" 90 | echo "[$stat_y] Please restart the script" 91 | exit 0 92 | else 93 | echo "[$stat_x] Error: Did not install into $btsdir" 94 | exit 1 95 | fi 96 | fi 97 | } 98 | 99 | function install { 100 | sleep 0.5 101 | err_cmd=$(cd $btsdir 2>&1 >/dev/null) 102 | if [ $? -ne 0 ]; then 103 | echo "[$stat_x] Error: Could not change to $btsdir since it does not exist" 104 | echo "[$stat_x] $err_cmd" 105 | exit 1 106 | fi 107 | if [ -z $dllink ]; then 108 | echo "[$stat_y] Downloading the latest stable version from BitTorrent Inc" 109 | #Check for HTTP Status code - if other than 200 quit script 110 | smart_file_download $default_dllink "BiTorrent Sync binary" 111 | else 112 | echo "[$stat_y] Downloading the binary from the link provided" 113 | #Check for HTTP Status code - if other than 200 quit script 114 | smart_file_download $dllink "BiTorrent Sync binary" 115 | fi 116 | # So right now all BitTorrent Sync binary files are expected to be a .tar.gz file. If they are not the following command and thus the script will fail and exit. 117 | err_cmd=$(sudo tar -zxvf $btsdir/*.tar.gz -C /usr/bin/ btsync 2>&1 >/dev/null) 118 | if [ $? -ne 0 ]; then 119 | echo "[$stat_x] Error: Could not extract binary archive" 120 | echo "[$stat_x] $err_cmd" 121 | exit 1 122 | fi 123 | echo "[$stat_ok] Extraced the binary to /usr/bin/btsync" 124 | err_cmd=$(rm $btsdir/*.tar.gz 2>&1 >/dev/null) 125 | if [ $? -ne 0 ]; then 126 | echo "[$stat_x] Error: Could not remove binary archive" 127 | echo "[$stat_x] $err_cmd" 128 | exit 1 129 | fi 130 | } 131 | 132 | function btsync_initscript { 133 | if [ -f /etc/init.d/btsync ]; then 134 | echo "[$stat_ok] /etc/init.d/btsync script already exists" 135 | else 136 | smart_file_download "https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/btsync" "init.d script" 137 | sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/btsync 138 | sudo mv $btsdir/btsync /etc/init.d/btsync 139 | chmod 755 /etc/init.d/btsync && sudo chown root:root /etc/init.d/btsync 140 | echo "[$stat_ok] Created /etc/init.d/btsync script" 141 | fi 142 | } 143 | 144 | function btsyncconfig { 145 | if [ -f /etc/btsync/config.json ]; then 146 | echo "[$stat_ok] Configuration file /etc/btsync/config.json already exists" 147 | else 148 | smart_file_download "https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/config.json" "configuration file" 149 | sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/config.json 150 | if [ ! -d /etc/btsync/ ]; then 151 | sudo mkdir -p /etc/btsync/ 152 | fi 153 | sudo mv $btsdir/config.json /etc/btsync/config.json 154 | echo "[$stat_ok] Created BitTorrent Sync configuration file /etc/btsync/config.json" 155 | fi 156 | } 157 | 158 | function backup { 159 | backup_date=$(date +"%d-%m-%Y_%H-%M-%S") 160 | backup_reason="BitTorrent Sync is being backed up" 161 | # Look for the 4 common signals that indicate this script was killed. 162 | # If the background command was started, kill it, too. 163 | if [ -e $btsdir ]; then 164 | trap '[ -z $! ] || kill $!' SIGHUP SIGINT SIGQUIT SIGTERM 165 | tar -czPf /home/$user/btsync_backup_$backup_date.tar.gz /etc/init.d/btsync /etc/btsync/config.json /usr/bin/btsync $btsdir --exclude="*.journal" --exclude="*.journal.zip" --exclude="*.log" --exclude="sync.log.*.zip" & # Backup the files in the background. 166 | else 167 | # That actually doesn't work yet... still have to figure that one out 168 | echo "[$stat_x] Error: Backup failed" 169 | echo "[$stat_x] Error: $btsdir does not exist" 170 | sleep 0.7 171 | exit 1 172 | fi 173 | # The /proc directory exists while the command runs. 174 | while [ -e /proc/$! ]; do 175 | echo -ne "[ooo] $backup_reason\r" && sleep 0.2 176 | echo -ne "[Ooo] $backup_reason\r" && sleep 0.2 177 | echo -ne "[oOo] $backup_reason\r" && sleep 0.2 178 | echo -ne "[ooO] $backup_reason\r" && sleep 0.2 179 | echo -ne "[ooo] $backup_reason\r" && sleep 0.2 180 | echo -ne "[ooO] $backup_reason\r" && sleep 0.2 181 | echo -ne "[oOo] $backup_reason\r" && sleep 0.2 182 | echo -ne "[Ooo] $backup_reason\r" && sleep 0.2 183 | echo -ne "[ooo] $backup_reason\r" 184 | done 185 | echo -e "\e[0K\r[$stat_ok] BitTorrent Sync has been successfully backed up at /home/$user/btsync_backup_$backup_date.tar.gz" 186 | } 187 | 188 | function remove { 189 | echo "[$stat_x] Are you sure you want to remove your BitTorrent Sync installation?" 190 | echo "[$stat_x] That includes the following files and folders:" 191 | echo "[$stat_x] /home/$user/.btsync/ and all it's files and subfolders" 192 | echo "[$stat_x] /etc/init.d/btsync" 193 | echo "[$stat_x] /etc/btsync/config.json" 194 | echo "[$stat_x] /usr/bin/btsync" 195 | echo "[$stat_x] Files and folders that are being synced are not affected by this" 196 | read -r -p "[$stat_x] Remove BiTtorrent Sync? (yes/no(default)): " response 197 | if [[ $response =~ ^([yY][eE][sS])$ ]]; then 198 | sudo service btsync stop 199 | sleep 3 200 | if [ -d /home/$user/.btsync ]; then 201 | sudo rm -r /home/$user/.btsync 202 | fi 203 | if [ -f /etc/init.d/btsync ]; then 204 | sudo rm /etc/init.d/btsync 205 | fi 206 | if [ -d /etc/btsync/ ]; then 207 | sudo rm -r /etc/btsync/ 208 | fi 209 | if [ -f /usr/bin/btsync ]; then 210 | sudo rm /usr/bin/btsync 211 | fi 212 | echo "[$stat_ok] Successfully removed BitTorrent Sync" 213 | exit 0 214 | else 215 | echo "[$stat_ok] Aborted removing of BitTorrent Sync" 216 | exit 1 217 | fi 218 | } 219 | 220 | function version_check { 221 | if [ -f /usr/bin/btsync ]; then 222 | echo $(/usr/bin/btsync -help | grep "BitTorrent Sync" | awk {'print $3" "$4'}) 223 | fi 224 | } 225 | 226 | function smart_file_download { 227 | # This function checks for the HTTP Status Code prior of trying to donwnload any file. If the code is not 200 it will quit and let the user know what code came back 228 | status_code=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}` 229 | if [ $status_code -ne 200 ];then 230 | echo -en "\e[1A"; echo -e "\e[0K\r[$stat_x] Error: There was an error downloading the $2" 231 | echo "[$stat_x] Error: HTTP Status Code $status_code" 232 | exit 1 233 | else 234 | cd $btsdir 235 | curl -# -O $1 236 | echo -en "\e[1A"; echo -e "\e[0K\r[$stat_ok] Successfully downloaded the $2" 237 | fi 238 | } 239 | 240 | read -r -p "[$stat_y] Choose one of the BitTorrent Sync Script options [(i)nstall(default)/(u)pdate/(b)ackup/(r)emove]: " response 241 | if [[ $response =~ ^([u|U]|[u|U]pdate)$ ]]; then 242 | stop_sync 243 | echo "[$stat_ok] Updating BitTorrent Sync from version $(version_check) to the latest available version" 244 | install $dllink 245 | btsync_initscript 246 | btsyncconfig 247 | echo "[$stat_ok] Updated BitTorrent Sync to version $(version_check)" 248 | echo "[$stat_ok] You can now start BitTorrent Sync by typing \"sudo service btsync start\"" 249 | exit 0 250 | elif [[ $response =~ ^([b|B]|[b|B]ackup)$ ]]; then 251 | stop_sync 252 | backup 253 | exit 0 254 | elif [[ $response =~ ^(""|[i|I]|[i|I]nstall)$ ]]; then 255 | stop_sync 256 | install_preperations 257 | install 258 | btsync_initscript 259 | btsyncconfig 260 | echo "[$stat_ok] Installed BitTorrent Sync version $(version_check)" 261 | echo "[$stat_ok] You can now start BitTorrent Sync by typing \"sudo service btsync start\"" 262 | exit 0 263 | elif [[ $response =~ ^(""|[r|R]|[r|R]emove)$ ]]; then 264 | remove 265 | exit 0 266 | else 267 | echo "[$stat_x] Error: You did not choose one of the provided script options. Please try again." 268 | exit 0 269 | fi 270 | -------------------------------------------------------------------------------- /configuration-files/btsync: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: btsync 4 | # Required-Start: $local_fs $remote_fs 5 | # Required-Stop: $local_fs $remote_fs 6 | # Should-Start: $network 7 | # Should-Stop: $network 8 | # Default-Start: 2 3 4 5 9 | # Default-Stop: 0 1 6 10 | # Short-Description: daemonized version of BiTorrent Sync. 11 | # Description: Starts the BitTorrent Sync daemon for pi user. 12 | ### END INIT INFO 13 | 14 | # Replace with linux user you want to run BitTorrent Sync client for 15 | BTSYNC_USER="pi" 16 | DAEMON=/usr/bin/btsync 17 | 18 | init_folders(){ 19 | if [ ! -d /home/pi/.btsync/data ]; then 20 | mkdir -p /home/pi/.btsync/data 21 | chown pi /home/pi/.btsync/data 22 | fi 23 | if [ ! -d /home/pi/.btsync/data/.syncsystem ]; then 24 | mkdir -p /home/pi/.btsync/data/.syncsystem 25 | chown pi /home/pi/.btsync/data/.syncsystem 26 | fi 27 | } 28 | 29 | start() { 30 | config=/etc/btsync/config.json 31 | if [ -f $config ]; then 32 | init_folders 33 | echo "Starting BitTorrent Sync for $BTSYNC_USER" 34 | start-stop-daemon --start --quiet -b -o -c $BTSYNC_USER -u $BTSYNC_USER --exec $DAEMON -- --config $config 35 | else 36 | echo "Couldn't start BitTorrent Sync (no $config found)" 37 | fi 38 | } 39 | 40 | stop() { 41 | dbpid=`pgrep -fu $BTSYNC_USER $DAEMON` 42 | if [ ! -z "$dbpid" ]; then 43 | echo "Stopping btsync for $BTSYNC_USER" 44 | start-stop-daemon --stop -o -c $BTSYNC_USER -K -u $BTSYNC_USER -x $DAEMON 45 | fi 46 | } 47 | 48 | status() { 49 | dbpid=`pgrep -fu $BTSYNC_USER $DAEMON` 50 | if [ -z "$dbpid" ]; then 51 | echo "BitTorrent Sync for USER $BTSYNC_USER: not running." 52 | else 53 | echo "BitTorrent Sync for USER $BTSYNC_USER: running (pid $dbpid)" 54 | fi 55 | } 56 | 57 | case "$1" in 58 | start) 59 | start 60 | ;; 61 | stop) 62 | stop 63 | ;; 64 | restart|reload|force-reload) 65 | stop 66 | start 67 | ;; 68 | status) 69 | status 70 | ;; 71 | *) 72 | echo "Usage: /etc/init.d/btsync {start|stop|reload|force-reload|restart|status}" 73 | exit 1 74 | esac 75 | 76 | exit 0 -------------------------------------------------------------------------------- /configuration-files/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "vendor" : "Raspberry Pi", 3 | "device_name": "Raspberry Pi", 4 | "display_new_version": true, 5 | "lan_encrypt_data" : false, 6 | "storage_path" : "/home/pi/.btsync/data/.syncsystem", 7 | "pid_file" : "/home/pi/.btsync/data/.syncsystem/btsync.pid", 8 | "webui" : 9 | { 10 | "listen" : "0.0.0.0:8888", 11 | "directory_root" : "/", 12 | "directory_root_policy" : "all" 13 | } 14 | } 15 | --------------------------------------------------------------------------------