HowTo Install Redmine 12x with Mercurial and Subversion on Ubuntu Server 1004 » History » Version 54

Dimitry Profus, 2011-09-22 12:32

1 1 Dimitry Profus
h1. HowTo Install Redmine 1.2.x with Mercurial and Subversion on Ubuntu Server 10.04
2 1 Dimitry Profus
3 1 Dimitry Profus
{{toc}}
4 1 Dimitry Profus
5 1 Dimitry Profus
h2. Redmine Installation
6 1 Dimitry Profus
7 1 Dimitry Profus
# Install the LAMP stack
8 1 Dimitry Profus
<pre>
9 1 Dimitry Profus
$ sudo tasksel install lamp-server
10 1 Dimitry Profus
</pre>
11 1 Dimitry Profus
# Install the required packages
12 1 Dimitry Profus
<pre>
13 1 Dimitry Profus
$ sudo apt-get install build-essential subversion llibmysqlclient15-dev libdigest-sha1-perl libgemplugin-ruby libgemplugin-ruby1.8 libruby-extras libruby1.8-extras rails rake ruby rubygems rubygems1.8 ruby1.8-dev libopenssl-ruby1.8 
14 1 Dimitry Profus
</pre>
15 1 Dimitry Profus
# Install the required Ruby gems
16 1 Dimitry Profus
<pre>
17 1 Dimitry Profus
$ sudo gem install rails -v=2.3.11 --no-ri --no-rdoc
18 1 Dimitry Profus
$ sudo gem install rake -v=0.8.7 --no-ri --no-rdoc
19 1 Dimitry Profus
$ sudo gem uninstall rake -v=0.9.2 
20 1 Dimitry Profus
$ sudo gem install i18n -v=0.4.2 --no-ri --no-rdoc
21 1 Dimitry Profus
$ sudo gem install mysql --no-ri --no-rdoc
22 1 Dimitry Profus
</pre> 
23 1 Dimitry Profus
# Download Redmine into /user/share/redmine directory
24 1 Dimitry Profus
<pre>
25 1 Dimitry Profus
$ sudo svn co http://redmine.rubyforge.org/svn/branches/1.2-stable /usr/share/redmine
26 1 Dimitry Profus
</pre>
27 1 Dimitry Profus
# Create an empty MySQL database and accompanying user named redmine for example.
28 1 Dimitry Profus
<pre>
29 1 Dimitry Profus
$ mysql -u root -p
30 1 Dimitry Profus
(enter the mysql root user password)
31 1 Dimitry Profus
> create database redmine character set utf8;
32 1 Dimitry Profus
> create user 'redmine'@'localhost' identified by '[password]';
33 1 Dimitry Profus
> grant all privileges on redmine.* to 'redmine'@'localhost' identified by '[password]';
34 1 Dimitry Profus
> exit
35 1 Dimitry Profus
</pre>
36 1 Dimitry Profus
# Copy config/database.yml.example to config/database.yml and edit this file in order to configure your database settings for "production" environment.
37 1 Dimitry Profus
<pre>
38 1 Dimitry Profus
$ sudo cp /usr/share/redmine/config/database.yml.example /usr/share/redmine/config/database.yml
39 1 Dimitry Profus
40 1 Dimitry Profus
$ sudo nano /usr/share/redmine/config/database.yml
41 1 Dimitry Profus
42 1 Dimitry Profus
Modify to the following and save (ctrl+x)
43 1 Dimitry Profus
44 1 Dimitry Profus
production:
45 1 Dimitry Profus
  adapter: mysql
46 1 Dimitry Profus
  socket: /var/run/mysqld/mysqld.sock
47 1 Dimitry Profus
  database: redmine
48 1 Dimitry Profus
  host: localhost
49 1 Dimitry Profus
  username: redmine
50 1 Dimitry Profus
  password: [password]
51 1 Dimitry Profus
  encoding: utf8
52 1 Dimitry Profus
</pre>
53 1 Dimitry Profus
# Generate a session store secret.
54 1 Dimitry Profus
<pre>
55 1 Dimitry Profus
$ cd /usr/share/redmine
56 1 Dimitry Profus
57 1 Dimitry Profus
$ sudo rake generate_session_store
58 1 Dimitry Profus
</pre>
59 1 Dimitry Profus
# Create the database structure, by running the following command under the application root directory:
60 1 Dimitry Profus
<pre>
61 1 Dimitry Profus
$ cd /usr/share/redmine
62 1 Dimitry Profus
63 1 Dimitry Profus
$ sudo rake db:migrate RAILS_ENV="production" 
64 1 Dimitry Profus
</pre>
65 1 Dimitry Profus
# Insert default configuration data in database, by running the following command:
66 1 Dimitry Profus
<pre>
67 1 Dimitry Profus
$ sudo RAILS_ENV=production rake redmine:load_default_data
68 1 Dimitry Profus
</pre>
69 1 Dimitry Profus
# Setting up permissions
70 1 Dimitry Profus
<pre>
71 1 Dimitry Profus
$ cd /usr/share/redmine
72 1 Dimitry Profus
$ sudo chown -R www-data:www-data files log tmp public/plugin_assets
73 1 Dimitry Profus
$ sudo chmod -R 755 files log tmp public/plugin_assets
74 1 Dimitry Profus
</pre>
75 1 Dimitry Profus
# Test using the webrick web server
76 1 Dimitry Profus
<pre>
77 1 Dimitry Profus
$ cd /usr/share/redmine
78 1 Dimitry Profus
79 1 Dimitry Profus
$ ruby script/server webrick -e production
80 1 Dimitry Profus
81 1 Dimitry Profus
Point your web browser at http://[my server ip]:3000
82 1 Dimitry Profus
83 1 Dimitry Profus
You should now see the application welcome page.
84 1 Dimitry Profus
</pre>
85 1 Dimitry Profus
86 1 Dimitry Profus
87 1 Dimitry Profus
h2. Apache Integration
88 1 Dimitry Profus
89 1 Dimitry Profus
# Install the required packages
90 1 Dimitry Profus
<pre>
91 1 Dimitry Profus
$ sudo apt-get install libapache2-mod-passenger
92 1 Dimitry Profus
</pre>
93 1 Dimitry Profus
# Add a symbolic link to the public redmine web directory
94 1 Dimitry Profus
<pre>
95 1 Dimitry Profus
$ sudo ln -s /usr/share/redmine/public /var/www/redmine
96 1 Dimitry Profus
</pre>
97 1 Dimitry Profus
# Configure Passanger to run as www-data
98 1 Dimitry Profus
<pre>
99 1 Dimitry Profus
$ sudo nano /etc/apache2/mods-available/passenger.conf
100 1 Dimitry Profus
101 1 Dimitry Profus
Add the follow line and save (ctrl+x)
102 1 Dimitry Profus
103 1 Dimitry Profus
PassengerDefaultUser www-data
104 1 Dimitry Profus
</pre>
105 1 Dimitry Profus
# Create a new Apache site file
106 1 Dimitry Profus
<pre> 
107 1 Dimitry Profus
$ sudo nano /etc/apache2/sites-available/redmine 
108 1 Dimitry Profus
</pre>
109 1 Dimitry Profus
Add the following lines and save (ctrl+x)
110 1 Dimitry Profus
<pre>
111 1 Dimitry Profus
<VirtualHost *:80>
112 1 Dimitry Profus
        ServerAdmin webmaster@localhost
113 1 Dimitry Profus
        DocumentRoot /var/www
114 1 Dimitry Profus
        ServerName myservername
115 1 Dimitry Profus
        
116 1 Dimitry Profus
        RewriteEngine on
117 1 Dimitry Profus
        RewriteRule   ^/$  /redmine  [R]
118 1 Dimitry Profus
119 1 Dimitry Profus
        <Directory /var/www/redmine>
120 1 Dimitry Profus
                RailsBaseURI /redmine
121 1 Dimitry Profus
                PassengerResolveSymlinksInDocumentRoot on
122 1 Dimitry Profus
        </Directory>
123 1 Dimitry Profus
124 1 Dimitry Profus
        ErrorLog /var/log/apache2/error.log
125 1 Dimitry Profus
126 1 Dimitry Profus
        # Possible values include: debug, info, notice, warn, error, crit,
127 1 Dimitry Profus
        # alert, emerg.
128 1 Dimitry Profus
        LogLevel warn
129 1 Dimitry Profus
130 1 Dimitry Profus
        CustomLog /var/log/apache2/access.log combined
131 1 Dimitry Profus
</VirtualHost>
132 1 Dimitry Profus
</pre>
133 1 Dimitry Profus
For SSL add the following text instead
134 1 Dimitry Profus
<pre>
135 1 Dimitry Profus
<VirtualHost *:443>
136 1 Dimitry Profus
        ServerAdmin webmaster@localhost
137 1 Dimitry Profus
        DocumentRoot /var/www
138 1 Dimitry Profus
        ServerName myservername
139 1 Dimitry Profus
140 1 Dimitry Profus
        SSLEngine On
141 1 Dimitry Profus
        SSLCertificateFile /etc/apache2/ssl/redmine.pem
142 1 Dimitry Profus
143 1 Dimitry Profus
        RewriteEngine on
144 1 Dimitry Profus
        RewriteRule   ^/$  /redmine  [R]
145 1 Dimitry Profus
146 1 Dimitry Profus
        <Directory /var/www/redmine>
147 1 Dimitry Profus
                RailsBaseURI /redmine
148 1 Dimitry Profus
                PassengerResolveSymlinksInDocumentRoot on
149 1 Dimitry Profus
        </Directory>
150 1 Dimitry Profus
151 1 Dimitry Profus
        ErrorLog /var/log/apache2/error.log
152 1 Dimitry Profus
153 1 Dimitry Profus
        # Possible values include: debug, info, notice, warn, error, crit,
154 1 Dimitry Profus
        # alert, emerg.
155 1 Dimitry Profus
        LogLevel warn
156 1 Dimitry Profus
157 1 Dimitry Profus
        CustomLog /var/log/apache2/access.log combined
158 1 Dimitry Profus
</VirtualHost>
159 1 Dimitry Profus
</pre>
160 1 Dimitry Profus
# Enable the Redmine website
161 1 Dimitry Profus
<pre>
162 1 Dimitry Profus
$ sudo a2dissite default
163 1 Dimitry Profus
$ sudo a2ensite redmine
164 1 Dimitry Profus
</pre> 
165 1 Dimitry Profus
# Enable the Passenger and Rewite modules and restart Apache
166 5 Dimitry Profus
<pre>
167 1 Dimitry Profus
$ sudo a2enmod passenger
168 1 Dimitry Profus
$ sudo a2enmod rewrite
169 1 Dimitry Profus
$ sudo /etc/init.d/apache2 restart
170 1 Dimitry Profus
</pre> 
171 1 Dimitry Profus
# Test the setup
172 1 Dimitry Profus
<pre>
173 1 Dimitry Profus
Open up your favorite web browser and goto
174 1 Dimitry Profus
175 1 Dimitry Profus
http://[my site or ip]/redmine
176 1 Dimitry Profus
</pre>
177 1 Dimitry Profus
178 1 Dimitry Profus
h2. Mercurial Integration
179 1 Dimitry Profus
180 1 Dimitry Profus
# Install the latest Mercurial release 
181 1 Dimitry Profus
<pre>
182 1 Dimitry Profus
$ sudo apt-get install python-software-properties
183 1 Dimitry Profus
$ sudo add-apt-repository ppa:mercurial-ppa/releases
184 1 Dimitry Profus
$ sudo apt-get update
185 1 Dimitry Profus
$ sudo apt-get install mercurial libapache-dbi-perl libapache2-mod-perl2
186 1 Dimitry Profus
</pre>
187 1 Dimitry Profus
# Create the hg web directory
188 1 Dimitry Profus
<pre>
189 12 Dimitry Profus
$ sudo mkdir -p /var/hg/repos
190 1 Dimitry Profus
</pre>
191 1 Dimitry Profus
# Create the web cgi script file
192 1 Dimitry Profus
<pre>
193 12 Dimitry Profus
$ sudo nano /var/hg/hgwebdir.cgi
194 1 Dimitry Profus
195 1 Dimitry Profus
Add the following and save
196 1 Dimitry Profus
197 1 Dimitry Profus
#!/usr/bin/env python
198 1 Dimitry Profus
#
199 1 Dimitry Profus
from mercurial import demandimport; demandimport.enable()
200 1 Dimitry Profus
from mercurial.hgweb.hgwebdir_mod import hgwebdir
201 1 Dimitry Profus
import mercurial.hgweb.wsgicgi as wsgicgi
202 1 Dimitry Profus
application = hgwebdir('hgweb.config')
203 1 Dimitry Profus
wsgicgi.launch(application)
204 1 Dimitry Profus
</pre>
205 1 Dimitry Profus
# Create the cgi web config file 
206 1 Dimitry Profus
<pre>
207 12 Dimitry Profus
$ sudo nano /var/hg/hgweb.config
208 1 Dimitry Profus
209 1 Dimitry Profus
Add the following and save
210 1 Dimitry Profus
211 1 Dimitry Profus
[paths]
212 12 Dimitry Profus
/=/var/hg/repos/**
213 1 Dimitry Profus
214 1 Dimitry Profus
[web]
215 1 Dimitry Profus
allow_push = *
216 1 Dimitry Profus
push_ssl = false
217 1 Dimitry Profus
allowbz2 = yes
218 1 Dimitry Profus
allowgz = yes
219 1 Dimitry Profus
allowzip = yes
220 1 Dimitry Profus
</pre>
221 1 Dimitry Profus
# Setup permissions
222 1 Dimitry Profus
<pre>
223 12 Dimitry Profus
$ sudo chown -R www-data:www-data /var/hg
224 12 Dimitry Profus
$ sudo chmod gu+x /var/hg/hgwebdir.cgi
225 1 Dimitry Profus
</pre>
226 1 Dimitry Profus
# Create a Apache config file
227 1 Dimitry Profus
<pre>
228 1 Dimitry Profus
$ sudo nano /etc/apache2/conf.d/hg.config
229 1 Dimitry Profus
230 1 Dimitry Profus
Add the following and save
231 1 Dimitry Profus
 
232 1 Dimitry Profus
PerlLoadModule Apache::Redmine
233 12 Dimitry Profus
ScriptAlias /hg  "/var/hg/hgwebdir.cgi"
234 1 Dimitry Profus
<Location /hg  >
235 1 Dimitry Profus
	AuthType Basic
236 13 Dimitry Profus
	AuthName "Redmine Mercurial Repository" 
237 1 Dimitry Profus
	Require valid-user
238 1 Dimitry Profus
239 1 Dimitry Profus
	#Redmine auth
240 1 Dimitry Profus
	PerlAccessHandler Apache::Authn::Redmine::access_handler
241 1 Dimitry Profus
	PerlAuthenHandler Apache::Authn::Redmine::authen_handler
242 1 Dimitry Profus
	RedmineDSN "DBI:mysql:database=redmine;host=localhost" 
243 1 Dimitry Profus
	RedmineDbUser "redmine" 
244 1 Dimitry Profus
	RedmineDbPass "password" 
245 1 Dimitry Profus
</Location>
246 1 Dimitry Profus
</pre>
247 1 Dimitry Profus
# Add a symbolic link to Redmine.pm
248 1 Dimitry Profus
<pre>
249 1 Dimitry Profus
$ sudo ln -s /usr/share/redmine/extra/svn/Redmine.pm /usr/lib/perl5/Apache/Redmine.pm
250 1 Dimitry Profus
$ sudo ln -s /usr/share/redmine/extra/svn/Redmine.pm /usr/lib/perl5/Apache2/Redmine.pm  
251 1 Dimitry Profus
</pre>
252 1 Dimitry Profus
# Enable the required Apache modules and restart Apache
253 1 Dimitry Profus
<pre>
254 1 Dimitry Profus
$ sudo /etc/init.d/apache2 restart
255 1 Dimitry Profus
</pre>
256 1 Dimitry Profus
# Create a new test repository and project in Redmine
257 1 Dimitry Profus
<pre>
258 12 Dimitry Profus
$ sudo hg init /var/hg/repos/test
259 12 Dimitry Profus
$ sudo chown -R www-data:www-data /var/hg/repos/test
260 1 Dimitry Profus
261 1 Dimitry Profus
Create a new project with and identifier 'test'
262 1 Dimitry Profus
263 1 Dimitry Profus
In the project Settings > Repository set
264 1 Dimitry Profus
SCM: Mercurial
265 12 Dimitry Profus
Path to repository: /var/hg/repos/test
266 1 Dimitry Profus
Press the 'Create' button
267 1 Dimitry Profus
268 1 Dimitry Profus
Goto to the Repository tab of the test project
269 1 Dimitry Profus
</pre>
270 1 Dimitry Profus
# View the test repository in the web browser 
271 1 Dimitry Profus
<pre>
272 1 Dimitry Profus
> http://[my site name]/hg/test
273 1 Dimitry Profus
</pre>
274 1 Dimitry Profus
275 1 Dimitry Profus
h2. Subversion Integration
276 1 Dimitry Profus
277 1 Dimitry Profus
# Install the latest Mercurial release 
278 1 Dimitry Profus
<pre>
279 1 Dimitry Profus
$ sudo apt-get install subversion libapache2-svn libapache-dbi-perl libapache2-mod-perl2
280 1 Dimitry Profus
</pre>
281 1 Dimitry Profus
# Create the svn repository directory
282 1 Dimitry Profus
<pre>
283 12 Dimitry Profus
$ sudo mkdir /var/svn
284 1 Dimitry Profus
</pre>
285 1 Dimitry Profus
# Setup permissions
286 1 Dimitry Profus
<pre>
287 12 Dimitry Profus
$ sudo chown -R www-data:www-data /var/svn
288 1 Dimitry Profus
</pre>
289 1 Dimitry Profus
# Add a symbolic link to Redmine.pm
290 1 Dimitry Profus
<pre>
291 1 Dimitry Profus
$ sudo ln -s /usr/share/redmine/extra/svn/Redmine.pm /usr/lib/perl5/Apache/Redmine.pm
292 1 Dimitry Profus
$ sudo ln -s /usr/share/redmine/extra/svn/Redmine.pm /usr/lib/perl5/Apache2/Redmine.pm  
293 1 Dimitry Profus
</pre>
294 1 Dimitry Profus
# Create a Apache config file
295 1 Dimitry Profus
<pre>
296 1 Dimitry Profus
$ sudo nano /etc/apache2/conf.d/svn.config
297 1 Dimitry Profus
</pre>
298 1 Dimitry Profus
Add the following and save
299 1 Dimitry Profus
<pre>
300 16 Dimitry Profus
PerlLoadModule Apache::Redmine
301 16 Dimitry Profus
<Location /svn>
302 16 Dimitry Profus
	DAV svn
303 16 Dimitry Profus
	SVNParentPath "/var/svn" 
304 16 Dimitry Profus
	Order deny,allow
305 16 Dimitry Profus
	Deny from all
306 16 Dimitry Profus
	Satisfy any
307 1 Dimitry Profus
308 16 Dimitry Profus
	PerlAccessHandler Apache::Authn::Redmine::access_handler
309 16 Dimitry Profus
	PerlAuthenHandler Apache::Authn::Redmine::authen_handler
310 16 Dimitry Profus
	AuthType Basic
311 16 Dimitry Profus
	AuthName "Redmine Subversion Repository" 
312 1 Dimitry Profus
313 16 Dimitry Profus
	#read-only access    
314 16 Dimitry Profus
	<Limit GET PROPFIND OPTIONS REPORT>
315 16 Dimitry Profus
		Require valid-user
316 16 Dimitry Profus
		Allow from [my server ip]
317 16 Dimitry Profus
		# Allow from another-ip
318 16 Dimitry Profus
		 Satisfy any
319 16 Dimitry Profus
	</Limit>
320 16 Dimitry Profus
		# write access
321 16 Dimitry Profus
		<LimitExcept GET PROPFIND OPTIONS REPORT>
322 16 Dimitry Profus
		Require valid-user
323 16 Dimitry Profus
	</LimitExcept>
324 1 Dimitry Profus
325 16 Dimitry Profus
	## for mysql
326 16 Dimitry Profus
	RedmineDSN "DBI:mysql:database=redmine;host=localhost" 
327 16 Dimitry Profus
	RedmineDbUser "redmine" 
328 16 Dimitry Profus
	RedmineDbPass "password" 
329 16 Dimitry Profus
</Location>
330 1 Dimitry Profus
</pre>
331 1 Dimitry Profus
# Enable the required Apache modules and restart Apache
332 1 Dimitry Profus
<pre>
333 1 Dimitry Profus
$ sudo a2enmod dav_svn
334 1 Dimitry Profus
$ sudo /etc/init.d/apache2 restart
335 1 Dimitry Profus
</pre>
336 1 Dimitry Profus
# Create a new test repository
337 1 Dimitry Profus
<pre>
338 12 Dimitry Profus
$ sudo svnadmin create /var/svn/test
339 12 Dimitry Profus
$ sudo chown -R www-data:www-data /var/svn/test
340 1 Dimitry Profus
</pre>
341 1 Dimitry Profus
342 7 Dimitry Profus
h2. Automate Repository Creation
343 5 Dimitry Profus
344 3 Dimitry Profus
# Enable WS for repository management and generate and API key
345 3 Dimitry Profus
<pre>
346 3 Dimitry Profus
* From the Redmine Administration menu select Settings
347 3 Dimitry Profus
* Click on the Repositories tab
348 3 Dimitry Profus
* Enable the 'Enable WS for repository management' checkbox
349 3 Dimitry Profus
* Click the 'Generate a key' link 
350 3 Dimitry Profus
* Press the 'Save' button
351 3 Dimitry Profus
</pre>
352 3 Dimitry Profus
# Modify reposman.rb
353 3 Dimitry Profus
<pre>
354 3 Dimitry Profus
$ sudo nano /usr/share/extra/svn/reposman.rb
355 3 Dimitry Profus
356 3 Dimitry Profus
Add the following to module SCM and save  
357 3 Dimitry Profus
358 3 Dimitry Profus
  module Mercurial
359 3 Dimitry Profus
    def self.create(path)
360 3 Dimitry Profus
      Dir.mkdir path
361 3 Dimitry Profus
      Dir.chdir(path) do
362 3 Dimitry Profus
        system_or_raise "hg init"
363 3 Dimitry Profus
      end
364 3 Dimitry Profus
    end
365 3 Dimitry Profus
  end
366 3 Dimitry Profus
</pre>
367 6 Dimitry Profus
# Schedule the reposman.rb script to run every minute
368 3 Dimitry Profus
<pre>
369 3 Dimitry Profus
$ sudo nano /etc/cron.d/redmine
370 7 Dimitry Profus
</pre>
371 9 Dimitry Profus
Add one of the following lines (not both) and save.
372 1 Dimitry Profus
(Note: you will need to replace [my API key]  with the API key you generated in step 1) 
373 7 Dimitry Profus
.
374 11 Dimitry Profus
To create subversion repositories add:
375 7 Dimitry Profus
<pre>
376 14 Dimitry Profus
* * * * * root ruby /usr/share/redmine/extra/svn/reposman.rb --redmine localhost/redmine --scm Subversion --svn-dir /var/hg/repos --owner www-data --url file:///var/svn --key=[my API key] >> /var/log/reposman.log
377 7 Dimitry Profus
</pre>
378 11 Dimitry Profus
OR to create Mecurial repositories add:
379 8 Dimitry Profus
<pre>
380 14 Dimitry Profus
* * * * * root ruby /usr/share/redmine/extra/svn/reposman.rb --redmine localhost/redmine --scm Mercurial --svn-dir /var/hg/repos --owner www-data --url /var/hg/repos --key=[my API key] >> /var/log/reposman.log
381 1 Dimitry Profus
</pre>
382 5 Dimitry Profus
383 15 Dimitry Profus
h2. Automatic refresh of repositories in Redmine
384 15 Dimitry Profus
385 15 Dimitry Profus
# Schedule the fetch_changesets script to run every 15 minutes
386 15 Dimitry Profus
<pre>
387 17 Dimitry Profus
$ sudo nano /var/cron.d/redmine
388 17 Dimitry Profus
389 17 Dimitry Profus
Add the following line and save
390 17 Dimitry Profus
391 15 Dimitry Profus
*/15 * * * * root ruby /usr/share/redmine/script/runner "Repository.fetch_changesets" -e production > /dev/null 2>&1
392 15 Dimitry Profus
</pre>
393 16 Dimitry Profus
# Setup a changegroup script on the Mercurial server to run fetch_changesets after each push to a Mercurial repository 
394 15 Dimitry Profus
<pre>
395 15 Dimitry Profus
$ sudo nano /var/hg/changegroup-hook
396 15 Dimitry Profus
</pre>
397 15 Dimitry Profus
Add the following text and save
398 15 Dimitry Profus
(Note: you will need to replace [your API key] the API key you generated in Redmine
399 15 Dimitry Profus
<pre>
400 15 Dimitry Profus
#!/bin/sh
401 15 Dimitry Profus
curl "http://localhost/redmine/sys/fetch_changesets?key=[your API key]"  > /dev/null 2>&1
402 15 Dimitry Profus
</pre>
403 15 Dimitry Profus
Setup permissions
404 15 Dimitry Profus
<pre>
405 15 Dimitry Profus
$ sudo chown www-data:www-data /var/hg/changegroup-hook
406 15 Dimitry Profus
$ sudo chmod ug+x /var/hg/changegroup-hook
407 15 Dimitry Profus
</pre>
408 15 Dimitry Profus
Modify the hgweb.config file
409 15 Dimitry Profus
<pre>
410 15 Dimitry Profus
$ sudo nano /var/hg/hgweb.config
411 15 Dimitry Profus
</pre>
412 15 Dimitry Profus
Add the following section and save
413 15 Dimitry Profus
<pre>
414 15 Dimitry Profus
[hooks]
415 15 Dimitry Profus
changegroup = /var/hg/changegroup-hook
416 15 Dimitry Profus
</pre>
417 15 Dimitry Profus
418 1 Dimitry Profus
h2. Email Integration
419 1 Dimitry Profus
420 1 Dimitry Profus
# Install and configure Sendmail
421 1 Dimitry Profus
<pre>
422 1 Dimitry Profus
$ sudo apt-get install sendmail
423 1 Dimitry Profus
$ sudo sendmailconfig
424 1 Dimitry Profus
425 1 Dimitry Profus
(Answer Yes to all questions which you will be asked)
426 1 Dimitry Profus
</pre>
427 1 Dimitry Profus
# Update the Redmine configuration file
428 1 Dimitry Profus
<pre>
429 1 Dimitry Profus
$ sudo nano /usr/share/redmine/config/configuration.yml
430 1 Dimitry Profus
431 1 Dimitry Profus
Add the following text and save
432 1 Dimitry Profus
433 1 Dimitry Profus
 production:
434 1 Dimitry Profus
   email_delivery:
435 1 Dimitry Profus
     delivery_method: :sendmail
436 1 Dimitry Profus
</pre>
437 18 Dimitry Profus
438 23 Dimitry Profus
h2. Backup to Amazon S3 cloud storage
439 18 Dimitry Profus
440 22 Dimitry Profus
# Create an account at http://aws.amazon.com/ 
441 18 Dimitry Profus
# Create a S3 bucket using the aws management console https://console.aws.amazon.com/ec2/home
442 21 Dimitry Profus
# View your Access Keys at https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key
443 18 Dimitry Profus
# Build and install fuse to 2.8.4
444 18 Dimitry Profus
<pre>
445 18 Dimitry Profus
$ sudo wget https://launchpad.net/ubuntu/+archive/primary/+files/fuse_2.8.4.orig.tar.gz
446 18 Dimitry Profus
$ cd fuse-2.8.4/
447 18 Dimitry Profus
$ tar xzf fuse_2.8.4.orig.tar.gz 
448 18 Dimitry Profus
$ sudo ./configure 
449 18 Dimitry Profus
$ sudo make
450 18 Dimitry Profus
$ sudo make install
451 18 Dimitry Profus
</pre>
452 18 Dimitry Profus
# Build and install s3fs 1.61 
453 18 Dimitry Profus
<pre>
454 18 Dimitry Profus
$ sudo apt-get install libxml2-dev
455 18 Dimitry Profus
$ wget http://s3fs.googlecode.com/files/s3fs-1.61.tar.gz
456 18 Dimitry Profus
$ tar xzf s3fs-1.61.tar.gz 
457 18 Dimitry Profus
$ cd s3fs-1.61/
458 18 Dimitry Profus
$ sudo ./configure 
459 18 Dimitry Profus
$ sudo make
460 18 Dimitry Profus
$ sudo make install
461 18 Dimitry Profus
</pre>
462 42 Dimitry Profus
# Create a s3fs password file
463 18 Dimitry Profus
<pre>
464 18 Dimitry Profus
$ sudo nano /etc/passwd-s3fs
465 18 Dimitry Profus
</pre>
466 24 Dimitry Profus
Added your 'Access Key ID' and 'Secret Access Key' separated by a colon
467 24 Dimitry Profus
<pre>
468 18 Dimitry Profus
[accessKeyId]:[secretAccessKey]
469 18 Dimitry Profus
</pre>
470 18 Dimitry Profus
Setup permissions
471 18 Dimitry Profus
<pre>
472 18 Dimitry Profus
sudo chmod 650 /etc/passwd-s3fs
473 18 Dimitry Profus
</pre> 
474 18 Dimitry Profus
# Create mounting point
475 18 Dimitry Profus
<pre>
476 18 Dimitry Profus
$ sudo mkdir /mnt/s3
477 18 Dimitry Profus
</pre>
478 25 Dimitry Profus
# Mount your S3 bucket
479 18 Dimitry Profus
<pre>
480 18 Dimitry Profus
$ sudo s3fs [your-s3-bucket-name] /mnt/s3 -ouse_cache=/tmp -o allow_other
481 18 Dimitry Profus
</pre>
482 37 Dimitry Profus
# Test it worked and unmount
483 18 Dimitry Profus
<pre>
484 38 Dimitry Profus
$ echo hello > /mnt/s3/welcome.txt
485 1 Dimitry Profus
$ ls /mnt/s3
486 36 Dimitry Profus
$ sudo umount /mnt/s3
487 18 Dimitry Profus
</pre>
488 18 Dimitry Profus
# Create upstart job to mount the s3 file system start up
489 18 Dimitry Profus
<pre>
490 18 Dimitry Profus
$ sudo nano /etc/init/s3.conf
491 18 Dimitry Profus
</pre>
492 18 Dimitry Profus
Add the following text and save.
493 18 Dimitry Profus
<pre>
494 43 Dimitry Profus
description "Mount Amazon S3 file system on system start"
495 18 Dimitry Profus
496 18 Dimitry Profus
start on (local-filesystems and net-device-up IFACE!=lo)
497 18 Dimitry Profus
stop on runlevel [016]
498 18 Dimitry Profus
499 18 Dimitry Profus
respawn
500 18 Dimitry Profus
501 18 Dimitry Profus
exec s3fs -f [your-s3-bucket-name] /mnt/s3 -ouse_cache=/tmp -o allow_other
502 18 Dimitry Profus
</pre>
503 35 Dimitry Profus
# Start the s3 job
504 35 Dimitry Profus
<pre>
505 35 Dimitry Profus
sudo start s3
506 1 Dimitry Profus
</pre>
507 40 Dimitry Profus
# Test it worked
508 38 Dimitry Profus
<pre>
509 38 Dimitry Profus
$ echo hello > /mnt/s3/welcome2.txt
510 38 Dimitry Profus
$ ls /mnt/s3
511 39 Dimitry Profus
</pre>
512 19 Dimitry Profus
# Create the backup script file
513 19 Dimitry Profus
<pre>
514 27 Dimitry Profus
$ sudo apt-get install mailutils
515 32 Dimitry Profus
$ sudo nano /usr/local/bin/backup-redmine.sh
516 29 Dimitry Profus
</pre>
517 19 Dimitry Profus
Add the following text and save.
518 29 Dimitry Profus
<pre>
519 19 Dimitry Profus
#!/bin/bash
520 50 Dimitry Profus
# Script to backup Redmine files to a mounted storage device
521 50 Dimitry Profus
# with daily, weekly, monthly backup rotation
522 1 Dimitry Profus
523 1 Dimitry Profus
# Admin email address
524 50 Dimitry Profus
admin_email="admin@yourdomain.com" 
525 1 Dimitry Profus
526 32 Dimitry Profus
# What to backup
527 50 Dimitry Profus
db_dump_file="/usr/share/redmine/db/redmine-database-dump.sql" 
528 1 Dimitry Profus
529 50 Dimitry Profus
backup_files="$db_dump_file /usr/share/redmine/files /var/hg /var/svn" 
530 50 Dimitry Profus
backup_files="$backup_files /etc/apache2/conf.d/hg.conf /etc/apache2/sites-available/redmine"
531 50 Dimitry Profus
backup_files="$backup_files /etc/init/s3.conf /etc/cron.d/redmine /usr/local/bin/backup-redmine.sh"
532 50 Dimitry Profus
533 1 Dimitry Profus
# Where to backup to
534 50 Dimitry Profus
backup_dir="/mnt/s3" 
535 1 Dimitry Profus
536 32 Dimitry Profus
# Set database access
537 50 Dimitry Profus
redmine_db_name="redmine" 
538 50 Dimitry Profus
redmine_db_user="redmine" 
539 50 Dimitry Profus
redmine_db_password="password" 
540 1 Dimitry Profus
541 1 Dimitry Profus
# Encryption
542 50 Dimitry Profus
encrypt="false" 
543 50 Dimitry Profus
secret_passphrase="your_gpg_passphrase" 
544 50 Dimitry Profus
545 32 Dimitry Profus
# Set rotation in units of days
546 32 Dimitry Profus
daily_remove_older_than=6
547 50 Dimitry Profus
weekly_remove_older_than=30
548 50 Dimitry Profus
monthly_remove_older_than=62
549 1 Dimitry Profus
550 32 Dimitry Profus
# Redirect stderr to a log file
551 50 Dimitry Profus
error_log="/tmp/backup-redmine.log" 
552 32 Dimitry Profus
exec 6>&2
553 32 Dimitry Profus
exec 2>$error_log
554 1 Dimitry Profus
555 32 Dimitry Profus
on_exit() {
556 1 Dimitry Profus
    # Restore IO output
557 1 Dimitry Profus
    exec 2>&6  6>$-
558 1 Dimitry Profus
559 1 Dimitry Profus
    # Check for errors
560 1 Dimitry Profus
    if [ -s "$error_log" ]; then
561 50 Dimitry Profus
        logger -t "$0"  -s "#### Backup Failed ####" 
562 50 Dimitry Profus
        logger -t "$0" -s -f "$error_log" 
563 1 Dimitry Profus
        cat "$error_log" | mail -s "Backup failed!"  $admin_email
564 1 Dimitry Profus
     else
565 50 Dimitry Profus
        logger -t "$0" -s "Backup Complete" 
566 1 Dimitry Profus
    fi
567 1 Dimitry Profus
568 1 Dimitry Profus
    # Clean up
569 32 Dimitry Profus
    rm -f $error_log
570 1 Dimitry Profus
}
571 32 Dimitry Profus
572 50 Dimitry Profus
trap on_exit EXIT SIGHUP SIGINT SIGQUIT SIGTERM
573 50 Dimitry Profus
574 1 Dimitry Profus
# Setup variables for the archive filename.
575 26 Dimitry Profus
hostname=$(hostname -s)
576 32 Dimitry Profus
date_time_stamp=`date +%Y-%m-%d_%Hh%Mm`        # Datestamp e.g 2011-12-31_23h59m
577 1 Dimitry Profus
date_stamp=`date +%Y-%m-%d`                    # Date p e.g 2011-12-31
578 32 Dimitry Profus
date_day_of_week=`date +%A`                    # Day of the week e.g. Monday
579 32 Dimitry Profus
date_day_of_month=`date +%e`                   # Date of the Month e.g. 27
580 31 Dimitry Profus
581 50 Dimitry Profus
# Is the  backup directory mounted?
582 50 Dimitry Profus
mount | grep -sq "$backup_dir"
583 50 Dimitry Profus
if  [ $? -eq 1 ]; then
584 50 Dimitry Profus
   echo "backup destination ${backup_dir} is not mounted" >&2
585 50 Dimitry Profus
  exit 1
586 50 Dimitry Profus
fi
587 50 Dimitry Profus
588 32 Dimitry Profus
# Make required directories
589 50 Dimitry Profus
[ -d "$backup_dir/monthly" ] || mkdir "$backup_dir/monthly" 
590 50 Dimitry Profus
[ -d "$backup_dir/weekly" ] || mkdir "$backup_dir/weekly" 
591 50 Dimitry Profus
[ -d "$backup_dir/daily" ] || mkdir "$backup_dir/daily" 
592 1 Dimitry Profus
593 49 Dimitry Profus
# Delete old archives
594 45 Dimitry Profus
find "${backup_dir}/monthly" -mtime +"$monthly_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
595 32 Dimitry Profus
find "${backup_dir}/weekly" -mtime +"$weekly_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
596 1 Dimitry Profus
find "${backup_dir}/daily" -mtime +"$daily_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
597 1 Dimitry Profus
598 50 Dimitry Profus
archive_file="${backup_dir}/daily/${hostname}_${date_time_stamp}.tgz" 
599 45 Dimitry Profus
600 50 Dimitry Profus
[ $encrypt == "true" ] && archive_file="${archive_file}.gpg" 
601 50 Dimitry Profus
602 50 Dimitry Profus
# Dump the redmine database
603 50 Dimitry Profus
mysqldump --user="${redmine_db_name}" --password="${redmine_db_password}" "${redmine_db_name}" > $db_dump_file
604 50 Dimitry Profus
605 50 Dimitry Profus
# Write the archive file to the backup directory
606 50 Dimitry Profus
if [ $? == 0 ]; then
607 50 Dimitry Profus
    if [ $encrypt == "true" ]; then
608 54 Dimitry Profus
        tar czP $backup_files | gpg -c -z 0 --yes --no-use-agent --passphrase="${secret_passphrase}" -o "${archive_file}" 
609 50 Dimitry Profus
    else
610 50 Dimitry Profus
        tar czfP "${archive_file}" $backup_files
611 1 Dimitry Profus
    fi
612 1 Dimitry Profus
fi
613 1 Dimitry Profus
614 1 Dimitry Profus
# Make a weekly backup on Saturday
615 45 Dimitry Profus
if [ $date_day_of_week == "Saturday" ]; then
616 1 Dimitry Profus
    weekly_count=$(find "${backup_dir}/weekly" -type f -name "${hostname}_${date_stamp}*" | wc -l)
617 50 Dimitry Profus
    [ $weekly_count == "0" ] && cp "${archive_file}" "${backup_dir}/weekly/" 
618 44 Dimitry Profus
fi
619 44 Dimitry Profus
620 44 Dimitry Profus
# Make a monthly backup on the first day of every  month
621 45 Dimitry Profus
if [ $date_day_of_month == "1" ]; then
622 1 Dimitry Profus
    monthly_count=$(find "${backup_dir}/monthly" -type f -name "${hostname}_${date_stamp}*" | wc -l)
623 50 Dimitry Profus
    [ $monthly_count == "0" ] && cp "${archive_file}" "${backup_dir}/monthly/" 
624 32 Dimitry Profus
fi
625 1 Dimitry Profus
626 52 Dimitry Profus
if [ -s "$error_log" ]; then
627 52 Dimitry Profus
    exit 1
628 52 Dimitry Profus
else
629 52 Dimitry Profus
    exit 0
630 52 Dimitry Profus
fi
631 1 Dimitry Profus
</pre>
632 32 Dimitry Profus
# Setup permissions 
633 47 Dimitry Profus
<pre>
634 1 Dimitry Profus
sudo chmod ug+x /usr/local/bin/backup-redmine.sh
635 1 Dimitry Profus
sudo chmod o-r /usr/local/bin/backup-redmine.sh
636 1 Dimitry Profus
</pre>
637 1 Dimitry Profus
# Schedule the backup to run once a day at 12am
638 1 Dimitry Profus
<pre>
639 1 Dimitry Profus
$ sudo nano /etc/cron.d/redmine
640 1 Dimitry Profus
641 32 Dimitry Profus
Add the following line and save
642 1 Dimitry Profus
643 1 Dimitry Profus
0 0 * * * root /usr/local/bin/backup-redmine.sh
644 1 Dimitry Profus
</pre>
645 41 Dimitry Profus
# Test the script
646 41 Dimitry Profus
<pre>
647 1 Dimitry Profus
$ sudo backup-redmine.sh
648 1 Dimitry Profus
$ ls -R /mnt/s3
649 1 Dimitry Profus
</pre>