📄 backup.sgml
字号:
to make a <quote>frozen snapshot</> of the volume containing the database, then copy the whole data directory (not just parts, see above) from the snapshot to a backup device, then release the frozen snapshot. This will work even while the database server is running. However, a backup created in this way saves the database files in a state where the database server was not properly shut down; therefore, when you start the database server on the backed-up data, it will think the server had crashed and replay the WAL log. This is not a problem, just be aware of it (and be sure to include the WAL files in your backup). </para> <para> If your database is spread across multiple file systems, there may not be any way to obtain exactly-simultaneous frozen snapshots of all the volumes. For example, if your data files and WAL log are on different disks, or if tablespaces are on different file systems, it might not be possible to use snapshot backup because the snapshots must be simultaneous. Read your file system documentation very carefully before trusting to the consistent-snapshot technique in such situations. The safest approach is to shut down the database server for long enough to establish all the frozen snapshots. </para> <para> Another option is to use <application>rsync</> to perform a file system backup. This is done by first running <application>rsync</> while the database server is running, then shutting down the database server just long enough to do a second <application>rsync</>. The second <application>rsync</> will be much quicker than the first, because it has relatively little data to transfer, and the end result will be consistent because the server was down. This method allows a file system backup to be performed with minimal downtime. </para> <para> Note that a file system backup will not necessarily be smaller than an SQL dump. On the contrary, it will most likely be larger. (<application>pg_dump</application> does not need to dump the contents of indexes for example, just the commands to recreate them.) </para> </sect1> <sect1 id="backup-online"> <title>On-line backup and point-in-time recovery (PITR)</title> <indexterm zone="backup"> <primary>on-line backup</primary> </indexterm> <indexterm zone="backup"> <primary>point-in-time recovery</primary> </indexterm> <indexterm zone="backup"> <primary>PITR</primary> </indexterm> <para> At all times, <productname>PostgreSQL</> maintains a <firstterm>write ahead log</> (WAL) in the <filename>pg_xlog/</> subdirectory of the cluster's data directory. The log describes every change made to the database's data files. This log exists primarily for crash-safety purposes: if the system crashes, the database can be restored to consistency by <quote>replaying</> the log entries made since the last checkpoint. However, the existence of the log makes it possible to use a third strategy for backing up databases: we can combine a file-system-level backup with backup of the WAL files. If recovery is needed, we restore the backup and then replay from the backed-up WAL files to bring the backup up to current time. This approach is more complex to administer than either of the previous approaches, but it has some significant benefits: <itemizedlist> <listitem> <para> We do not need a perfectly consistent backup as the starting point. Any internal inconsistency in the backup will be corrected by log replay (this is not significantly different from what happens during crash recovery). So we don't need file system snapshot capability, just <application>tar</> or a similar archiving tool. </para> </listitem> <listitem> <para> Since we can string together an indefinitely long sequence of WAL files for replay, continuous backup can be achieved simply by continuing to archive the WAL files. This is particularly valuable for large databases, where it may not be convenient to take a full backup frequently. </para> </listitem> <listitem> <para> There is nothing that says we have to replay the WAL entries all the way to the end. We could stop the replay at any point and have a consistent snapshot of the database as it was at that time. Thus, this technique supports <firstterm>point-in-time recovery</>: it is possible to restore the database to its state at any time since your base backup was taken. </para> </listitem> <listitem> <para> If we continuously feed the series of WAL files to another machine that has been loaded with the same base backup file, we have a <quote>hot standby</> system: at any point we can bring up the second machine and it will have a nearly-current copy of the database. </para> </listitem> </itemizedlist> </para> <para> As with the plain file-system-backup technique, this method can only support restoration of an entire database cluster, not a subset. Also, it requires a lot of archival storage: the base backup may be bulky, and a busy system will generate many megabytes of WAL traffic that have to be archived. Still, it is the preferred backup technique in many situations where high reliability is needed. </para> <para> To recover successfully using an on-line backup, you need a continuous sequence of archived WAL files that extends back at least as far as the start time of your backup. So to get started, you should set up and test your procedure for archiving WAL files <emphasis>before</> you take your first base backup. Accordingly, we first discuss the mechanics of archiving WAL files. </para> <sect2 id="backup-archiving-wal"> <title>Setting up WAL archiving</title> <para> In an abstract sense, a running <productname>PostgreSQL</> system produces an indefinitely long sequence of WAL records. The system physically divides this sequence into WAL <firstterm>segment files</>, which are normally 16MB apiece (although the size can be altered when building <productname>PostgreSQL</>). The segment files are given numeric names that reflect their position in the abstract WAL sequence. When not using WAL archiving, the system normally creates just a few segment files and then <quote>recycles</> them by renaming no-longer-needed segment files to higher segment numbers. It's assumed that a segment file whose contents precede the checkpoint-before-last is no longer of interest and can be recycled. </para> <para> When archiving WAL data, we want to capture the contents of each segment file once it is filled, and save that data somewhere before the segment file is recycled for reuse. Depending on the application and the available hardware, there could be many different ways of <quote>saving the data somewhere</>: we could copy the segment files to an NFS-mounted directory on another machine, write them onto a tape drive (ensuring that you have a way of restoring the file with its original file name), or batch them together and burn them onto CDs, or something else entirely. To provide the database administrator with as much flexibility as possible, <productname>PostgreSQL</> tries not to make any assumptions about how the archiving will be done. Instead, <productname>PostgreSQL</> lets the administrator specify a shell command to be executed to copy a completed segment file to wherever it needs to go. The command could be as simple as a <application>cp</>, or it could invoke a complex shell script — it's all up to you. </para> <para> The shell command to use is specified by the <xref linkend="guc-archive-command"> configuration parameter, which in practice will always be placed in the <filename>postgresql.conf</filename> file. In this string, any <literal>%p</> is replaced by the absolute path of the file to archive, while any <literal>%f</> is replaced by the file name only. Write <literal>%%</> if you need to embed an actual <literal>%</> character in the command. The simplest useful command is something like<programlisting>archive_command = 'cp -i %p /mnt/server/archivedir/%f </dev/null'</programlisting> which will copy archivable WAL segments to the directory <filename>/mnt/server/archivedir</>. (This is an example, not a recommendation, and may not work on all platforms.) </para> <para> The archive command will be executed under the ownership of the same user that the <productname>PostgreSQL</> server is running as. Since the series of WAL files being archived contains effectively everything in your database, you will want to be sure that the archived data is protected from prying eyes; for example, archive into a directory that does not have group or world read access. </para> <para> It is important that the archive command return zero exit status if and only if it succeeded. Upon getting a zero result, <productname>PostgreSQL</> will assume that the WAL segment file has been successfully archived, and will remove or recycle it. However, a nonzero status tells <productname>PostgreSQL</> that the file was not archived; it will try again periodically until it succeeds. </para> <para> The archive command should generally be designed to refuse to overwrite any pre-existing archive file. This is an important safety feature to preserve the integrity of your archive in case of administrator error (such as sending the output of two different servers to the same archive directory). It is advisable to test your proposed archive command to ensure that it indeed does not overwrite an existing file, <emphasis>and that it returns nonzero status in this case</>. We have found that <literal>cp -i</> does this correctly on some platforms but not others. If the chosen command does not itself handle this case correctly, you should add a command to test for pre-existence of the archive file. For example, something like<programlisting>archive_command = 'test ! -f .../%f && cp %p .../%f'</programlisting> works correctly on most Unix variants. </para> <para> While designing your archiving setup, consider what will happen if the archive command fails repeatedly because some aspect requires operator intervention or the archive runs out of space. For example, this could occur if you write to tape without an autochanger; when the tape fills, nothing further can be archived until the tape is swapped. You should ensure that any error condition or request to a human operator is reported appropriately so that the situation can be resolved relatively quickly. The <filename>pg_xlog/</> directory will continue to fill with WAL segment files until the situation is resolved. </para> <para> The speed of the archiving command is not important, so long as it can keep up with the average rate at which your server generates WAL data. Normal operation continues even if the archiving process falls a little behind. If archiving falls significantly behind, this will increase the amount of data that would be lost in the event of a disaster. It will also mean that the <filename>pg_xlog/</> directory will contain large numbers of not-yet-archived segment files, which could eventually exceed available disk space. You are advised to monitor the archiving process to ensure that it is working as you intend. </para> <para> If you are concerned about being able to recover right up to the current instant, you may want to take additional steps to ensure that the current, partially-filled WAL segment is also copied someplace. This is particularly important if your server generates only little WAL traffic (or has slack periods where it does so), since it could take a long time before a WAL segment file is completely filled and ready to archive. One possible way to handle this is to set up a <application>cron</> job that periodically (once a minute, perhaps) identifies the current WAL segment file and saves it someplace safe. Then the combination of the archived WAL segments and the saved current segment will be enough to ensure you can always restore to within a minute of current time. This behavior is not presently built into <productname>PostgreSQL</> because we did not want to complicate the definition of the <xref linkend="guc-archive-command"> by requiring it to keep track of successively archived, but different, copies of the same WAL file. The <xref linkend="guc-archive-command"> is only invoked on completed WAL segments. Except in the case of retrying a failure, it will be called only once for any given file name. </para> <para> In writing your archive command, you should assume that the file names to be archived may be up to 64 characters long and may contain any combination of ASCII letters, digits, and dots. It is not necessary to remember the original full path (<literal>%p</>) but it is necessary to remember the file name (<literal>%f</>). </para> <para> Note that although WAL archiving will allow you to restore any modifications made to the data in your <productname>PostgreSQL</> database it will not restore changes made to configuration files (that is, <filename>postgresql.conf</>, <filename>pg_hba.conf</> and <filename>pg_ident.conf</>), since those are edited manually rather than through SQL operations. You may wish to keep the configuration files in a location that will be backed up by your regular file system backup procedures. See <xref linkend="runtime-config-file-locations"> for how to relocate the configuration files. </para> </sect2> <sect2 id="backup-base-backup"> <title>Making a Base Backup</title> <para> The procedure for making a base backup is relatively simple: <orderedlist> <listitem> <para> Ensure that WAL archiving is enabled and working. </para> </listitem> <listitem> <para> Connect to the database as a superuser, and issue the command<programlisting>SELECT pg_start_backup('label');</programlisting> where <literal>label</> is any string you want to use to uniquely identify this backup operation. (One good practice is to use the full path where you intend to put the backup dump file.) <function>pg_start_backup</> creates a <firstterm>backup label</> file, called <filename>backup_label</>, in the cluster directory with information about your backup. </para> <para> It does not matter which database within the cluster you connect to to issue this command. You can ignore the result returned by the function; but if it reports an error, deal with that before proceeding. </para> </listitem> <listitem> <para> Perform the backup, using any convenient file-system-backup tool such as <application>tar</> or <application>cpio</>. It is neither necessary nor desirable to stop normal operation of the database while you do this.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -