HowTo Migrate Redmine to a new server to a new Redmine version » History » Version 22

Toshi MARUYAMA, 2012-09-30 09:27
Spam squashing

1 1 Gergely Szabo
h1. The Question
2 1 Gergely Szabo
3 1 Gergely Szabo
How to move your Redmine data to another machine with a newer Redmine version?
4 1 Gergely Szabo
5 2 Gergely Szabo
|*Machine*|*OS*|*Redmine*|
6 1 Gergely Szabo
|Old|Ubuntu 10.04|0.9.3|
7 1 Gergely Szabo
|New|Debian Wheezy Testing|1.1.2|
8 1 Gergely Szabo
9 1 Gergely Szabo
h1. The Answer
10 1 Gergely Szabo
11 1 Gergely Szabo
The answer can be found mainly in my well commented Redmine backup-script which can also restore data from backup.
12 1 Gergely Szabo
The executable (chmod +x) script is called *redmine_bak*, residing in /root/redmine/ along with a git-repo. See details below.
13 1 Gergely Szabo
<pre>
14 1 Gergely Szabo
#!/bin/bash
15 1 Gergely Szabo
usage() {
16 1 Gergely Szabo
cat <<EOF
17 3 Gergely Szabo
Usage: redmine_bak [ -r | -h ] [commit msg]
18 1 Gergely Szabo
19 1 Gergely Szabo
When called without parameters, the Redmine database and files are dumped to
20 1 Gergely Szabo
git-repo in /root/redmine, then the git-repo is pushed to ssh://git@GitServer.
21 1 Gergely Szabo
22 3 Gergely Szabo
When the first parameter is none of the ones below, the same backup procedure
23 3 Gergely Szabo
is done, but the commit message is the parameter list instead of the date.
24 3 Gergely Szabo
25 1 Gergely Szabo
-r --restore
26 1 Gergely Szabo
Beforehand, check out the desired version of the Redmine database from git-repo.
27 1 Gergely Szabo
This command will restore that version into Redmine.
28 1 Gergely Szabo
29 1 Gergely Szabo
-h --help
30 1 Gergely Szabo
Print this help text.
31 1 Gergely Szabo
EOF
32 1 Gergely Szabo
exit $1
33 1 Gergely Szabo
}
34 1 Gergely Szabo
35 1 Gergely Szabo
DATABASE=`cat /etc/redmine/default/database.yml | sed -rn 's/ *database: (.+)/\1/p'`
36 1 Gergely Szabo
USERNAME=`cat /etc/redmine/default/database.yml | sed -rn 's/ *username: (.+)/\1/p'`
37 1 Gergely Szabo
PASSWORD=`cat /etc/redmine/default/database.yml | sed -rn 's/ *password: (.+)/\1/p'`
38 1 Gergely Szabo
FILES=/var/lib/redmine/default/files
39 1 Gergely Szabo
cd /root/redmine
40 1 Gergely Szabo
41 3 Gergely Szabo
# Help
42 3 Gergely Szabo
if [ "$1" = "-h" -o "$1" = "--help" ]; then
43 3 Gergely Szabo
  usage 0
44 1 Gergely Szabo
45 1 Gergely Szabo
# Restore
46 1 Gergely Szabo
elif [ "$1" = "-r" -o "$1" = "--restore" ]; then
47 3 Gergely Szabo
  /usr/bin/mysql --user=${USERNAME} --password=${PASSWORD} $DATABASE < redmine.sql
48 3 Gergely Szabo
  cp -f [!r][!e][!d][!m][!i][!n][!e]* $FILES
49 1 Gergely Szabo
50 3 Gergely Szabo
# Backup
51 1 Gergely Szabo
else
52 3 Gergely Szabo
  if [ "$1" ]; then MSG="$@"; else MSG="`date`"; fi
53 3 Gergely Szabo
  /usr/bin/mysqldump --user=${USERNAME} --password=${PASSWORD} --skip-extended-insert $DATABASE > redmine.sql
54 3 Gergely Szabo
  cp -f ${FILES}/* .
55 3 Gergely Szabo
  git add *
56 3 Gergely Szabo
  git commit -m "$MSG"
57 3 Gergely Szabo
  git push --all origin
58 1 Gergely Szabo
59 1 Gergely Szabo
fi
60 1 Gergely Szabo
</pre>
61 1 Gergely Szabo
62 1 Gergely Szabo
h2. Prepare Git-repos for Backups
63 1 Gergely Szabo
64 1 Gergely Szabo
We have a third backup machine called GitServer which has a simple github service represented by the git user. We need a bare git repo for redmine:
65 1 Gergely Szabo
<pre>
66 1 Gergely Szabo
git@GitServer ~ $ mkdir redmine.git && cd redmine.git && git --bare init
67 1 Gergely Szabo
</pre>
68 1 Gergely Szabo
69 1 Gergely Szabo
On the Old Redmine machine: We assume its root has a passphrase-less ssh-key, and his public key is stored on the GitServer backup machine in /home/git/.ssh/authorized_keys.
70 1 Gergely Szabo
71 1 Gergely Szabo
Create git-repo on Old Redmine machine:
72 1 Gergely Szabo
<pre>
73 2 Gergely Szabo
# cd /root/redmine
74 1 Gergely Szabo
# git init
75 1 Gergely Szabo
# git remote add ssh://git@GitServer/~/redmine.git
76 1 Gergely Szabo
</pre>
77 1 Gergely Szabo
78 1 Gergely Szabo
h2. Backing Up the Old Machine
79 1 Gergely Szabo
80 1 Gergely Szabo
redmine_bak is called every midnight by cron without parameters, which means back-up.
81 1 Gergely Szabo
It gets database-name, MySQL username and password from the database.yml file.
82 1 Gergely Szabo
Besides the database, the uploaded files are saved too, see the FILES variable for their location.
83 1 Gergely Szabo
84 1 Gergely Szabo
After dumping the database to redmine.sql and copying the files to /root/redmine/ they are all committed to the git repo which, in turn is pushed to the backup-box (GitServer).
85 1 Gergely Szabo
86 1 Gergely Szabo
The advantage of the git-repo is that you can go back to the last correct version even if you notice a corruption 2 weeks too late. You could even use git-bisect.
87 1 Gergely Szabo
88 1 Gergely Szabo
Before the migration, the last backup from the old machine is available on GitServer.
89 1 Gergely Szabo
90 1 Gergely Szabo
h2. The New Machine
91 1 Gergely Szabo
92 1 Gergely Szabo
Redmine's email config should be simply copied from Old the New. Somehow. It's located here:
93 1 Gergely Szabo
<pre>/etc/redmine/default/email.yml</pre>
94 1 Gergely Szabo
95 1 Gergely Szabo
You should be root, have a passphrase-less ssh-key, stored on the GitServer backup machine in /home/git/.ssh/authorized_keys. Same as with the Old Redmine machine. Let's clone the backup repo to the new box.
96 1 Gergely Szabo
<pre>
97 1 Gergely Szabo
# cd /root
98 1 Gergely Szabo
# git clone ssh://git@GitServer/~/redmine.git
99 1 Gergely Szabo
</pre>
100 1 Gergely Szabo
101 1 Gergely Szabo
We assume you already have a running Redmine on the New machine with a virgin database.
102 1 Gergely Szabo
Import the saved database into Redmine, then migrate the database and restart Redmine:
103 1 Gergely Szabo
<pre>
104 1 Gergely Szabo
# cd /root/redmine
105 1 Gergely Szabo
# ./redmine_bak --restore
106 1 Gergely Szabo
# cd /usr/share/redmine
107 1 Gergely Szabo
# rake db:migrate RAILS_ENV=production
108 1 Gergely Szabo
# touch /usr/share/redmine/tmp/restart.txt
109 1 Gergely Szabo
</pre>
110 1 Gergely Szabo
111 1 Gergely Szabo
That's it.
112 1 Gergely Szabo
113 1 Gergely Szabo
We also need to set up regular backups on the new machine as well:
114 1 Gergely Szabo
<pre>
115 1 Gergely Szabo
# crontab -e
116 1 Gergely Szabo
</pre>
117 2 Gergely Szabo
This will open up your favourite editor vi, vi, vi or vi. Or in my case, mcedit. Add a line to create a backup every midnight (Midnight cron jobs with Midnight Commander):
118 1 Gergely Szabo
<pre>
119 1 Gergely Szabo
0 0 * * * /root/redmine/redmine_bak
120 1 Gergely Szabo
</pre>