Shell script I wrote to update btsync on linux/unix


Recommended Posts


#/bin/sh
killall -9 btsync
rm btsync
wget $1 -O download.tar.gz
tar -xzvf download.tar.gz
rm download.tar.gz
./btsync

Save the file as btupdater in the same directory as your btsync executable

Run


chmod +x btupdater

to allow execution

To use, pass of the location of the latest btsync tar.gz file as the first argument.

ex.


./btupdater http://syncapp.bittorrent.com/1.1.33/btsync_x64-1.1.33.tar.gz

I wonder, could btsync be used to update itself with a new binary?

Assuming all your btsync daemons are running the same architecture, could you sync the actual executable folder and replace the executable on one and it will replace on all the others?

I'm not a professional scripter or anything.

Link to comment
Share on other sites

Just a side not: Is there a reason why you use key code 9 instead of the default one? This is not a smooth shutdown and in my cases btsync wants to reindex the whole collection after another restart which can be quite time-consuming. I would just consider "killall btsync".

Link to comment
Share on other sites

Personally I would not hard code a possible maximum time that depends on your system and especially on your system load. Make an endless loop with a sleep inside and check whether btsync has been shutdown or not. For example you can check the amount of 'btsync' in the process table:

ps aux | grep btsync | wc -l

If thats =1 btsync has been shutdown.

Link to comment
Share on other sites

Hello,

Instead of the killall, it may be better to use some init-script. I did this one on my Debian computers/servers:


#!/bin/sh
# Largely adapted from xdm's init script:
# Copyright 1998-2002, 2004, 2005 Branden Robinson <branden@debian.org>.
# Copyright 2006 Eugene Konev <ejka@imfi.kspu.ru>
### BEGIN INIT INFO
# Provides: btsync
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop the SLiM daemon.
### END INIT INFO
DAEMON=/usr/local/bin/btsync
CONFIG=/etc/default/btsync
DAEMON_OPTS="--config ${CONFIG}"
STARTAS=<username>
case $1 in
start)
su -c "${DAEMON} ${DAEMON_OPTS}" $STARTAS
;;
stop)
kill $(cat /tmp/btsync.pid)
;;
*)
echo 'Usage: start|stop'
;;
esac
# End of file

Smarter, and this allows Sync to start on boot :).

The equivalent for Gentoo computer:


#!/sbin/runscript

depend() {
use net
}

start() {
ebegin "Starting btsync"
su -c "/usr/local/bin/btsync --config /etc/btsync.json" <username>
eend $?
}

stop() {
ebegin "Stopping btsync"
kill $(cat /tmp/btsync.pid)
eend $?
}

With the location, you may as well use this command line for tar:


tar zxp <archive> -C /usr/local/bin/

As for the URL, it may be better to fix it in the script, and just pass arch and version as arguments.

Modified script would look like:


#/bin/sh
if [ $# -ne 2 ]; then
echo 'Please provide architecture and version'
exit 1
fi
arch=$1
version=$2
url="http://syncapp.bittorrent.com/1.1.33/btsync_${arch}-${version}.tar.gz"
wget $url -O /tmp/download.tar.gz
tar -xzvf /tmp/download.tar.gz -C /usr/local/bin/ || (echo 'ERROR: unable to download' && exit 2)
service btsync stop
rm -f /tmp/download.tar.gz
service btsync start

Cheers,

C.

Link to comment
Share on other sites

By the way, it's maybe even better to download from the generic link:

http://btsync.s3-website-us-east-1.amazonaws.com/btsync_x64.tar.gz

This way, you just have to provide the architecture, and Voilà. Some md5sum check would be of some use in order to not stop btsync for nothing.

This would mean:

- uncompress in some temporary directory

- md5sum of the newly downloaded btsync binary, compare it with the current one

- IF md5sum aren't the same, do the update

- ELSE discard the update, and try it again the next day.

That said… the best way would be to have distro packages (.deb, .rpm, .ebuild and so on) in order to get a fully system integrated update.

Cheers,

C.

Link to comment
Share on other sites

You should update url like this:


url="http://syncapp.bittorrent.com/${version}/btsync_${arch}-${version}.tar.gz"

I also did a similar script, I added a small test to check the url:


test_url=`curl -Is $url | head -n 1 | sed -r 's/.* ([0-9]*) .*/\1/'`
if [ "$test_url" = "200" ]; then
wget $url -O - | tar -xvz
else
echo version ${version} not found.
fi

My script. I don't pass the arch because it never really changes, just edit the script.


#/bin/sh

arch="i386"

if [ -z $# ]; then
echo ./btupdater [version]
exit 1
fi

url="http://syncapp.bittorrent.com/$1/btsync_$arch-$1.tar.gz"
test_url=`curl --silent -Is $url | head -n 1 | sed -r 's/.* ([0-9]*) .*/\1/'`

if [ "$test_url" != "200" ]; then
echo version $1 not found.
exit 1
fi

echo -e "killing process\c"
while [ `ps aux | grep btsync | wc -l` -ne 1 ]; do
echo -e ".\c"
killall btsync
sleep 1
done
echo .

wget --quiet $url -O - | tar -xz

./btsync

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.