Wiki source code of Backup/Restore

Version 26.1 by Vincent Massol on 2019/09/17

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 There are several parts that need to be backed up:
8
9 * The database
10 * The XWiki WAR file
11 * The Permanent data (located in the [[XWiki permanent directory>>platform:AdminGuide.Configuration||anchor="HConfiguringDirectories"]])
12 * Log files (optional)
13
14 = Backup your database =
15
16 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/]]).
17
18 * **MySQL**:(((
19 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)
20
21 {{warning}}
22 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.
23 {{/warning}}
24 )))
25 * **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.
26 * **Oracle**: Use ##exp## to export data.
27 * **HSQLDB**: Simply backup the HSQLDB directory. For the XWiki standalone packaging it's the ##data/## directory.
28
29 = Backup the XWiki WAR =
30
31 To be on the safe side you can and should backup the whole XWiki WAR file (that's the file that was expanded and deployed in your Servlet Container when XWiki was installed).
32
33 More specifically the WAR contains mostly static files that are not changed. However it also contains some configuration files for XWiki and you really want to backup those, as explained below.
34
35 == Configuration files ==
36
37 There are several configuration files you'll need to backup:
38
39 * ##WEB-INF/hibernate.cfg.xml## (Hibernate configuration)
40 * ##WEB-INF/xwiki.cfg## (old XWiki configuration file but still used)
41 * ##WEB-INF/xwiki.properties## (new XWiki Configuration file)
42 * ##WEB-INF/classes/logback.xml## (Logging configuration)
43 * ##WEB-INF/observation/*## (Cluster configuration)
44
45 To restore them simply copy them at the same location where you backed them up from.
46
47 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.
48
49 == Custom filesystem Skins ==
50
51 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.
52
53 = Backup Permanent Data =
54
55 XWiki generates the following data:
56
57 * Wiki pages (but they're saved in the database)
58 * Attachments (when they're saved on the filesystem, otherwise they're saved in the database)
59 * Installed extensions
60 * Search index files (Lucene, Solr)
61 * Logs
62 * Mails that failed to be sent
63
64 It's important that you back them up.
65
66 All generated data is located in the [[permanent directory>>platform:AdminGuide.Configuration||anchor="HConfiguringDirectories"]] of XWiki. If you've installed the default standalone version of XWiki 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.
67
68 Backing up this Permanent Directory will backup all files generated by XWiki. Read on below for details.
69
70 == Attachments ==
71
72 Since XWiki 3.x you can choose to [[store attachments on the filesystem>>platform:AdminGuide.Attachments||anchor="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.
73
74 == Extensions ==
75
76 Extensions are also saved on the filesystem in the Permanent Data Directory in an ##extension## subdirectory.
77
78 In addition, the Extension Manager also maintains a ##cache## subdirectory that currently only contains descriptors of core extensions. If you delete it XWiki will simply try to resolve those extensions again at startup but it won't need to resolve much since the WAR embeds complete descriptors for all the JARs (since XWiki 8.4.4). Removing this directory allows cleaning descriptors that were downloaded in previous versions of XWiki.
79
80 == Lucene index files ==
81
82 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.
83
84 == Solr Index Files ==
85
86 Starting with XWiki 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. However XWiki will regenerate this directory automatically if it's not found at startup.
87
88 == Mails ==
89
90 The [[Mail Sender API>>extensions:Extension.Mail Sender API]] (used by several features of XWiki that require sending mails) will serialize email messages in a ##mails## subdirectpry inside the permanent directory before they are sent, so that if XWiki crashes or is stopped, those mails are not lost and can be resent. Once a mail is successfully sent, its associated file is removed from the ##mails## directory.
91
92 = Backup Log files =
93
94 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.
95
96 However this is fully [[configurable>>platform: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.
97
98 = Using the XWiki Export feature =
99
100 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>>platform:AdminGuide.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.
101
102 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:
103
104 * Activity stream data
105 * Statistics data
106 * Feed plugin data (if you use it)
107 * Deleted documents/attachments data
108 * XWiki Instance Id
109 * Mail Statuses
110
111 Moreover this won't save configuration data or other Permanent Data mentioned above such as installed Extensions, Lucene/SOLR index files and Logs.
112
113 In practice the Import/Export feature should be reserved for the following use cases:
114
115 * move data across XWiki instances, including sharing of applications between separate instances
116 * move data to another wiki
117 * convert from one database to another
118
119 = Restore your data =
120
121 * **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.(((
122 * 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)
123 * Delete the existing ##xwiki## database if any: {{code}}mysql -e "DROP DATABASE xwiki;"{{/code}} (better to start fresh)
124 * Recreate the ##xwiki## database: {{code}}mysql -e "CREATE DATABASE IF NOT EXISTS xwiki DEFAULT CHARACTER SET utf8;"{{/code}}.
125 * 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]).
126 * Re-enable constraints checks: {{code}}mysql -e "SET FOREIGN_KEY_CHECKS=1;"{{/code}}
127
128 That is not all, now you need to restore the xwiki user rights (assuming the ##xwiki## user exists in MySQL).
129
130 * Update the xwiki user password from the (previously restored) hibernate configuration file(((
131 {{code language="none"}}
132 pass=$(cat /etc/xwiki/hibernate.cfg.xml | grep passw| head -1 | sed -e 's/^.*<prop.*password">//' | sed -e 's|</property>.*||')
133 mysql -e "SET PASSWORD FOR 'xwiki'@'localhost' = PASSWORD('$pass');"
134 {{/code}}
135 )))
136
137 Finally, restart you container, for example in Linux: {{code}}service tomcat7 restart{{/code}}
138 )))
139 * **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.
140 * **Oracle**: Use ##imp##.
141
142 = Tips in case you are using nginx =
143
144 Assuming you have a nginx configuration with a {{code}}server_name{{/code}} entry that needs updating, the following script might be useful.
145
146 {{code}}
147 #!/bin/bash
148 configfile="/etc/nginx/sites-available/xwiki-as-root.conf"
149 # The following line works for Amazon EC2 instances. Change accordingly to find this host's IP dynamically.
150 thisip=$(curl -s http://instance-data/latest/meta-data/public-ipv4)
151 if [ ! -f $configfile ]; then
152 echo "Missing file: $configfile"
153 exit 1
154 fi
155 echo backing-up $configfile
156 cp $configfile $configfile-old
157
158 echo Setting nginx server_name to $thisip
159 old=$(cat $configfile | grep '^\ *server_name' | awk '{print $2}' | sed -e 's/;//' | grep "$thipip" | head -1)
160 if [ $? -eq 0 ] ; then
161 echo Fixing server_name
162 cat $configfile | sed -e "s/$old/$thisip/" > $configfile-new
163 mv $configfile-new $configfile
164 fi
165
166 echo Done. New config file:
167 cat $configfile
168
169 echo restarting nginx
170 service nginx restart
171 {{/code}}
172
173 = Sample Backup Script =
174
175 {{code}}
176 #!/bin/bash
177
178 #Space Separated List of Databases to Dump
179 #DATABASE="xwiki d1 d3"
180 DATABASE="xwiki"
181 DBUSER=root
182 DBPASS=*****
183
184 #XWIKI data folder
185 DATAFOLDER=/srv/tomcat6/data/
186 #Where is the webapps folder for your tomcat installation
187 # SLES 11 is located /srv/tomcat6/webapps
188 WEBAPPDIR=/srv/tomcat6/webapps
189 #What context (dir) does your application deploy to
190 DEPLOYCONTEXT=ROOT
191
192
193 ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 DEPLOYDIR=${WEBAPPDIR}/${DEPLOYCONTEXT}
195 DATE=$(date '+%Y-%m-%d')
196 mkdir ./${DATE}
197
198 #backup mysql
199 echo "Backing up Mysql"
200 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
201
202 echo "Backing up Data"
203 #Backup Exteral Data Storage
204 /bin/tar -C ${DATAFOLDER}/../ -zcf ./${DATE}/data.tar.gz data
205
206 #Backing Java Keystore
207 /bin/cp /srv/tomcat6/.keystore ./${DATE}/.keystore
208
209 echo "Backing up xwiki configuration"
210 /bin/cp ${DEPLOYDIR}/WEB-INF/hibernate.cfg.xml ./${DATE}/hibernate.cfg.xml
211 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.cfg ./${DATE}/xwiki.cfg
212 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.properties ./${DATE}/xwiki.properties
213
214 #Backup Deploy Context
215 echo "Backing UP deploy Context"
216 /bin/tar -C ${DEPLOYDIR}/../ -zcf ./${DATE}/ROOT.tar.gz ROOT
217
218 echo "DONE"
219 {{/code}}

Get Connected