Wiki source code of Backup/Restore

Version 20.1 by markgard on 2014/08/29

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 It's crucial that you define a backup strategy for your wiki so that you don't lose any data in case of a problem with your wiki installation or in case you have issues upgrading your XWiki instance to a newer version.
6
7 = Backup configuration files =
8
9 There are several configuration files you'll need to backup:
10
11 * ##WEB-INF/hibernate.cfg.xml## (Hibernate configuration)
12 * ##WEB-INF/xwiki.cfg## (old XWiki configuration file but still used)
13 * ##WEB-INF/xwiki.properties## (new XWiki Configuration file)
14 * ##WEB-INF/classes/logback.xml## (Logging configuration)
15 * ##WEB-INF/observation/*## (Cluster configuration)
16
17 To restore them simply copy them at the same location where you backed them up from.
18
19 In addition you may want to also backup any extra files you'll have installed in your XWiki installation such as JDBC drivers, extra plugins, etc.
20
21 = Backup Permanent Data =
22
23 XWiki generates the following data:
24
25 * Wiki pages (they're saved in a Database)
26 * Attachments on the filesystem
27 * Installed extensions
28 * Search index files (Lucene, Solr)
29 * Logs
30
31 It's important that you back them up.
32
33 All generated data is located in the Permanent Directory of XWiki. If you've installed the default standalone version of XWiki then then the Permanent Directory is located in the ##data/## directory inside the directory where you've installed XWiki. If you have installed the XWiki WAR, you'll find this directly in the location pointed by the ##environment.permanentDirectory## configuration property in your ##WEB-INF/xwiki.properties## configuration file.
34
35 Backing up this Permanent Directory will backup all files generated by XWiki. Read on below for details.
36
37 == Backup your database ==
38
39 If you've installed XWiki using your own database then you'll need a backup tool for that database to back it up and to restore it. Moreover, if you're backing up a huge amount of data, we advise that you compress the dump files to save disk space (for example with [[7-zip>>http://www.7-zip.org/]]).
40
41 * **MySQL**:(((
42 Backup: {{code}}mysqldump --add-drop-database --databases xwiki > xwiki.sql{{/code}} backs up the xwiki schema (use the option ##~-~-all-databases## instead of ##~-~-databases xwiki## if you're in multiwiki mode)
43
44 {{warning}}
45 It's important that the backup is done with ##~-~-add-drop-database## since when you restore it it's important that tables that might have been created are removed. For example database migrations can create extra tables. If you want to cancel the migration and go back to your clean dump you'll need to have those tables removed as otherwise when you run the migration again later on you might have errors.
46 {{/warning}}
47 )))
48 * **PostgreSQL**: You can use {{code}}pg_dump xwiki > xwiki.sql{{/code}} to backup the PostgreSQL database. In case of multiwiki mode, use the binary ##pg_dumpall## instead.
49 * **Oracle**: Use ##exp## to export data.
50
51 == Backup Attachments ==
52
53 Since XWiki 3.x you can choose to [[store attachments on the filesystem>>AdminGuide.Attachments#HAttachmentStorage]] instead of in the database. If you've configured it this way then you need to backup the Permanent Directory which is where attachments saved on the filesystem are located.
54
55 == Backup custom filesystem Skins ==
56
57 If you have a custom skin that is stored on the filesystem, make sure you backup the corresponding folder located in the ##webapps/xwiki/skins/## directory.
58
59 == Backup Extensions ==
60
61 Extensions are also saved on the filesystem in the Permanent Data Directory in an ##extension## subdirectory.
62
63 == Backup Lucene index files ==
64
65 You could decide to backup Lucene index files. However XWiki will regenerate them automatically if they are not found at startup. Now if you have a lot of wiki pages that can take a lot of time and you may want to back them up. They are also located in the Permanent Data Directory in a ##lucene## subdirectory.
66
67 == Backup Solr Index Files ==
68
69 Starting with XWiki Enterprise 4.3, if you are using Solr as the default search engine you will also have to backup the Solr index which is stored by default in the Permanent Data Directory in a ##solr## subdirectory.
70
71 == Backup Log files ==
72
73 XWiki generates logs in the console by default and these will go where your Servlet container has been defined to save them. For Tomcat for example this usually goes in the ##TOMCAT_HOME/logs/catalina.out## file.
74
75 However this is fully [[configurable>>AdminGuide.Logging]] and you may have configured XWiki to output log files elsewhere. If you want to keep those logs, don't forget to save them. Those logs are not needed by XWiki but you may want to save them to review them later on to diagnose issues that happened for example.
76
77 = Using the XWiki Export feature =
78
79 Since most XWiki data is saved in wiki pages one solution to backup a XWiki instance is to export all its pages using XWiki's [[Import/Export feature>>ImportExport]]. However note that this is quite a resource hungry operation and depending on the size of your wiki you may need a lot of memory. As a consequence the recommended backup strategy is to backup the databases. It's much better to use a specialized backup tool that'll backup the database, perform incremental backups, verify backup integrity, etc.
80
81 In addition you should know that currently the following information is not located in wiki pages and is thus not saved in a wiki export:
82
83 * Activity stream data
84 * Statistics data
85 * Feed plugin data (if you use it)
86 * Deleted documents/attachments data
87
88 Moreover this won't save configuration data or other Permanent Data mentioned above such as installed Extensions, Lucene index files and Logs.
89
90 In practice the Import/Export feature should be reserved for the following use cases:
91
92 * move data across XWiki instances, including sharing of applications between separate instances
93 * move data to another wiki
94 * convert from one database to another
95
96 = Restore your data =
97
98 * **MySQL**: Assuming you have your ##xwiki.sql## file handy (obtained running ##mysqldump## as explained above), run the following from the command line (or a script). Examples below are for the Linux shell.(((
99 * Disable constraints check first: {{code}}mysql -e "SET FOREIGN_KEY_CHECKS=0;"{{/code}} (restoring may fail with some cryptic error code, leaving the database in an inconsistent state, if you omit this step)
100 * Delete the existing ##xwiki## database if any: {{code}}mysql -e "DROP DATABASE xwiki;"{{/code}} (better to start fresh)
101 * Recreate the ##xwiki## database: {{code}}mysql -e "CREATE DATABASE IF NOT EXISTS xwiki DEFAULT CHARACTER SET utf8;"{{/code}}.
102 * Restore data from the dump file: {{code}}mysql xwiki --user=root -p < xwiki.sql{{/code}}. Alternatively, you can omit the ##-p## by adding a section [mysqldump] in your ##.my.cnf## file (with a user and password line, same as for [mysql]).
103 * Re-enable constraints checks: {{code}}mysql -e "SET FOREIGN_KEY_CHECKS=1;"{{/code}}
104
105 That is not all, now you need to restore the xwiki user rights (assuming the ##xwiki## user exists in MySQL).
106
107 * Update the xwiki user password from the (previously restored) hibernate configuration file:(((
108 {{code language="none"}}
109 pass=$(cat /etc/xwiki/hibernate.cfg.xml | grep passw| head -1 | sed -e 's/^.*<prop.*password">//' | sed -e 's|</property>.*||')
110 mysql -e "SET PASSWORD FOR 'xwiki'@'localhost' = PASSWORD('$pass');"
111 {{/code}}
112 )))
113
114 Finally, restart you container, for example in Linux: {{code}}service tomcat7 restart{{/code}}
115 )))
116 * **PostgreSQL**: {{code}}psql -d xwiki -f xwiki.sql{{/code}} to restore your previously saved data. In multiwiki mode, use {{code}}psql -d postgres -f xwiki.sql{{/code}} instead.
117 * **Oracle**: Use ##imp##.
118
119 = Tips in case you are using nginx =
120
121 Assuming you have a nginx configuration with a {{code}}server_name{{/code}} entry that needs updating, the following script might be useful.
122
123 {{code}}
124 #!/bin/bash
125 configfile="/etc/nginx/sites-available/xwiki-as-root.conf"
126 # The following line works for Amazon EC2 instances. Change accordingly to find this host's IP dynamically.
127 thisip=$(curl -s http://instance-data/latest/meta-data/public-ipv4)
128 if [ ! -f $configfile ]; then
129 echo "Missing file: $configfile"
130 exit 1
131 fi
132 echo backing-up $configfile
133 cp $configfile $configfile-old
134
135 echo Setting nginx server_name to $thisip
136 old=$(cat $configfile | grep '^\ *server_name' | awk '{print $2}' | sed -e 's/;//' | grep "$thipip" | head -1)
137 if [ $? -eq 0 ] ; then
138 echo Fixing server_name
139 cat $configfile | sed -e "s/$old/$thisip/" > $configfile-new
140 mv $configfile-new $configfile
141 fi
142
143 echo Done. New config file:
144 cat $configfile
145
146 echo restarting nginx
147 service nginx restart
148 {{/code}}
149
150 = Sample Backup Script =
151 {{code}}
152 #!/bin/bash
153
154 #Space Separated List of Databases to Dump
155 #DATABASE="xwiki d1 d3"
156 DATABASE="xwiki"
157 DBUSER=root
158 DBPASS=*****
159
160 #XWIKI data folder
161 DATAFOLDER=/srv/tomcat6/data/
162 #Where is the webapps folder for your tomcat installation
163 # SLES 11 is located /srv/tomcat6/webapps
164 WEBAPPDIR=/srv/tomcat6/webapps
165 #What context (dir) does your application deploy to
166 DEPLOYCONTEXT=ROOT
167
168
169 ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170 DEPLOYDIR=${WEBAPPDIR}/${DEPLOYCONTEXT}
171 DATE=$(date '+%Y-%m-%d')
172 mkdir ./${DATE}
173
174 #backup mysql
175 echo "Backing up Mysql"
176 mysqldump -h 127.0.0.1 -u ${DBUSER} --password=${DBPASS} --max_allowed_packet=512m --add-drop-database --databases ${DATABASE} | /bin/gzip > ./${DATE}/${DATABASE}.sql.gz
177
178 echo "Backing up Data"
179 #Backup Exteral Data Storage
180 /bin/tar -C ${DATAFOLDER}/../ -zcf ./${DATE}/data.tar.gz data
181
182 #Backing Java Keystore
183 /bin/cp /srv/tomcat6/.keystore ./${DATE}/.keystore
184
185 echo "Backing up xwiki configuration"
186 /bin/cp ${DEPLOYDIR}/WEB-INF/hibernate.cfg.xml ./${DATE}/hibernate.cfg.xml
187 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.cfg ./${DATE}/xwiki.cfg
188 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.properties ./${DATE}/xwiki.properties
189
190 #Backup Deploy Context
191 echo "Backing UP deploy Context"
192 /bin/tar -C ${DEPLOYDIR}/../ -zcf ./${DATE}/ROOT.tar.gz ROOT
193
194 echo "DONE"
195 {{/code}}

Get Connected