Manual:ウィキのバックアップ

This page is a translated version of the page Manual:Backing up a wiki and the translation is 43% complete.

It is important to make regular backups of your wiki (data and files). This page provides an overview of the backup process for a typical MediaWiki wiki; you will probably want to devise your own backup scripts or schedule to suit the size of your wiki and your individual needs.

Help:書き出し は、Wiki 上のすべてのページを保存するための迅速で簡単な方法です。

概要

MediaWiki は、重要なデータを 2 箇所に保持しています:

データベース
ページとその内容、利用者とその個人設定、メタデータ、検索のインデックス情報など。
ファイルシステム
ソフトウェア構成ファイル、カスタム外装、拡張機能、画像 (削除されたものを含む) など。

バックアップを作る前に、ウィキを読み取り専用にする($wgReadOnly 参照)ことを検討してください。これによって出来上がったバックアップが自己矛盾の無いものとなります (一部の拡張機能は読み取り専用にしてもデータを書き込むかもしれません)。

ファイル転送

サーバーからファイルを転送するための方法を選択する必要があります:

  • 非公開ではないデータであれば単にpublish on archive.orgまたはwebサーバーのdumps/ディレクトリの中に入っています。
  • SCP (または WinSCP)、SFTP/FTP などの転送プロトコルが使用できます。
  • ホスティング会社がwebブラウザー上で動作するファイルマネージャーを提供しているかも知れません。

データベース

ほとんどの重要なデータは簡単にバックアップできるデータベース内に保存されています。 ウィキが現在オフラインの場合は、そのデータベースは単純にデータベース ファイルをコピーすることでバックアップできます。

既定の MySQL バックエンドを使用している場合、このデータベースはスクリプトファイルとしてダンプすることができます。このスクリプトファイルを使ってゼロからデータベースを再構築できます。

MySQL

Automysqlbackup

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.
[...]

パッケージをインストール:

# 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

他のディストリビューションについては、Sourceforgeを参照してください。

コマンドラインでmysqldumpを実行する

最も簡便なのは、MySQLに標準で付属するmysqldumpを用いることである。これは、バックアップ対象のデータベース名やテーブル名を指定してダンプファイルを作成することができるコマンドラインアプリケーションである。

まずLocalSettings.phpに次の行を挿入します

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

これは、ダンプが完了するとすぐに削除することができます。

Linux/UNIX シェル上で実行するコマンドの例:

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

hostnameuseridcharsetdbname を適宜変更します。 4つはすべて LocalSettings.php (LSP) ファイルにあります。 hostname may be found under $wgDBserver ; by default it is localhost. userid$wgDBuser の下にありますが、charset$wgDBTableOptions の下にあり、DEFAULT CHARSET= の後に列挙されます。 charset が指定されていない場合、mysqldump は既定の utf8 を使用するか、古いバージョンの MySQL を使用する場合は latin1 を使用します。 dbname$wgDBname にあります。 コマンドラインからこの行を実行すると、mysqldumpはサーバーのパスワードを要求します (LSPの Manual:$wgDBpassword にあります)。

コマンドライン パラメーターの完全な一覧については mysqldump を参照してください。

より小さい出力ファイルの場合、mysqldump の出力を以下のように gzip にパイプすることもできます

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 --no-tablespaces option to the command:

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

同様のmysqldumpコマンドを使用して、代わりに--xmlパラメータを含めることでXML出力を生成することができます。

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

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.

Additional Mysqldump Options
オプション 説明
--default-character-set Specify default character set
--no-tablespaces Do not write CREATE LOGFILE GROUP or CREATE TABLESPACE statements in output
--single-transaction Issue a BEGIN SQL statement before dumping data from server
--triggers Dump triggers for each dumped table
--routines Dump stored routines (procedures and functions) from dumped databases
--events Dump events from dumped databases
--add-drop-table Add DROP DATABASE statement before each CREATE DATABASE statement
--create-options Include MySQL-specific table options in CREATE TABLE statements
--extended-insert Use multiple-row INSERT syntax

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.

cronでのmysqldumpの実行

Cron は、Unix ライクなコンピュータオペレーティング システムにおける時間ベースのジョブ スケジューラです。Cronを使用すると、特定の時刻または日付に定期的にジョブ(コマンドまたはシェル スクリプト)を実行するようにスケジュールすることができます。

crontab から実行できるサンプルコマンドは次のようになります:

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

nice -n 19はプロセスの優先順位を下げます。

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.

  警告: mysqlhotcopy を使用して MediaWiki データベースをバックアップしようとしないでください。MediaWiki で使用されているテーブル形式はこのツールではバックアップできません。

このタスクをCpanel経由でCronに追加する場合は、文字「%」をエスケープする必要があります

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

エスケープしないと、以下のエラーになります:

/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 /sbin.

$ 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

#!/usr/bin/env bash

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

# Read the backup password from conf or ini Failed
wiki_password=...

# Fix the wiki tables just in case. This step produces a lot of noise,
# so send stdout to /dev/null.
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

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

# Lock option choice due to MySQL change at versions 5.7.41 and 8.0.32 in
# February 2023. See https://bugs.mysql.com/bug.php?id=109685 and
# https://bugs.launchpad.net/ubuntu/+source/mysql-5.7/+bug/2003866.
if mysql --version 2>&1 | grep -q -E 'mysql[[:space:]]+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

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

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

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).

トピックについては、メーリングリストのスレッドmysql5バイナリスキーマを参照してください。

Latin-1からUTF-8への変換

このプロセスの詳細については、アップグレードのページの関連する節を参照してください。文字セットの一般的な操作の詳細については、トーク ページを参照してください。

PostgreSQL

pg_dumpツールを使用して、MediaWiki PostgreSQLデータベースをバックアップすることができます。例えば:

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

データベースが現在オフラインなら、データベースファイルをコピーするだけでデータベースをバックアップできます。 そうではない場合は、メンテナンス スクリプト php maintenance/SqliteMaintenance.php --backup-to <backup file name> を使用します。この操作はアトミック (すべての処理が実行されるか全く実行されないかのいずれか) なので、不整合が生じないことが保証されます。 ウィキのデータベースが非常に大きかったり、サーバーの負荷が重かったりしなければ、ウィキを編集しているユーザーは多少のもたつきを感じるだけでしょう。 ウィキを読んでいるだけの利用者は何も感じないでしょう。

phpMyAdmin

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

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

## Database settings
$wgDBtype           = "mysql";
$wgDBserver         = "localhost";
$wgDBname           = "sashtmax_mw19999";
$wgDBuser           = "sashtmax_mw19999";
$wgDBpassword       = "S7[88p]jJJ";
  1. Open the browser to your phpadmin link, login, choose the wiki database.
  2. 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.
  3. Select zipped.
  4. Click on GO and save the backup file.[1]
  5. Remove $wgReadOnly = 'Site Maintenance'; from LocalSettings.php

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

外部リンク

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.

ファイルシステム

データベースに保存するよりもファイルシステムに保存した方が適切であるような要素についてはファイルシステムの中に保存されます。

The most important of these are:

  • LocalSettings.php
  • uploaded files in the images/ directory (including deleted files, thumbnails, and rendered math and SVG images, if applicable).

これらのファイルをバックアップするのに最適な方法は、.tarのようなアーカイブファイル(必要が有れば圧縮して)に纏めることです。WindowsであればWinZipや7-Zipといったアプリケーションが使用できます。

Linux の場合でウィキが /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.

Configuration files

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

Uploaded files

Files uploaded to the wiki are by default put into the images/ directory, separated into subdirectories such as images/8/8f. There are also other directories such as images/archive/ and images/deleted/. These should all be backed up.

The images/thumb/ 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 $wgGenerateThumbnailOnParse this may need to be a manual process).

ウィキのコンテンツのバックアップ (XML ダンプ)

データベース ダンプに加えて XML ダンプを作成するというのもいいアイデアです。XML ダンプはウィキの「コンテンツ」を含みますが (すべてのリビジョンを含むすべてのページ)、利用者アカウントや画像のメタデータ、ログなどのサイトに関連するデータは含まれません。[2]

XMLダンプは文字コード化の問題が発生しにくく、大量のコンテンツを素早く転送する手段として、またサードパーティのツールで簡単に使用できるため、メインのデータベースダンプが使用できなくなった場合の予備として有効です。

XMLダンプを作成するには、MediaWikiのインストールのmaintenanceディレクトリにあるコマンドラインツールdumpBackup.php を使用します。 詳細は Manual:dumpBackup.php を参照してください。

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).

詳細はManual:XMLダンプの取り込み をご覧ください。

サーバーにシェルでアクセスできない場合

MediaWiki Dump Generator

If you have no shell access, use the MediaWiki Client Tools' MediaWiki Dump Generator 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 siteinfo.json and SpecialVersion.html 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 Dump Generator GitHub repository.

Meta:Data dumps も参照してください。

スクリプト

  警告: 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.

Shell script, last updated 2012.

  • 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.

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. Also provides a script to restore a backup restore.sh -a backup/directory/dated_archive.tar.gz -w installation/directory. Shell script, last updated 2013.

Shell script, last updated 2015.

Shell script, last updated 2016.

  • 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.

PHP script, last updated 2017.

  • Another unofficial MediaWiki backup script for Windows 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.

Can be used with Windows task scheduler. Last updated 2019.

  • 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.

Python 2 script.

  • MediaWiki Dump Generator - 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.

拡張機能

  • Extension:DumpsOnDemand – データベースダンプの作成とダウンロードが可能です。
  • Extension:DataDump – XMLおよびファイル/イメージダンプの生成とダウンロードが可能です。

関連項目

脚注

  1. Manual talk:Backing up a wiki#Ubuntu 10.10 - Step by Step Instructions
  2. XML ダンプはデータベースの構造からは独立しており、将来 (そして過去) のバージョンの MediaWiki で取り込めます。