Skip to content

Commit 108dae8

Browse files
committed
v1.2.0
1 parent 0b4b5b4 commit 108dae8

File tree

2 files changed

+100
-98
lines changed

2 files changed

+100
-98
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
![BitTorrent Sync loves Raspberry Pi](http://d3f61ff4egbtyy.cloudfront.net/monthly_2016_01/bts_loves_pi_forum.png.b4ba9fc38c01ea35e4b79a3ee7879ed5.png)
22
Raspberry Pi + Sync Script
33
=======
44

@@ -18,9 +18,9 @@ This script will do the following after you run it:
1818
* Extract the downloaded binary to a predefined location ```/usr/bin/btsync```
1919
* Create an ```/etc/init.d/btsync``` script so you can easily start/stop it
2020
* Create a configuration file for it to run from ```/etc/btsync/config.json```
21-
* Create a data directory for it's databases etc. in ```/home/pi/.btsync/```
21+
* Create a data directory for it's databases etc. in ```/home/<user>/.btsync/```
2222

23-
Backups will be created in ```~/pi/``` directory using this format ```btsync_backup_dd-mm-yyyy_hh-mm-ss.tar.gz```
23+
Backups will be created in ```~/<user>/``` directory using this format ```btsync_backup_dd-mm-yyyy_hh-mm-ss.tar.gz```
2424

2525
### Instructions
2626

@@ -34,7 +34,7 @@ curl -O https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/btsyn
3434
chmod +x btsync.sh
3535
```
3636
* Run the script
37-
* Verify that the user variable on line 5 is correct. The script will use the default user ```pi```. If that is the case for you, you don't need to change anything. Just keep following the instructions.
37+
* 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
3838
```
3939
./btsync.sh
4040
```
@@ -44,7 +44,7 @@ Please be mindful: by using other URLs/links I can not garantuee that the script
4444
Only use links provided by the Sync Staff on the forums or from the official BitTorrent Inc. website.
4545

4646
BitTorrent Sync will use a default configuration. Please make sure that it will work in your environment.
47-
[Here](http://help.getsync.com/customer/portal/articles/2018454-running-sync-in-configuration-mode) and [here](http://help.getsync.com/customer/en/portal/articles/1902098-sync-preferences-general-advanced-more-options) are help articles from the [Sync Help Center](http://help.getsync.com/) that will guide you through the configuration options.
47+
[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.
4848

4949
After the script is done, you want to have it automatically start after you reboot your Raspberry Pi.
5050
To do that enter the following:```sudo update-rc.d btsync defaults```

btsync.sh

Lines changed: 95 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
#!/bin/bash
22
#
3-
#Change the $user variable to the user you would like Sync to be installed to and later run as
4-
#Default is pi
5-
user="pi"
6-
# Don't alter variables below since they are relevant for running the script properly
7-
btsdir="/home/$user/.btsync"
83
red=`tput setaf 1`
94
green=`tput setaf 2`
105
yellow=`tput setaf 3`
116
reset=`tput sgr0`
127
stat_y=${yellow}\?${reset}
138
stat_x=${red}X${reset}
149
stat_ok=${green}OK${reset}
15-
dllink="$1"
1610
err_cmd=""
1711

1812
if [ "$(id -u)" == "0" ]; then
19-
echo "[$stat_x] This script cannot run as root"
13+
echo "[$stat_x] Error: This script cannot run as root"
2014
exit 1
2115
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"
31+
32+
if [ ! -z $1 ];then
33+
dllink="$1"
34+
else
35+
if [ $(uname -m) == "armv6l" ]; then
36+
default_dllink="https://download-cdn.getsync.com/stable/linux-arm/BitTorrent-Sync_arm.tar.gz"
37+
elif [ $(uname -m) == "armv7l" ]; then
38+
default_dllink="https://download-cdn.getsync.com/2.3.1/linux-armhf/BitTorrent-Sync_armhf.tar.gz"
39+
fi
40+
fi
2241

2342
function stop_sync {
2443
echo "[$stat_y] Trying to stop a running BitTorrent Sync instance"
@@ -45,42 +64,36 @@ function stop_sync {
4564

4665
function install_preperations {
4766
if [ ! -d "$btsdir" ]; then
48-
echo "[$stat_ok] Trying to create installation folder $btsdir"
67+
echo "[$stat_ok] Trying to create installation folder ($btsdir)"
4968
sleep 0.5
5069
err_cmd=$(mkdir $btsdir 2>&1 >/dev/null)
5170
if [ $? -ne 0 ]; then
52-
echo "[$stat_x] Could not create installation folder $btsdir"
71+
echo "[$stat_x] Error: Could not create installation folder $btsdir"
5372
echo "[$stat_x] $err_cmd"
5473
exit 1
5574
fi
5675
echo "[$stat_ok] BitTorrent Sync installation folder has been created ($btsdir)"
5776
sleep 0.5
5877
else
59-
echo "[$stat_x] BitTorrent Sync installation folder $btsdir already exists"
78+
echo "[$stat_y] BitTorrent Sync installation folder $btsdir already exists"
6079
sleep 0.5
61-
read -r -p "[$stat_x] Delete all files and sub-folders inside $btsdir? [Y/N]: " response
80+
read -r -p "[$stat_y] Delete all files and sub-folders inside $btsdir? [Y/N]: " response
6281
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
6382
echo -ne "[$stat_y] Deleting all files in $btsdir\r"
64-
sudo rm -rf ${btsdir}
83+
err_cmd=$(rm -r $btsdir 2>&1 >/dev/null)
84+
if [ $? -ne 0 ]; then
85+
echo "[$stat_x] Error: Could not remove $btsdir/btsync_arm.tar.gz"
86+
echo "[$stat_x] $err_cmd"
87+
sleep 0.5
88+
exit 1
89+
fi
6590
echo "[$stat_ok] Deleteted all files and sub-folders from $btsdir"
6691
echo "[$stat_y] Please restart the script"
6792
exit 0
6893
else
69-
echo "[$stat_x] Did not install into $btsdir"
94+
echo "[$stat_x] Error: Did not install into $btsdir"
7095
exit 1
7196
fi
72-
if [ -f $btsdir/btsync_arm.tar.gz ]; then
73-
echo "[$stat_y] A binary file already exists"
74-
sleep 0.5
75-
echo "[$stat_ok] Trying to remove and re-download the latest binary"
76-
err_cmd=$(rm $btsdir/btsync_arm.tar.gz 2>&1 >/dev/null)
77-
if [ $? -ne 0 ]; then
78-
echo "[$stat_x] Error: Could not remove $btsdir/btsync_arm.tar.gz"
79-
echo "[$stat_x] $err_cmd"
80-
sleep 0.5
81-
exit 1
82-
fi
83-
fi
8497
fi
8598
}
8699

@@ -94,38 +107,24 @@ function install {
94107
fi
95108
if [ -z $dllink ]; then
96109
echo "[$stat_y] Downloading the latest stable version from BitTorrent Inc"
97-
curl -# -o $btsdir/btsync_arm.tar.gz https://download-cdn.getsync.com/stable/linux-arm/BitTorrent-Sync_arm.tar.gz
98-
if [ $? -ne 0 ]; then
99-
echo "[$stat_x] Error: There was an error downloading the BiTorrent Sync binary"
100-
exit 1
101-
else
102-
echo -en "\e[1A"; echo -e "\e[0K\r[$stat_ok] Successfully downloaded the binary"
103-
fi
110+
#Check for HTTP Status code - if other than 200 quit script
111+
smart_file_download $default_dllink "BiTorrent Sync binary"
104112
else
105113
echo "[$stat_y] Downloading the binary from the link provided"
106-
curl -# -o $btsdir/btsync_arm.tar.gz $dllink
107-
if [ $? -ne 0 ]; then
108-
echo "[$stat_x] Error: There was an error downloading the file using the link you provided. Please check the URL and try again"
109-
exit 1
110-
else
111-
echo -en "\e[1A"; echo -e "\e[0K\r[$stat_ok] Successfully downloaded the binary"
112-
fi
114+
#Check for HTTP Status code - if other than 200 quit script
115+
smart_file_download $dllink "BiTorrent Sync binary"
113116
fi
117+
# 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.
118+
err_cmd=$(sudo tar -zxvf $btsdir/*.tar.gz -C /usr/bin/ btsync 2>&1 >/dev/null)
114119
if [ $? -ne 0 ]; then
115-
echo -ne "[$stat_x] Error: There was an error downloading the binary"
116-
echo -ne '\n'
117-
exit 1
118-
fi
119-
err_cmd=$(sudo tar -zxvf $btsdir/btsync_arm.tar.gz -C /usr/bin/ btsync 2>&1 >/dev/null)
120-
if [ $? -ne 0 ]; then
121-
echo "[$stat_x] Error: Could not extract btsync_arm.tar.gz"
120+
echo "[$stat_x] Error: Could not extract binary archive"
122121
echo "[$stat_x] $err_cmd"
123122
exit 1
124123
fi
125124
echo "[$stat_ok] Extraced the binary to /usr/bin/btsync"
126-
err_cmd=$(rm $btsdir/btsync_arm.tar.gz 2>&1 >/dev/null)
125+
err_cmd=$(rm $btsdir/*.tar.gz 2>&1 >/dev/null)
127126
if [ $? -ne 0 ]; then
128-
echo "[$stat_x] Error: Could not remove $btsdir/btsync_arm.tar.gz"
127+
echo "[$stat_x] Error: Could not remove binary archive"
129128
echo "[$stat_x] $err_cmd"
130129
exit 1
131130
fi
@@ -135,39 +134,25 @@ function btsync_initscript {
135134
if [ -f /etc/init.d/btsync ]; then
136135
echo "[$stat_ok] /etc/init.d/btsync script already exists"
137136
else
138-
err_cmd=$(curl -o $btsdir/btsync_init https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/btsync 2>&1 >/dev/null)
139-
if [ $? -ne 0 ]; then
140-
echo "[$stat_x] Error: There was an error downloading the init.d script"
141-
echo "[$stat_x] $err_cmd"
142-
sleep 0.5
143-
exit 1
144-
else
145-
sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/btsync_init
146-
sudo mv $btsdir/btsync_init /etc/init.d/btsync
147-
chmod 755 /etc/init.d/btsync && sudo chown root:root /etc/init.d/btsync
148-
echo "[$stat_ok] Created /etc/init.d/btsync script"
149-
fi
137+
smart_file_download "https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/btsync" "init.d script"
138+
sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/btsync
139+
sudo mv $btsdir/btsync /etc/init.d/btsync
140+
chmod 755 /etc/init.d/btsync && sudo chown root:root /etc/init.d/btsync
141+
echo "[$stat_ok] Created /etc/init.d/btsync script"
150142
fi
151143
}
152144

153145
function btsyncconfig {
154-
if [ ! -f /etc/btsync/config.json ]; then
155-
err_cmd=$(curl -o $btsdir/config.json https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/config.json 2>&1 >/dev/null)
156-
if [ $? -ne 0 ]; then
157-
echo "[$stat_x] Error: There was an error downloading the configuration file"
158-
echo "[$stat_x] $err_cmd"
159-
sleep 0.5
160-
exit 1
161-
else
162-
sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/config.json
163-
if [ ! -d /etc/btsync/ ]; then
164-
sudo mkdir -p /etc/btsync/
165-
fi
166-
sudo mv $btsdir/config.json /etc/btsync/config.json
167-
echo "[$stat_ok] Created BitTorrent Sync configuration file /etc/btsync/config.json"
168-
fi
169-
else
146+
if [ -f /etc/btsync/config.json ]; then
170147
echo "[$stat_ok] Configuration file /etc/btsync/config.json already exists"
148+
else
149+
smart_file_download "https://raw.githubusercontent.com/moritzdietz/pi-syncscript/master/configuration-files/config.json" "configuration file"
150+
sed -r -i.tmp "s/\bpi\b/$user/g" $btsdir/config.json
151+
if [ ! -d /etc/btsync/ ]; then
152+
sudo mkdir -p /etc/btsync/
153+
fi
154+
sudo mv $btsdir/config.json /etc/btsync/config.json
155+
echo "[$stat_ok] Created BitTorrent Sync configuration file /etc/btsync/config.json"
171156
fi
172157
}
173158

@@ -176,48 +161,51 @@ function backup {
176161
backup_reason="BitTorrent Sync is being backed up"
177162
# Look for the 4 common signals that indicate this script was killed.
178163
# If the background command was started, kill it, too.
179-
if [ -e ${btsdir} ]; then
164+
if [ -e $btsdir ]; then
180165
trap '[ -z $! ] || kill $!' SIGHUP SIGINT SIGQUIT SIGTERM
181-
tar -czPf /home/$user/btsync_backup_$backup_date.tar.gz /etc/init.d/btsync /etc/btsync/config.json /usr/bin/btsync ${btsdir} --exclude="*.log" --exclude="*.journal" --exclude="sync.log.*.zip" & # Backup the files in the background.
166+
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.
182167
else
183168
# That actually doesn't work yet... still have to figure that one out
184-
echo "[$stat_x] Error: Copying failed"
169+
echo "[$stat_x] Error: Backup failed"
170+
echo "[$stat_x] Error: $btsdir does not exist"
185171
sleep 0.7
186172
exit 1
187173
fi
188174
# The /proc directory exists while the command runs.
189175
while [ -e /proc/$! ]; do
190-
echo -ne "[ooo] ${backup_reason}\r" && sleep 0.2
191-
echo -ne "[Ooo] ${backup_reason}\r" && sleep 0.2
192-
echo -ne "[oOo] ${backup_reason}\r" && sleep 0.2
193-
echo -ne "[ooO] ${backup_reason}\r" && sleep 0.2
194-
echo -ne "[ooo] ${backup_reason}\r" && sleep 0.2
195-
echo -ne "[ooO] ${backup_reason}\r" && sleep 0.2
196-
echo -ne "[oOo] ${backup_reason}\r" && sleep 0.2
197-
echo -ne "[Ooo] ${backup_reason}\r" && sleep 0.2
198-
echo -ne "[ooo] ${backup_reason}\r"
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" && sleep 0.2
184+
echo -ne "[ooo] $backup_reason\r"
199185
done
200186
echo -e "\e[0K\r[$stat_ok] BitTorrent Sync has been successfully backed up at /home/$user/btsync_backup_$backup_date.tar.gz"
201187
}
202188

203189
function remove {
204190
echo "[$stat_x] Are you sure you want to remove your BitTorrent Sync installation?"
205191
echo "[$stat_x] That includes the following files and folders:"
206-
echo "[$stat_x] /home/${user}/.btsync/ and all it's files and subfolders"
192+
echo "[$stat_x] /home/$user/.btsync/ and all it's files and subfolders"
207193
echo "[$stat_x] /etc/init.d/btsync"
208194
echo "[$stat_x] /etc/btsync/config.json"
209195
echo "[$stat_x] /usr/bin/btsync"
210196
echo "[$stat_x] Files and folders that are being synced are not affected by this"
211197
read -r -p "[$stat_x] Remove BiTtorrent Sync? (yes/no(default)): " response
212198
if [[ $response =~ ^([yY][eE][sS])$ ]]; then
213-
if [ -d /home/${user}/.btsync ]; then
214-
sudo rm -rf /home/${user}/.btsync
199+
sudo service btsync stop
200+
sleep 3
201+
if [ -d /home/$user/.btsync ]; then
202+
sudo rm -r /home/$user/.btsync
215203
fi
216204
if [ -f /etc/init.d/btsync ]; then
217205
sudo rm /etc/init.d/btsync
218206
fi
219207
if [ -d /etc/btsync/ ]; then
220-
sudo rm -rf /etc/btsync/
208+
sudo rm -r /etc/btsync/
221209
fi
222210
if [ -f /usr/bin/btsync ]; then
223211
sudo rm /usr/bin/btsync
@@ -236,6 +224,20 @@ function version_check {
236224
fi
237225
}
238226

227+
function smart_file_download {
228+
# 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
229+
status_code=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}`
230+
if [ $status_code -ne 200 ];then
231+
echo -en "\e[1A"; echo -e "\e[0K\r[$stat_x] Error: There was an error downloading the $2"
232+
echo "[$stat_x] Error: HTTP Status Code $status_code"
233+
exit 1
234+
else
235+
cd $btsdir
236+
curl -# -O $1
237+
echo -en "\e[1A"; echo -e "\e[0K\r[$stat_ok] Successfully downloaded the $2"
238+
fi
239+
}
240+
239241
read -r -p "[$stat_y] Choose one of the BitTorrent Sync Script options [(i)nstall(default)/(u)pdate/(b)ackup/(r)emove]: " response
240242
if [[ $response =~ ^([u|U]|[u|U]pdate)$ ]]; then
241243
stop_sync
@@ -263,6 +265,6 @@ elif [[ $response =~ ^(""|[r|R]|[r|R]emove)$ ]]; then
263265
remove
264266
exit 0
265267
else
266-
echo "[$stat_x] You did not choose one of the provided script options. Please try again."
268+
echo "[$stat_x] Error: You did not choose one of the provided script options. Please try again."
267269
exit 0
268270
fi

0 commit comments

Comments
 (0)