Bittorrent Sync install

Status
Not open for further replies.

taupinfada

Cadet
Joined
May 12, 2013
Messages
3
I create a script to install BtSync in the Plugging Jail.

So If you want use your Freenas as a dropbox you can use this one.

Of course, I am open to all suggestions ...

Code:
#!/bin/sh
cd /tmp

echo "Souhaitez-vous installer Bittorrent Sync (o/n)? [n] : "
read install_btsync
if [ $install_btsync = "o" ]; then
	echo "Initialisation de l'utilisateur btsync"
	if [ `more /etc/passwd | grep btsync | wc -l` -gt 0 ]; then
		echo "L'utilisateur btsync existe déjà"
	else
		echo "Création de l'utilisateur btsync"
		pw useradd btsync -m -c "Utilisateur Bittorrent Sync" -s /bin/sh -w yes
	fi

	echo "Initialisation des répertoire"
	home_dir="/home/btsync"
	config_dir="$home_dir/.config"
	config_btsync_dir="$config_dir/btsync"
	if [ ! -e $config_dir ]; then
		mkdir $config_dir
	fi
	if [ ! -e $config_btsync_dir ]; then
		mkdir $config_btsync_dir
	fi
	chown -R btsync $home_dir
	chmod -R 744 $home_dir

	if [ ! -e btsync_freebsd_x64-1.0.128.tar.gz ]; then
		echo "Téléchargement de Bittorrent Sync."
		wget "http://syncapp.bittorrent.com/1.0.128/btsync_freebsd_x64-1.0.128.tar.gz"
	fi
	if [ -e btsync ]; then
		rm btsync
	fi
	echo "Extraction de Bittorrent Sync."
	tar xvfz btsync_freebsd_x64-1.0.128.tar.gz
	echo "Copie de Bittorrent Sync."
	cp btsync /usr/local/bin/btsync
	chmod 755 /usr/local/bin/btsync

	echo "Initialisation du daemon"
	cat > "/etc/rc.d/btsync" <<EOF
#!/bin/sh
#
# PROVIDE: btsync
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv

# btsync service
# Take from <https://gist.github.com/MendelGusmao/5398362>
# Replace with UNIX users you want to run BTSync clients for
BTSYNC_USERS="btsync"
DAEMON=/usr/bin/btsync

name="btsync"
rcvar="btsync_enable"
command="/usr/bin/\${name}"

start() {
	echo "Starting btsync..."
	for btsuser in \$BTSYNC_USERS; do
		echo " - for \$btsuser"
		HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
		if [ -d \$HOMEDIR/.config ]; then
			su -l \$btsuser -c "/usr/local/bin/btsync --config \$HOMEDIR/.config/btsync/config.json"
		fi
	done
}

stop() {
	echo "Stopping btsync..."
	for btsuser in \$BTSYNC_USERS; do
		HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
		if [ -d \$HOMEDIR/.config ]; then
			dbpid=\`pgrep -u $btsuser btsync\`
			if [ -z "\$dbpid" ]; then
				echo "btsync for USER \$btsuser: not running."
			else
				echo "Stopping btsync for USER \$btsuser: running (pid \$dbpid)"
				kill \$dbpid
				until [ -z "\$dbpid" ]
				do
					dbpid=\`pgrep -u \$btsuser btsync\`
					echo -n "."
					sleep 1
				done
				echo ""
			fi
		fi
	done
}

status() {
	for btsuser in \$BTSYNC_USERS; do
		dbpid=\`pgrep -u $btsuser btsync\`
		if [ -z "\$dbpid" ]; then
			echo "btsync for USER \$btsuser: not running."
		else
			echo "btsync for USER \$btsuser: running (pid \$dbpid)"
		fi
	done
}

case "\$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: /etc/rc.d/btsync {start|stop|reload|force-reload|restart|status}"
exit 1
esac

exit 0
EOF
	chmod 755 /etc/rc.d/btsync
	
	if [ `more /etc/rc.conf | grep btsync | wc -l` -lt 1 ]; then
		echo "btsync_enable=\"YES\"" >> /etc/rc.conf
	fi
	
	echo "Configuration de l'interface web"
	echo "Utilisateur de connexion à l'interface web : "
	read user
	echo "Mot de passe de connexion à l'interface web : "
	read pwd
	cat > "$config_btsync_dir/config.json" <<EOF
{ 
  "device_name": "Freenas",
  "listening_port" : 0,                       // 0 - randomize port
  
/* storage_path dir contains auxilliary app files
   if no storage_path field: .sync dir created in the directory 
   where binary is located.
   otherwise user-defined directory will be used 
*/
  "storage_path" : "/home/btsync/.config/btsync",

// uncomment next line if you want to set location of pid file
// "pid_file" : "/var/run/syncapp/syncapp.pid",


  "check_for_updates" : true, 
  "use_upnp" : true,                              // use UPnP for port mapping


/* limits in kB/s
   0 - no limit
*/
  "download_limit" : 0,                       
  "upload_limit" : 0, 

/* remove "listen" field to disable WebUI
   remove "login" and "password" fields to disable credentials check
*/
  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "$user",
    "password" : "$pwd"
  }

}
EOF
	chown btsync $config_btsync_dir/config.json
	chmod 744 $config_btsync_dir/config.json

	echo "Lancement de BtSync :"
	/etc/rc.d/btsync start
fi
 

Joshua Parker Ruehlig

Hall of Famer
Joined
Dec 5, 2011
Messages
5,949
I create a script to install BtSync in the Plugging Jail.

So If you want use your Freenas as a dropbox you can use this one.

Of course, I am open to all suggestions ...

Code:
#!/bin/sh
cd /tmp

echo "Souhaitez-vous installer Bittorrent Sync (o/n)? [n] : "
read install_btsync
if [ $install_btsync = "o" ]; then
	echo "Initialisation de l'utilisateur btsync"
	if [ `more /etc/passwd | grep btsync | wc -l` -gt 0 ]; then
		echo "L'utilisateur btsync existe déjà"
	else
		echo "Création de l'utilisateur btsync"
		pw useradd btsync -m -c "Utilisateur Bittorrent Sync" -s /bin/sh -w yes
	fi

	echo "Initialisation des répertoire"
	home_dir="/home/btsync"
	config_dir="$home_dir/.config"
	config_btsync_dir="$config_dir/btsync"
	if [ ! -e $config_dir ]; then
		mkdir $config_dir
	fi
	if [ ! -e $config_btsync_dir ]; then
		mkdir $config_btsync_dir
	fi
	chown -R btsync $home_dir
	chmod -R 744 $home_dir

	if [ ! -e btsync_freebsd_x64-1.0.128.tar.gz ]; then
		echo "Téléchargement de Bittorrent Sync."
		wget "http://syncapp.bittorrent.com/1.0.128/btsync_freebsd_x64-1.0.128.tar.gz"
	fi
	if [ -e btsync ]; then
		rm btsync
	fi
	echo "Extraction de Bittorrent Sync."
	tar xvfz btsync_freebsd_x64-1.0.128.tar.gz
	echo "Copie de Bittorrent Sync."
	cp btsync /usr/bin/btsync
	chmod 755 /usr/bin/btsync

	echo "Initialisation du daemon"
	cat > "/etc/rc.d/btsync" <<EOF
#!/bin/sh
# btsync service
# Take from <https://gist.github.com/MendelGusmao/5398362>
# Replace with linux users you want to run BTSync clients for
BTSYNC_USERS="btsync"
DAEMON=/usr/bin/btsync

name="btsync"
rcvar="btsync_enable"
command="/usr/bin/\${name}"

start() {
	echo "Starting btsync..."
	for btsuser in \$BTSYNC_USERS; do
		echo " - for \$btsuser"
		HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
		if [ -d \$HOMEDIR/.config ]; then
			su -l \$btsuser -c "/usr/bin/btsync --config \$HOMEDIR/.config/btsync/config.json"
		fi
	done
}

stop() {
	echo "Stopping btsync..."
	for btsuser in \$BTSYNC_USERS; do
		HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
		if [ -d \$HOMEDIR/.config ]; then
			dbpid=\`pgrep -u $btsuser btsync\`
			if [ -z "\$dbpid" ]; then
				echo "btsync for USER \$btsuser: not running."
			else
				echo "Stopping btsync for USER \$btsuser: running (pid \$dbpid)"
				kill \$dbpid
				until [ -z "\$dbpid" ]
				do
					dbpid=\`pgrep -u \$btsuser btsync\`
					echo -n "."
					sleep 1
				done
				echo ""
			fi
		fi
	done
}

status() {
	for btsuser in \$BTSYNC_USERS; do
		dbpid=\`pgrep -u $btsuser btsync\`
		if [ -z "\$dbpid" ]; then
			echo "btsync for USER \$btsuser: not running."
		else
			echo "btsync for USER \$btsuser: running (pid \$dbpid)"
		fi
	done
}

case "\$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: /etc/rc.d/btsync {start|stop|reload|force-reload|restart|status}"
exit 1
esac

exit 0
EOF
	chmod 755 /etc/rc.d/btsync
	
	if [ `more /etc/rc.conf | grep btsync | wc -l` -lt 1 ]; then
		echo "btsync_enable=\"YES\"" >> /etc/rc.conf
	fi
	
	echo "Configuration de l'interface web"
	echo "Utilisateur de connexion à l'interface web : "
	read user
	echo "Mot de passe de connexion à l'interface web : "
	read pwd
	cat > "$config_btsync_dir/config.json" <<EOF
{ 
  "device_name": "Freenas",
  "listening_port" : 0,                       // 0 - randomize port
  
/* storage_path dir contains auxilliary app files
   if no storage_path field: .sync dir created in the directory 
   where binary is located.
   otherwise user-defined directory will be used 
*/
  "storage_path" : "/home/btsync/.config/btsync",

// uncomment next line if you want to set location of pid file
// "pid_file" : "/var/run/syncapp/syncapp.pid",


  "check_for_updates" : true, 
  "use_upnp" : true,                              // use UPnP for port mapping


/* limits in kB/s
   0 - no limit
*/
  "download_limit" : 0,                       
  "upload_limit" : 0, 

/* remove "listen" field to disable WebUI
   remove "login" and "password" fields to disable credentials check
*/
  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "$user",
    "password" : "$pwd"
  }

}
EOF
	chown btsync $config_btsync_dir/config.json
	chmod 744 $config_btsync_dir/config.json

	echo "Lancement de BtSync :"
	/etc/rc.d/btsync start
fi


In FreeBSD user binaries go in /usr/local/bin.
Also there is usually something like this in the begining of the init, otherwise some daemons wont start correctly as they can't bind to anything.
Code:
#!/bin/sh
#
# PROVIDE: btsync
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv


this program looks very interesting.
 

madmax

Explorer
Joined
Aug 31, 2012
Messages
64
Any luck or help on getting the Daemon to start up on boot up?!


I been using the git hub by Mendel Gusmao for direction which has been gracious enough to share this code with us. The example he uses for the config.json, I was successful with and been able to execute within the jail to start the Daemon.
Code:
/etc/rc.d/btsync start


I put a /etc/rc.conf entry of
Code:
btsync_enable="YES"
but yet after the stopping and starting of the jail the status and webgui are both down. It doesn't automatic boot up upon boot up of the jail.


I didn't understand this at first so there's two scripts, ones for install and the other is daemon but when run as one script it will install the Daemon for you. I was getting a syntax error on the lines before the last two line of install script so I had to put in "fi" and that worked.
I was getting an "syntax error: end of file unexpected (expecting "fi")
Code:
echo "Initialization of the daemon...ROAR"
cat > "/etc/rc.d/btsync" <<EOF

Corrected to

Code:
fi
echo "Initialization of the daemon...ROAR"
cat > "/etc/rc.d/btsync" <<EOF


I running in a jail in system 9.1 RC1 and of course jexec into the jail. I also had to give it permissions and run it in the /usr/bin area.

Here's the script I'm trying to use. I translated the French, updated the tar link to the x64 link, and renamed the tar file when downloaded.

Install script w/ DAEMON

Code:
    #!/bin/sh
 
    cd /tmp
 
    echo "Would you like to install Bittorrent Sync (y/n)? [n] : "
    read install_btsync
    if [ $install_btsync = "y" ]; then
        echo "Initialization of btsync user"
        if [ `more /etc/passwd | grep btsync | wc -l` -gt 0 ]; then
            echo "The btsync user already exists"
        else
            echo "Creation of btsync user"
            pw useradd btsync -m -c "User Bittorrent Sync" -s /bin/sh -w yes
        fi
 
        echo "Initialization of directory"
        home_dir="/home/btsync"
        config_dir="$home_dir/.config"
        config_btsync_dir="$config_dir/btsync"
        if [ ! -e $config_dir ]; then
            mkdir $config_dir
        fi
        if [ ! -e $config_btsync_dir ]; then
            mkdir $config_btsync_dir
        fi
        chown -R btsync $home_dir
        chmod -R 744 $home_dir
 
        if [ ! -e btsync_freebsd_x64.tar.gz ]; then
            echo "Downloading Bittorrent Sync."
            wget "http://btsync.s3-website-us-east-1.amazonaws.com/btsync_freebsd_x64.tar.gz"
        fi
        if [ -e btsync ]; then
            rm btsync
        fi
        echo "Extraction of Bittorrent Sync."
        tar xvfz btsync_freebsd_x64.tar.gz
        echo "Copying Bittorrent Sync."
        cp btsync /usr/bin/btsync
        chmod 755 /usr/bin/btsync
 
      fi
 
        echo "Initialization of the daemon...ROAR"
        cat > "/etc/rc.d/btsync" <<EOF
 
 
 
 
#!/bin/sh
 
# PROVIDE: btsync
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv
 
# btsync service
# Take from <https://gist.github.com/MendelGusmao/5398362>
# Replace with linux users you want to run BTSync clients for
 
    BTSYNC_USERS="btsync"
    DAEMON=/usr/bin/btsync
 
    name="btsync"
    rcvar="btsync_enable"
    command="/usr/bin/\${name}"
 
    start() {
        echo "Starting btsync..."
        for btsuser in \$BTSYNC_USERS; do
            echo " - for \$btsuser"
            HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
            if [ -d \$HOMEDIR/.config ]; then
                su -l \$btsuser -c "/usr/bin/btsync --config \$HOMEDIR/.config/btsync/config.json"
            fi
        done
    }
 
    stop() {
        echo "Stopping btsync..."
        for btsuser in \$BTSYNC_USERS; do
            HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\`
            if [ -d \$HOMEDIR/.config ]; then
                dbpid=\`pgrep -u $btsuser btsync\`
                if [ -z "\$dbpid" ]; then
                    echo "btsync for USER \$btsuser: not running."
                else
                    echo "Stopping btsync for USER \$btsuser: running (pid \$dbpid)"
                    kill \$dbpid
                    until [ -z "\$dbpid" ]
                    do
                        dbpid=\`pgrep -u \$btsuser btsync\`
                        echo -n "."
                        sleep 1
                    done
                    echo ""
                fi
            fi
        done
    }
 
    status() {
        for btsuser in \$BTSYNC_USERS; do
            dbpid=\`pgrep -u $btsuser btsync\`
            if [ -z "\$dbpid" ]; then
                echo "btsync for USER \$btsuser: not running."
            else
                echo "btsync for USER \$btsuser: running (pid \$dbpid)"
            fi
        done
    }
 
    case "\$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|reload|force-reload)
    stop
    start
    ;;
    status)
    status
    ;;
    *)
    echo "Usage: /etc/rc.d/btsync {start|stop|reload|force-reload|restart|status}"
    exit 1
    esac
 
    exit 0
    EOF
        chmod 755 /etc/rc.d/btsync
 
        if [ `more /etc/rc.conf | grep btsync | wc -l` -lt 1 ]; then
            echo "btsync_enable=\"YES\"" >> /etc/rc.conf
        fi
 
        echo "Web Interface configuration"
        echo "User login to the web interface : "
        read user
        echo "Password to connect to the web interface : "
        read pwd
        cat > "$config_btsync_dir/config.json" <<EOF
    {
      "device_name": "Freenas",
      "listening_port" : 0,                      // 0 - randomize port
 
    /* storage_path dir contains auxilliary app files
      if no storage_path field: .sync dir created in the directory
      where binary is located.
      otherwise user-defined directory will be used
    */
      "storage_path" : "/home/btsync/.config/btsync",
 
    // uncomment next line if you want to set location of pid file
    // "pid_file" : "/var/run/syncapp/syncapp.pid",
 
 
      "check_for_updates" : true,
      "use_upnp" : true,                              // use UPnP for port mapping
 
 
    /* limits in kB/s
      0 - no limit
    */
      "download_limit" : 0,
      "upload_limit" : 0,
 
    /* remove "listen" field to disable WebUI
      remove "login" and "password" fields to disable credentials check
    */
      "webui" :
      {
        "listen" : "0.0.0.0:8888",
        "login" : "$user",
        "password" : "$pwd"
      }
 
    }
    EOF
        chown btsync $config_btsync_dir/config.json
        chmod 744 $config_btsync_dir/config.json
 
        echo "Launching btsync :"
        /etc/rc.d/btsync start
    fi
 
 
 
 

Brian Buchanan

Dabbler
Joined
Aug 14, 2013
Messages
17
1. On a fresh jail, there is no wget. use Fetch.
2. the fi is "fix" wrong. The matching fi is at the end of the file. Your problem is that your EOF line is not left justified. For any "... << EOF" to work, the string "EOF" must be at the beginning of the line. (and EOF can be any string)

Edit: Here's my version: http://pastebin.com/SNBRQW31 I added a link back to this thread but didn't put any other attribution.
I put the binary in /usr/local/sbin, added -n to echo lines before read, fixed indentation and corrected a bug in the stop section (missing a \ before a $btsuser).
 

kaisch

Dabbler
Joined
May 20, 2013
Messages
12
@Brian with your Code i always get:

btsync.sh: 184 lines, 5660 characters.
root@BTSYNC:/usr/local/sbin # sh btsync.sh
Would you like to install Bittorrent Sync (y/n)? [n] : y
btsync.sh: 185: Syntax error: end of file unexpected (expecting "fi")
root@BTSYNC:/usr/local/sbin #

any suggestions? iam not a coder? thx
 

Brian Buchanan

Dabbler
Joined
Aug 14, 2013
Messages
17
My guess is that there is a space before one of the EOF lines. EOF must be the first thing on the line.
 

kaisch

Dabbler
Joined
May 20, 2013
Messages
12
no it works... sry i did not understand your message above before...

two more questions...
- Is there any trick to autostart btsync in jail? like a script/cron?
- when i want to mount some storage from main-freenas-system: is there any toturial like mount location, credentials to make it work properly?
i tried /usr/home/btsync/syncfolder but after that btsync did not start...
- is there any command to start and stop btsync while testing?

thx
 

Brian Buchanan

Dabbler
Joined
Aug 14, 2013
Messages
17
The pastebin script creates two files after downloading and installing btsync. If you got a "missing fi" error then one or both of those files didn't get created.

See if you have a /etc/rc.d/btsync and /home/btsync/.config/btsync/config.json files

To start you run "/etc/rc.d/btsync start" which obviously won't work if it doesn't exist.

Edit the pastebin file and make sure that the two lines that start with EOF don't have any space at the beginning of the line and run it again.
 

Brian Buchanan

Dabbler
Joined
Aug 14, 2013
Messages
17
Mounting storage makes things a bit more difficult. You have to use the FeeNAS web page to "add storage" to the jail. There are was a problems that I didn't have a good solutions
for.

1. I wanted to share the btsync folders via FreeNAS but the files are owned by a jail user and the FreeNAS system wants the folders a files with different owners.

Additionally I tried btsync with my main folder and it never ever finished syncing. I had about 800-GB and just over 100,000 files and about 200 would never sync. I ended up abandoning btsync because of this :(. I submitted logs so maybe they'll get fixed but it wasn't ready to be the solution I needed.
 
Status
Not open for further replies.
Top