SHOW:
|
|
- or go back to the newest paste.
1 | #!/bin/sh | |
2 | # Installs Bittorrent Sync (btsync) under a FreeNAS jail | |
3 | # http://forums.freenas.org/threads/bittorrent-sync-install.12722/ | |
4 | # 2013-08-20 | |
5 | ||
6 | cd /tmp | |
7 | ||
8 | echo -n "Would you like to install Bittorrent Sync (y/n)? [n] : " | |
9 | read install_btsync | |
10 | ||
11 | if [ $install_btsync = "y" ]; then | |
12 | echo "Initialization of btsync user" | |
13 | if [ `more /etc/passwd | grep btsync | wc -l` -gt 0 ]; then | |
14 | echo "The btsync user already exists" | |
15 | else | |
16 | echo "Creation of btsync user" | |
17 | pw useradd btsync -m -c "User Bittorrent Sync" -s /bin/sh -w yes | |
18 | fi | |
19 | ||
20 | echo "Initialization of directory" | |
21 | home_dir="/home/btsync" | |
22 | config_dir="$home_dir/.config" | |
23 | config_btsync_dir="$config_dir/btsync" | |
24 | ||
25 | if [ ! -e $config_dir ]; then | |
26 | mkdir $config_dir | |
27 | fi | |
28 | if [ ! -e $config_btsync_dir ]; then | |
29 | mkdir $config_btsync_dir | |
30 | fi | |
31 | chown -R btsync $home_dir | |
32 | chmod -R 744 $home_dir | |
33 | ||
34 | if [ ! -e btsync_freebsd_x64.tar.gz ]; then | |
35 | echo "Downloading Bittorrent Sync." | |
36 | fetch "http://btsync.s3-website-us-east-1.amazonaws.com/btsync_freebsd_x64.tar.gz" | |
37 | fi | |
38 | if [ -e btsync ]; then | |
39 | rm btsync | |
40 | fi | |
41 | echo "Extraction of Bittorrent Sync." | |
42 | tar xvfz btsync_freebsd_x64.tar.gz | |
43 | echo "Copying Bittorrent Sync." | |
44 | cp btsync /usr/local/sbin/btsync | |
45 | chmod 755 /usr/local/sbin/btsync | |
46 | ||
47 | echo "Initialization of the daemon...ROAR" | |
48 | cat > "/etc/rc.d/btsync" <<EOF | |
49 | #!/bin/sh | |
50 | ||
51 | # PROVIDE: btsync | |
52 | # REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv | |
53 | ||
54 | # btsync service | |
55 | # Take from <https://gist.github.com/MendelGusmao/5398362> | |
56 | # Replace with linux users you want to run BTSync clients for | |
57 | ||
58 | BTSYNC_USERS="btsync" | |
59 | DAEMON=/usr/local/sbin/btsync | |
60 | ||
61 | name="btsync" | |
62 | rcvar="btsync_enable" | |
63 | command="/usr/local/sbin/\${name}" | |
64 | ||
65 | start() { | |
66 | echo "Starting btsync..." | |
67 | for btsuser in \$BTSYNC_USERS; do | |
68 | echo " - for \$btsuser" | |
69 | HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\` | |
70 | if [ -d \$HOMEDIR/.config ]; then | |
71 | su -l \$btsuser -c "\$command --config \$HOMEDIR/.config/btsync/config.json" | |
72 | fi | |
73 | done | |
74 | } | |
75 | ||
76 | stop() { | |
77 | echo "Stopping btsync..." | |
78 | for btsuser in \$BTSYNC_USERS; do | |
79 | HOMEDIR=\`getent passwd \$btsuser | cut -d: -f6\` | |
80 | if [ -d \$HOMEDIR/.config ]; then | |
81 | dbpid=\`pgrep -u \$btsuser btsync\` | |
82 | if [ -z "\$dbpid" ]; then | |
83 | echo "btsync for USER \$btsuser: not running." | |
84 | else | |
85 | echo "Stopping btsync for USER \$btsuser: running (pid \$dbpid)" | |
86 | kill \$dbpid | |
87 | until [ -z "\$dbpid" ] | |
88 | do | |
89 | dbpid=\`pgrep -u \$btsuser btsync\` | |
90 | echo -n "." | |
91 | sleep 1 | |
92 | done | |
93 | echo "" | |
94 | fi | |
95 | fi | |
96 | done | |
97 | } | |
98 | ||
99 | status() { | |
100 | for btsuser in \$BTSYNC_USERS; do | |
101 | dbpid=\`pgrep -u $btsuser btsync\` | |
102 | if [ -z "\$dbpid" ]; then | |
103 | echo "btsync for USER \$btsuser: not running." | |
104 | else | |
105 | echo "btsync for USER \$btsuser: running (pid \$dbpid)" | |
106 | fi | |
107 | done | |
108 | } | |
109 | ||
110 | case "\$1" in | |
111 | start) | |
112 | start | |
113 | ;; | |
114 | stop) | |
115 | stop | |
116 | ;; | |
117 | restart|reload|force-reload) | |
118 | stop | |
119 | start | |
120 | ;; | |
121 | status) | |
122 | status | |
123 | ;; | |
124 | *) | |
125 | echo "Usage: /etc/rc.d/btsync {start|stop|reload|force-reload|restart|status}" | |
126 | exit 1 | |
127 | esac | |
128 | exit 0 | |
129 | ||
130 | EOF | |
131 | chmod 755 /etc/rc.d/btsync | |
132 | ||
133 | if [ `more /etc/rc.conf | grep btsync | wc -l` -lt 1 ]; then | |
134 | echo "btsync_enable=\"YES\"" >> /etc/rc.conf | |
135 | fi | |
136 | ||
137 | echo "Web Interface configuration" | |
138 | echo -n "User login to the web interface : " | |
139 | read user | |
140 | echo -n "Password to connect to the web interface : " | |
141 | read pwd | |
142 | ||
143 | cat > "$config_btsync_dir/config.json" <<EOF | |
144 | { | |
145 | "device_name": "Freenas", | |
146 | "listening_port" : 0, // 0 - randomize port | |
147 | ||
148 | /* storage_path dir contains auxilliary app files | |
149 | if no storage_path field: .sync dir created in the directory | |
150 | where binary is located. | |
151 | otherwise user-defined directory will be used | |
152 | */ | |
153 | "storage_path" : "/home/btsync/.config/btsync", | |
154 | ||
155 | // uncomment next line if you want to set location of pid file | |
156 | // "pid_file" : "/var/run/syncapp/syncapp.pid", | |
157 | ||
158 | "check_for_updates" : true, | |
159 | "use_upnp" : true, // use UPnP for port mapping | |
160 | ||
161 | /* limits in kB/s | |
162 | 0 - no limit | |
163 | */ | |
164 | "download_limit" : 0, | |
165 | "upload_limit" : 0, | |
166 | ||
167 | /* remove "listen" field to disable WebUI | |
168 | remove "login" and "password" fields to disable credentials check | |
169 | */ | |
170 | "webui" : | |
171 | { | |
172 | "listen" : "0.0.0.0:8888", | |
173 | "login" : "$user", | |
174 | "password" : "$pwd" | |
175 | } | |
176 | ||
177 | } | |
178 | EOF | |
179 | chown btsync $config_btsync_dir/config.json | |
180 | chmod 744 $config_btsync_dir/config.json | |
181 | ||
182 | echo "Launching btsync :" | |
183 | /etc/rc.d/btsync start | |
184 | fi |