手册:备份一个wiki

From MediaWiki.org
Jump to: navigation, search
This page is a translated version of the page Manual:Backing up a wiki and the translation is 30% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎magyar • ‎Bahasa Indonesia • ‎italiano • ‎日本語 • ‎한국어 • ‎polski • ‎português • ‎português do Brasil • ‎русский • ‎සිංහල • ‎தமிழ் • ‎中文

经常备份您维基的数据非常重要。本页面提供一个典型MediaWiki维基的备份步骤概要。您也可以自己设计备份脚本或计划来适应您维基的大小和特殊需求。

概要[edit]

MediaWiki 的重要数据保存在两个地方:

数据库:页面和内容,用户和用户的偏好设置,原数据,搜索索引,等等。
文件系统:软件配置文件,自定外观,扩充功能,图像(包括已删除图像)等等。

请考虑在备份之前先把维基设置为唯读 - 请参見$wgReadOnly。这可以保证备份的所有部分是一致的(不过已安装的扩充功能仍然可以写入数据)。

文件传输[edit]

你需要选择一个方法来传输服务器上的文件:

  • 您可以把非私人数据 在 archive.org 上发布 及/或 存放在您的服务器的dumps/目录中。
  • SCP (或 WinSCP), SFTP/FTP 或任何其他 传输协议 ,衹要是您慣用或可用的。
  • 网页寄存公司有可能通过网络浏览器提供文件管理器界面; 请咨询您的供应商。

数据库[edit]

Most of the critical data in the wiki is stored in the database, which is typically straightforward to back up. When using the default MySQL backend, the database can be dumped into a script file which can be used later to recreate the database and all the data in it from scratch.

MySQL[edit]

Automysqlbackup[edit]

See the package on Debian:

$ apt show automysqlbackup
[...]
Description: automysqlbackup creates backup every day, week and month for all of your MySQL database, to a configured folder. There's nothing to do but to install this package, and you'll rest assured that you have a way to go back in the history of your database.
[...]

Install the package:

# apt install automysqlbackup

你所有的数据库都将被保存在/var/lib/automysqlbackup/:

$ find /var/lib/automysqlbackup/
/var/lib/automysqlbackup/
/var/lib/automysqlbackup/weekly
/var/lib/automysqlbackup/weekly/my_wiki
/var/lib/automysqlbackup/weekly/my_wiki/my_wiki_week.18.2016-05-07_15h32m.sql.gz
/var/lib/automysqlbackup/monthly
/var/lib/automysqlbackup/daily
/var/lib/automysqlbackup/daily/my_wiki

手动备份:

# automysqlbackup

恢复数据库:

gunzip < /var/lib/automysqlbackup/weekly/my_wiki/my_wiki_week.18.2016-05-07_15h32m.sql.gz|mysql -uUSER -pPASSWORD my_wiki

For other distributions, see on Sourceforge.

Mysqldump from the command line[edit]

The most convenient way to create a dump file of the database you want to back up is to use the standard MySQL dump tool mysqldump from the command line. Be sure to get the parameters right or you may have difficulty restoring the database. Depending on database size, mysqldump could take a considerable amount of time.

首先,将以下文本加入至LocalSettings.php:

$wgReadOnly = 'Dumping Database, Access will be restored shortly';

在转储完成后就可以移除上述文本。

Example of the command to run on the Linux/UNIX shell:

mysqldump -h hostname -u userid -p --default-character-set=whatever dbname > backup.sql

Substituting hostname, userid, whatever, and dbname as appropriate. All four may be found in your LocalSettings.php (LSP) file. hostname may be found under $wgDBserver; by default it is localhost. userid may be found under $wgDBuser, whatever may be found under $wgDBTableOptions, where it is listed after DEFAULT CHARSET=. If whatever is not specified mysqldump will likely use the default of utf8, or if using an older version of MySQL, latin1. While dbname may be found under $wgDBname. After running this line from the command line mysqldump will prompt for the server password (which may be found under Manual:$wgDBpassword in LSP).

See mysqldump for a full list of command line parameters.

The output from mysqldump can instead be piped to gzip, for a smaller output file, as follows

mysqldump -h hostname -u userid -p dbname | gzip > backup.sql.gz

A similar mysqldump command can be used to produce XML output instead, by including the --xml parameter.

mysqldump -h hostname -u userid -p --xml dbname > backup.xml

and to compress the file with a pipe to gzip

mysqldump -h hostname -u userid -p --xml dbname | gzip > backup.xml.gz

Remember to also backup the file system components of the wiki that might be required, e.g., images, logo, and extensions.

Running mysqldump with Cron[edit]

Cron is the time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates.

A sample command that you may run from a crontab may look like this:

nice -n 19 mysqldump -u $USER --password=$PASSWORD $DATABASE -c | nice -n 19 gzip -9 > ~/backup/wiki-$DATABASE-$(date '+%Y%m%d').sql.gz

The nice -n 19 lowers the priority of the process.

Use valid values for $USER, $PASSWORD, and $DATABASE. This will write a backup file with the weekday in the filename so you would have a rolling set of backups. If you want to save the files and extensions as well, you might want to use this one.

警告 警告: Do not attempt to back up your Mediawiki database using mysqlhotcopy. The table format used by Mediawiki cannot be backed up with this tool, and it will fail silently!

If you want to add this task in Cron through Cpanel then you must escape the character "%"

/usr/bin/mysqldump -u $USER --password=$PASSWORD $DATABASE -c | /bin/gzip > ~/backup/wiki-$DATABASE-$(date '+\%Y\%m\%d').sql.gz

or you will get an error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file

表格[edit]

Under close examination one finds that some of the tables dumped have various degrees of temporariness. So to save disk space (beyond just gziping), although those tables need to be present in a proper dump, their data does not. However, under certain circumstances the disadvantage of having to rebuild all this data may outweigh the saving in disk space (for example, on a large wiki where restoration speed is paramount).

See mailing list thread mysql5 binary schema about the topic.

Latin-1 to UTF-8 conversion[edit]

See the relevant section of the upgrading page for information about this process. Also see the talk page for more information about working with character sets in general.

PostgreSQL[edit]

You can use the pg_dump tool to back up a MediaWiki PostgreSQL database. For example:

pg_dump mywiki > mywikidump.sql

will dump the mywiki database to mywikidump.sql.

要恢复转储:

psql mywiki -f mywikidump.sql

You may also want to dump the global information, e.g. the database users:

pg_dumpall --globals > postgres_globals.sql

SQLite[edit]

If your wiki is currently offline, its database can be backed up by simply copying the database file. Otherwise, you should use a maintenance script: php maintenance/sqlite.php --backup-to <backup file name>, which will make sure that operation is atomic and there are no inconsistencies. If your database is not really huge and server is not under heavy load, users editing the wiki will notice nothing but a short lag. Users who are just reading will not notice anything in any case.

phpMyAdmin[edit]

Turn your wiki to read only by adding $wgReadOnly = 'Site Maintenance'; to LocalSettings.php.

Open the browser to your phpadmin link, login, choose the wiki database. (Check LocalSettings.php if you're not sure). Select Export. Make sure all items under Export are highlighted, and make sure Structure is highlighted (it's important to maintain the table structure). Optionally check Add DROP TABLE to delete existing references when importing. Make sure Data is checked. Select zipped. Then click on GO and save the backup file.[1]

Remove $wgReadOnly = 'Site Maintenance'; from LocalSettings.php

Remember to also backup the file system components of the wiki that might be required, eg. images, logo, and extensions.

外部链接[edit]

HeidiSQL[edit]

HeidiSQL is similar to phpMyAdmin, but without any restrictions of phpMyAdmin's free version. HeidiSQL requires a direct database connection, where some hosts may only offer web interfaces (phpMyAdmin) to firewalled databases.

文件系统[edit]

MediaWiki stores other components of the wiki in the file system where this is more appropriate than insertion into the database, for example, site configuration files (LocalSettings.php, AdminSettings.php (finally removed in 1.23)), image files (including deleted images, thumbnails and rendered math and SVG images, if applicable), skin customisations, extension files, etc.

The best method to back these up is to place them into an archive file, such as a .tar file, which can then be compressed if desired. On Windows, applications such as WinZip or 7-zip can be used if preferred.

For Linux variants, assuming the wiki is stored in /srv/www/htdocs/wiki

  tar zcvhf wikidata.tgz /srv/www/htdocs/wiki

It should be possible to backup the entire "wiki" folder in "htdocs" if using XAMPP.

备份wiki的内容(XML转储)[edit]

It is also a good idea to create an XML dump in addition to the database dump. XML dumps contain the content of the wiki (wiki pages with all their revisions), without the site-related data (they do not contain user accounts, image metadata, logs, etc).[2]

XML dumps are less likely to cause problems with character encoding, as a means of transfering large amounts of content quickly, and are easily be used by third party tools, which makes XML dumps a good fallback should your main database dump become unusable.

To create an XML dump, use the command-line tool dumpBackup.php, located in the maintenance directory of your MediaWiki installation. See Manual:dumpBackup.php for more details.

You can also create an XML dump for a specific set of pages online, using Special:Export, although attempting to dump large quantities of pages through this interface will usually time out.

To import an XML dump into a wiki, use the command-line tool importDump.php. For a small set of pages, you can also use the Special:Import page via your browser (by default, this is restricted to the sysop group). As an alternative to dumpBackup.php and importDump.php, you can use MWDumper, which is faster, but requires a Java runtime environment.

See Manual:Importing XML dumps for more information.

在没有对服务器shell访问权的情况下[edit]

If you have no shell access, then use the WikiTeam Python script dumpgenerator.py from a DOS, Unix or Linux command-line. 需要Python v2(v3尚不能工作)。

To get an XML, with edit histories, dump and a dump of all images plus their descriptions. Without extensions and LocalSettings.php configs.

python dumpgenerator.py --api=http://www.sdiy.info/w/api.php --xml --images

Full instructions are at the WikiTeam tutorial.

See also Meta:Data dumps.

脚本[edit]

警告 警告: Use these at your own risk. Check your wiki's LocalSettings.php for the correct character set to use, as you may need to edit the script to suit.
  • Unofficial backup script by User:Duesentrieb.
  • Unofficial backup script by Flominator; creates a backup of all files and the database, with optional backup rotation.
  • User:Darizotas/MediaWiki Backup Script for Windows - a script for backing up a Windows MediaWiki install. Note: Has no restore feature.
  • Unofficial web-based backup script, mw_tools, by Wanglong (allwiki.com); you can use it to back up your database, or use the backup files to recover the database, the operation is very easy.
  • WikiTeam tools - if you do not have server access (e.g. your wiki is in a free wikifarm), you can generate an XML dump and an image dump using WikiTeam tools (see some saved wikis)
  • FullsitebackupFullsitebackup
  • Another backup script that: dumps DB, files, and XML; puts the site into read-only mode; timestamps backups; and reads the charset from LocalSettings. Script does not need to be modified for each site to be backed up. Does not (yet) rotate old backups. Usage: backup.sh -d backup/directory -w installation/directory
  • Script to make periodical backups mw_backup. This script will make daily, weekly and monthly backups of your database and images directory when run as a daily cron job.

参见[edit]

参考资料[edit]

  1. Manual_talk:Backing_up_a_wiki#Ubuntu_10.10_-_Step_by_Step_Instructions
  2. XML dumps are independent of the database structure, and can be imported into future (and even past) versions of MediaWiki.