Manual:Backing up a wiki/zh

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

概要
MediaWiki 的重要数据保存在两个地方：
 * 数据库：页面和内容，用户和用户的偏好设置，原数据，搜索索引，等等.
 * 文件系统：软件配置文件，自定外观，扩充功能，图像（包括已删除图像）等等.

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



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


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

資料庫
Most of the critical data in the wiki is stored in the database. If your wiki is currently offline, its database can be backed up by simply copying the database file.

When using the default MySQL or MariaDB 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.

Automysqlbackup
在Debian上檢視此套件：

安装套件：

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

手动备份：

恢复数据库：

其他發行版請見Sourceforge.



在命令行執行mysqldump
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：

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

运行在 Linux/UNIX shell 上的命令范例:

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

Substituting,  ,  , and   as appropriate. All four may be found in your (LSP) file. 可以在 下找到；默认情况下此值为localhost. may be found under,  may be found under , where it is listed after. If  is not specified mysqldump will likely use the default of utf8, or if using an older version of MySQL, latin1. While  may be found under. After running this line from the command line mysqldump will prompt for the server password (which may be found under 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

Some newer versions of MySQL might show an error about tablespaces and PROCESS privilege. MediaWiki does not use tablespaces. The solution is to add the  option to the command:

mysqldump --no-tablespaces -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

Additional options you should consider using with mysqldump for a backup includes the following.

If you do not use --single-transaction, then you should consider using the --lock-tables and --add-locks options.

Due to an unexpected change in MySQL versions 5.7.41 and 8.0.32 in February 2023, the --single-transaction option required the backup user to have RELOAD or FLUSH_TABLES privileges. The issue was fixed in MySQL versions 5.7.42 and 8.0.33. See MySQL Bug 109685 and Ubuntu Bug 2003866 for details.

Remember to backup the additional file system components used by the wiki that might be required during a restore, like images, logo, skins and extensions.

Running mysqldump with Cron
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  lowers the priority of the process.

Use valid values for,  , and. 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.

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

Running mysqldump with Systemd
Systemd unifies service configurations and control. Timers are systemd unit files that control service files or events. Timers can be used as an alternative to cron. An example of systemd unit files and backup script is shown below.

wiki-backup.timer
The following timer runs the wiki-backup service at 5:10 AM every morning.

$ cat /etc/systemd/system/wiki-backup.timer

[Unit] Description=Run the backup service once a day Documentation=...

[Timer] OnCalendar=*-*-* 05:10:00 RandomizedDelaySec=600 Persistent=true

[Install] WantedBy=timers.target

wiki-backup.service
When the wiki-backup timer fires, then the service is invoked. The service runs a script located in.

$ cat /etc/systemd/system/wiki-backup.service

[Unit] Description=Run the backup service once a day Documentation=...

[Service] Type=oneshot User=root ExecStart=/sbin/wiki-backup

wiki-backup script
$ cat /sbin/wiki-backup


 * 1) !/usr/bin/env bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH
 * 1) Systemd adds random paths at times. Take full control of PATH.

wiki_password=...
 * 1) Read the backup password from conf or ini Failed

if MYSQL_PWD="${wiki_password}" \ mysqlcheck my_wiki --auto-repair --user=mwuser 1>/dev/null; then echo "Repair wiki database ok" else echo "Failed to repair wiki database" echo "Continuing anyways" fi
 * 1) Fix the wiki tables just in case. This step produces a lot of noise,
 * 2) so send stdout to /dev/null.

if ! systemctl stop apache2.service ; then echo "Failed to stop Apache service" echo "Continuing anyways" fi
 * 1) Disable the connection from Apache to MySQL for the dump

if mysql --version 2>&1 | grep -q -E 'mysqlspace:+Ver 8\.0\.32'; then echo "Using MySQL --lock-tables --add-locks options" mysql_lock_opt="--lock-tables --add-locks" else echo "Using MySQL --single-transaction option" mysql_lock_opt="--single-transaction" fi
 * 1) Lock option choice due to MySQL change at versions 5.7.41 and 8.0.32 in
 * 2) February 2023. See https://bugs.mysql.com/bug.php?id=109685 and
 * 3) https://bugs.launchpad.net/ubuntu/+source/mysql-5.7/+bug/2003866.

if MYSQL_PWD="${wiki_password}" \ mysqldump --no-tablespaces \ ${mysql_lock_opt} \ --events --triggers --routines \ --add-drop-table --create-options \ --extended-insert \ --default-character-set=utf8 \ -u mwuser -h localhost my_wiki | gzip -q -v9 > /backup/wiki-backup.sql.gz ; then echo "Dump wiki database ok" else echo "Failed to dump wiki database" echo "Continuing anyways" fi

if ! systemctl start apache2.service ; then echo "Failed to start Apache service" echo "Continuing anyways" fi
 * 1) Re-enable connection from Apache to MySQL for the dump

exit 0

表格
Some of the tables dumped have different 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 saving 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
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
You can use the  tool to back up a MediaWiki PostgreSQL database. For example:

pg_dump mywiki > mywikidump.sql

will dump the  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

phpMyAdmin
Turn your wiki to read only by adding  to LocalSettings.php.

Find the wiki database in LocalSettings.php. Here is an example of what this looks like in LocalSettings.php:

  Open the browser to your phpadmin link, login, choose the wiki database.   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.   Click on GO and save the backup file.   Remove  from LocalSettings.php  

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



外部链接

 * Backing up the Database

HeidiSQL (alternative to phpMyAdmin)
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.

<span id="File_system">

文件系统
MediaWiki stores other components of the wiki in the file system.

The most important of these are:


 * uploaded files in the  directory (including deleted files, thumbnails, and rendered math and SVG images, if applicable).
 * uploaded files in the  directory (including deleted files, thumbnails, and rendered math and SVG images, if applicable).

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

For Linux variants, assuming the wiki is stored in

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

Configuration files
is the most important of these, but a wiki might also have things like  or other web server configuration files that should be backed up.

Uploaded files
Files uploaded to the wiki are by default put into the  directory, separated into subdirectories such as. There are also other directories such as  and. These should all be backed up.

The  can be backed up along with everything else, but can optionally be excluded in order to save backup space. This directory stores the derived thumbnails of images and other files; generally multiple thumbnails per wiki file. After restoring from backup, these thumbnails will be recreated as required (although depending on this may need to be a manual process).

<span id="Backup_the_content_of_the_wiki_(XML_dump)">

备份wiki的内容（XML转储）
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).

XML dumps are less likely to cause problems with character encoding, as a means of transferring large amounts of content quickly, and can 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, located in the   directory of your MediaWiki installation. See 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. 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).

'' See for more information. ''

<span id="Without_shell_access_to_the_server">

MediaWiki Scraper
If you have no shell access, use the MediaWiki Client Tools' MediaWiki Scraper dumpgenerator script. It is run from the command-line in a terminal.

The XML dump can include full or only most recent page history. The images dump will contain all file types with associated descriptions. The  and   files will contain information about wiki features such as the installed extensions and skins. User account information won't be preserved.

Full instructions are at the MediaWiki Client Tools' MediaWiki Scraper GitHub repository.

参见 Data dumps.

脚本
Shell script, last updated 2012. Script does not need to be modified for each site to be backed up. Does not (yet) rotate old backups. Usage:. Also provides a script to restore a backup. Shell script, last updated 2013. Shell script, last updated 2015. Shell script, last updated 2016. PHP script, last updated 2017. Can be used with Windows task scheduler. Last updated 2019. Python 2 script.
 * Unofficial backup script by Flominator; creates a backup of all files and the database, with optional backup rotation.
 * Another backup script that: dumps DB, files (just pictures by default, option to include all files in installation), and XML; puts the site into read-only mode; timestamps backups; and reads the charset from LocalSettings.
 * User:Darizotas/MediaWiki Backup Script for Windows - a script for backing up a Windows MediaWiki install. Note: Has no restore feature.
 * Unofficial backup script by User:Duesentrieb.
 * 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.
 * Another unofficial by Lanthanis that: exports the pages of specified namespaces as an XML file; dumps specified database tables; and adds further specified folders and files to a ZIP backup file.
 * 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 dumpgenerator from WikiTeam tools, (Python 2). See some saved wikis.
 * Mediawiki Scraper - 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 dumpgenerator from Mediawiki Client Tools, Python 3 script, last update 2023.

Extensions

 * – Allows users to generate and download database dumps
 * – Allows users to generate and download XML and file/image dumps

<span id="See_also">

参见

 * is a quick and easy way to save all pages on your wiki.
 * — 如果你没有成功备份
 * — 如果你没有成功备份
 * — 如果你没有成功备份
 * — 如果你没有成功备份