Project

General

Profile

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

Revision 17 (Etienne Massip, 2012-09-29 16:10) → Revision 22/27 (Toshi MARUYAMA, 2012-09-30 09:27)

h1. The Question 

 How to move your Redmine data to another machine with a newer Redmine version? 

 |*Machine*|*OS*|*Redmine*| 
 |Old|Ubuntu 10.04|0.9.3| 
 |New|Debian Wheezy Testing|1.1.2| 

 h1. The Answer 

 The answer can be found mainly in my well commented Redmine backup-script which can also restore data from backup. 
 The executable (chmod +x) script is called *redmine_bak*, residing in /root/redmine/ along with a git-repo. See details below. 
 <pre> 
 #!/bin/bash 
 usage() { 
 cat <<EOF 
 Usage: redmine_bak [ -r | -h ] [commit msg] 

 When called without parameters, the Redmine database and files are dumped to 
 git-repo in /root/redmine, then the git-repo is pushed to ssh://git@GitServer. 

 When the first parameter is none of the ones below, the same backup procedure 
 is done, but the commit message is the parameter list instead of the date. 

 -r --restore 
 Beforehand, check out the desired version of the Redmine database from git-repo. 
 This command will restore that version into Redmine. 

 -h --help 
 Print this help text. 
 EOF 
 exit $1 
 } 

 DATABASE=`cat /etc/redmine/default/database.yml | sed -rn 's/ *database: (.+)/\1/p'` 
 USERNAME=`cat /etc/redmine/default/database.yml | sed -rn 's/ *username: (.+)/\1/p'` 
 PASSWORD=`cat /etc/redmine/default/database.yml | sed -rn 's/ *password: (.+)/\1/p'` 
 FILES=/var/lib/redmine/default/files 
 cd /root/redmine 

 # Help 
 if [ "$1" = "-h" -o "$1" = "--help" ]; then 
   usage 0 

 # Restore 
 elif [ "$1" = "-r" -o "$1" = "--restore" ]; then 
   /usr/bin/mysql --user=${USERNAME} --password=${PASSWORD} $DATABASE < redmine.sql 
   cp -f [!r][!e][!d][!m][!i][!n][!e]* $FILES 

 # Backup 
 else 
   if [ "$1" ]; then MSG="$@"; else MSG="`date`"; fi 
   /usr/bin/mysqldump --user=${USERNAME} --password=${PASSWORD} --skip-extended-insert $DATABASE > redmine.sql 
   cp -f ${FILES}/* . 
   git add * 
   git commit -m "$MSG" 
   git push --all origin 

 fi 
 </pre> 

 h2. Prepare Git-repos for Backups 

 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: 
 <pre> 
 git@GitServer ~ $ mkdir redmine.git && cd redmine.git && git --bare init 
 </pre> 

 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. 

 Create git-repo on Old Redmine machine: 
 <pre> 
 # cd /root/redmine 
 # git init 
 # git remote add ssh://git@GitServer/~/redmine.git 
 </pre> 

 h2. Backing Up the Old Machine 

 redmine_bak is called every midnight by cron without parameters, which means back-up. 
 It gets database-name, MySQL username and password from the database.yml file. 
 Besides the database, the uploaded files are saved too, see the FILES variable for their location. 

 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). 

 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. 

 Before the migration, the last backup from the old machine is available on GitServer. 

 h2. The New Machine 

 Redmine's email config should be simply copied from Old the New. Somehow. It's located here: 
 <pre>/etc/redmine/default/email.yml</pre> 

 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. 
 <pre> 
 # cd /root 
 # git clone ssh://git@GitServer/~/redmine.git 
 </pre> 

 We assume you already have a running Redmine on the New machine with a virgin database. 
 Import the saved database into Redmine, then migrate the database and restart Redmine: 
 <pre> 
 # cd /root/redmine 
 # ./redmine_bak --restore 
 # cd /usr/share/redmine 
 # rake db:migrate RAILS_ENV=production 
 # touch /usr/share/redmine/tmp/restart.txt 
 </pre> 

 That's it. 

 We also need to set up regular backups on the new machine as well: 
 <pre> 
 # crontab -e 
 </pre> 
 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): 
 <pre> 
 0 0 * * * /root/redmine/redmine_bak 
 </pre>