Wiki source code of Backup/Restore

Version 21.4 by Vincent Massol on 2017/04/10

Hide last authors
Thomas Steinbach 5.3 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Ludovic Dubost 5.1 4
Vincent Massol 10.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.
Vincent Massol 1.1 6
Silvia Macovei 5.2 7 = Backup configuration files =
Vincent Massol 1.1 8
Vincent Massol 8.3 9 There are several configuration files you'll need to backup:
Ludovic Dubost 4.1 10
Vincent Massol 8.3 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)
Vincent Massol 1.1 16
Manuel Smeria 13.2 17 To restore them simply copy them at the same location where you backed them up from.
Vincent Massol 1.1 18
Vincent Massol 10.3 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.
Guillaume Fenollar 8.1 20
Vincent Massol 10.1 21 = Backup Permanent Data =
Vincent Massol 1.1 22
Vincent Massol 10.1 23 XWiki generates the following data:
Marc Lijour 17.1 24
Vincent Massol 10.1 25 * Wiki pages (they're saved in a Database)
Vincent Massol 21.4 26 * Attachments (when they're saved on the filesystem)
Vincent Massol 10.1 27 * Installed extensions
Vincent Massol 18.1 28 * Search index files (Lucene, Solr)
Vincent Massol 10.1 29 * Logs
Vincent Massol 21.4 30 * Mails that failed to be sent
Vincent Massol 1.1 31
Vincent Massol 10.1 32 It's important that you back them up.
Vincent Massol 1.1 33
Vincent Massol 21.4 34 All generated data is located in the Permanent Directory 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.
Vincent Massol 10.1 35
Vincent Massol 18.1 36 Backing up this Permanent Directory will backup all files generated by XWiki. Read on below for details.
37
Vincent Massol 10.1 38 == Backup your database ==
39
Manuel Smeria 13.2 40 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/]]).
Vincent Massol 1.1 41
Vincent Massol 13.1 42 * **MySQL**:(((
Manuel Smeria 13.2 43 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)
Vincent Massol 13.1 44
45 {{warning}}
Manuel Smeria 13.2 46 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.
Vincent Massol 13.1 47 {{/warning}}
48 )))
Clemens Robbenhaar 19.1 49 * **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.
Vincent Massol 16.1 50 * **Oracle**: Use ##exp## to export data.
Vincent Massol 1.3 51
Vincent Massol 10.1 52 == Backup Attachments ==
Guillaume Fenollar 8.1 53
Vincent Massol 18.1 54 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.
Guillaume Fenollar 8.1 55
Vincent Massol 18.1 56 == Backup custom filesystem Skins ==
Raluca Moisa 17.3 57
58 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.
59
Vincent Massol 10.1 60 == Backup Extensions ==
Guillaume Fenollar 8.1 61
Vincent Massol 18.1 62 Extensions are also saved on the filesystem in the Permanent Data Directory in an ##extension## subdirectory.
Vincent Massol 10.1 63
64 == Backup Lucene index files ==
65
Vincent Massol 18.1 66 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.
Vincent Massol 10.1 67
Vincent Massol 18.1 68 == Backup Solr Index Files ==
Raluca Moisa 17.2 69
Vincent Massol 21.4 70 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. However XWiki will regenerate this directory automatically if it's not found at startup.
Raluca Moisa 17.2 71
Vincent Massol 10.1 72 == Backup Log files ==
73
Manuel Smeria 13.2 74 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.
Vincent Massol 10.1 75
76 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.
77
Vincent Massol 21.4 78 == Backup Mails ==
79
80 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.
81
Vincent Massol 10.5 82 = Using the XWiki Export feature =
Vincent Massol 1.1 83
Vincent Massol 10.1 84 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.
Vincent Massol 1.1 85
Vincent Massol 10.1 86 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:
Marc Lijour 17.1 87
Vincent Massol 10.1 88 * Activity stream data
89 * Statistics data
90 * Feed plugin data (if you use it)
91 * Deleted documents/attachments data
Vincent Massol 21.1 92 * XWiki Instance Id
93 * Mail Statuses
Ludovic Dubost 4.1 94
Vincent Massol 21.2 95 Moreover this won't save configuration data or other Permanent Data mentioned above such as installed Extensions, Lucene/SOLR index files and Logs.
Vincent Massol 10.1 96
Manuel Smeria 13.2 97 In practice the Import/Export feature should be reserved for the following use cases:
Marc Lijour 17.1 98
Vincent Massol 1.5 99 * move data across XWiki instances, including sharing of applications between separate instances
100 * move data to another wiki
Vincent Massol 14.1 101 * convert from one database to another
Marc Lijour 15.1 102
103 = Restore your data =
104
Vincent Massol 16.1 105 * **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.(((
106 * 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)
Marc Lijour 17.1 107 * Delete the existing ##xwiki## database if any: {{code}}mysql -e "DROP DATABASE xwiki;"{{/code}} (better to start fresh)
108 * Recreate the ##xwiki## database: {{code}}mysql -e "CREATE DATABASE IF NOT EXISTS xwiki DEFAULT CHARACTER SET utf8;"{{/code}}.
Vincent Massol 16.1 109 * 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]).
110 * Re-enable constraints checks: {{code}}mysql -e "SET FOREIGN_KEY_CHECKS=1;"{{/code}}
Marc Lijour 15.1 111
Vincent Massol 16.1 112 That is not all, now you need to restore the xwiki user rights (assuming the ##xwiki## user exists in MySQL).
Marc Lijour 17.1 113
Vincent Massol 16.1 114 * Update the xwiki user password from the (previously restored) hibernate configuration file:(((
Vincent Massol 16.2 115 {{code language="none"}}
116 pass=$(cat /etc/xwiki/hibernate.cfg.xml | grep passw| head -1 | sed -e 's/^.*<prop.*password">//' | sed -e 's|</property>.*||')
Marc Lijour 15.1 117 mysql -e "SET PASSWORD FOR 'xwiki'@'localhost' = PASSWORD('$pass');"
118 {{/code}}
Vincent Massol 16.1 119 )))
120
Vincent Massol 18.2 121 Finally, restart you container, for example in Linux: {{code}}service tomcat7 restart{{/code}}
Marc Lijour 15.1 122 )))
Clemens Robbenhaar 19.1 123 * **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.
Vincent Massol 16.1 124 * **Oracle**: Use ##imp##.
Marc Lijour 15.1 125
Vincent Massol 16.2 126 = Tips in case you are using nginx =
127
Marc Lijour 15.1 128 Assuming you have a nginx configuration with a {{code}}server_name{{/code}} entry that needs updating, the following script might be useful.
129
130 {{code}}
131 #!/bin/bash
132 configfile="/etc/nginx/sites-available/xwiki-as-root.conf"
133 # The following line works for Amazon EC2 instances. Change accordingly to find this host's IP dynamically.
134 thisip=$(curl -s http://instance-data/latest/meta-data/public-ipv4)
135 if [ ! -f $configfile ]; then
136 echo "Missing file: $configfile"
137 exit 1
138 fi
139 echo backing-up $configfile
140 cp $configfile $configfile-old
141
142 echo Setting nginx server_name to $thisip
143 old=$(cat $configfile | grep '^\ *server_name' | awk '{print $2}' | sed -e 's/;//' | grep "$thipip" | head -1)
144 if [ $? -eq 0 ] ; then
145 echo Fixing server_name
146 cat $configfile | sed -e "s/$old/$thisip/" > $configfile-new
147 mv $configfile-new $configfile
148 fi
149
150 echo Done. New config file:
151 cat $configfile
152
153 echo restarting nginx
154 service nginx restart
155 {{/code}}
markgard 20.1 156
157 = Sample Backup Script =
158 {{code}}
159 #!/bin/bash
160
161 #Space Separated List of Databases to Dump
162 #DATABASE="xwiki d1 d3"
163 DATABASE="xwiki"
164 DBUSER=root
165 DBPASS=*****
166
167 #XWIKI data folder
168 DATAFOLDER=/srv/tomcat6/data/
169 #Where is the webapps folder for your tomcat installation
170 # SLES 11 is located /srv/tomcat6/webapps
171 WEBAPPDIR=/srv/tomcat6/webapps
172 #What context (dir) does your application deploy to
173 DEPLOYCONTEXT=ROOT
174
175
176 ###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177 DEPLOYDIR=${WEBAPPDIR}/${DEPLOYCONTEXT}
178 DATE=$(date '+%Y-%m-%d')
179 mkdir ./${DATE}
180
181 #backup mysql
182 echo "Backing up Mysql"
183 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
184
185 echo "Backing up Data"
186 #Backup Exteral Data Storage
187 /bin/tar -C ${DATAFOLDER}/../ -zcf ./${DATE}/data.tar.gz data
188
189 #Backing Java Keystore
190 /bin/cp /srv/tomcat6/.keystore ./${DATE}/.keystore
191
192 echo "Backing up xwiki configuration"
193 /bin/cp ${DEPLOYDIR}/WEB-INF/hibernate.cfg.xml ./${DATE}/hibernate.cfg.xml
194 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.cfg ./${DATE}/xwiki.cfg
195 /bin/cp ${DEPLOYDIR}/WEB-INF/xwiki.properties ./${DATE}/xwiki.properties
196
197 #Backup Deploy Context
198 echo "Backing UP deploy Context"
199 /bin/tar -C ${DEPLOYDIR}/../ -zcf ./${DATE}/ROOT.tar.gz ROOT
200
201 echo "DONE"
202 {{/code}}

Get Connected