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