]> git.evergreen-ils.org Git - working/Evergreen.git/blob - docs/admin/backups.adoc
7ddd0c281f2a3365893f47f7b917e4a4e0165923
[working/Evergreen.git] / docs / admin / backups.adoc
1 Backing up your Evergreen System
2 ================================
3
4 Database backups
5 ----------------
6
7 Although it might seem pessimistic, spending some of your limited time preparing for disaster is one of
8 the best investments you can make for the long-term health of your Evergreen system. If one of your
9 servers crashes and burns, you want to be confident that you can get a working system back in place --
10 whether it is your database server that suffers, or an Evergreen application server.
11
12 At a minimum, you need to be able to recover your system's data from your PostgreSQL database server:
13 patron information, circulation transactions, bibliographic records, and the like. If all else fails,
14 you can at least restore that data to a stock Evergreen system to enable your staff and patrons to find
15 and circulate materials while you work on restoring your local customizations such as branding, colors,
16 or additional functionality. This section describes how to back up your data so that you or a colleague
17 can help you recover from various disaster scenarios.
18
19 Creating logical database backups
20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 The simplest method to back up your PostgreSQL data is to use the `pg_dump` utility to create a logical
23 backup of your database. Logical backups have the advantage of taking up minimal space, as the indexes
24 derived from the data are not part of the backup. For example, an Evergreen database with 2.25 million
25 records and 3 years of transactions that takes over 120 GB on disk creates just a 7.0 GB compressed
26 backup file. The drawback to this method is that you can only recover the data at the exact point in time
27 at which the backup began; any updates, additions, or deletions of your data since the backup began will
28 not be captured. In addition, when you restore a logical backup, the database server has to recreate all
29 of the indexes--so it can take several hours to restore a logical backup of that 2.25 million record
30 Evergreen database.
31
32 As the effort and server space required for logical database backups are minimal, your first step towards
33 preparing for disaster should be to automate regular logical database backups. You should also ensure
34 that the backups are stored in a different physical location, so that if a flood or other disaster strikes
35 your primary server room, you will not lose your logical backup at the same time.
36
37 To create a logical dump of your PostgreSQL database:
38
39 . Issue the command to back up your database: `pg_dump -Fc <database-name> > <backup-filename>`. If you
40 are not running the command as the postgres user on the database server itself, you may need to include
41 options such as `-U <user-name>` and `-h <hostname>` to connect to the database server. You can use a
42 newer version of the PostgreSQL to run `pg_dump` against an older version of PostgreSQL if your client
43 and server operating systems differ. The `-Fc` option specifies the "custom" format: a compressed format
44 that gives you a great deal of flexibility at restore time (for example, restoring only one table from
45 the database instead of the entire schema).
46 . If you created the logical backup on the database server itself, copy it to a server located in a
47 different physical location.
48
49 You should establish a routine of nightly logical backups of your database, with older logical backups
50 being automatically deleted after a given interval.
51
52 Restoring from logical database backups
53 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54
55 To increase your confidence in the safety of your data, you should regularly test your ability to
56 restore from a logical backup. Restoring a logical backup that you created using the custom format
57 requires the use of the `pg_restore` tool as follows:
58
59 . On the server on which you plan to restore the logical backup, ensure that you have installed
60 PostgreSQL and the corresponding server package prerequisites. The `Makefile.instal` prerequisite
61 installer than came with your version of Evergreen contains an installation target that should
62 satisfy these requirements. Refer to the installation documentation for more details.
63 . As the `postgres` user, create a new database using the `createdb` command into which you will
64 restore the data. Base the new database on the _template0_ template database to enable the
65 combination of UTF8 encoding and C locale options, and specify the character type and collation
66 type as "C" using the `--lc-ctype` and `--lc-collate` parameters. For example, to create a new
67 database called "testrestore": `createdb --template=template0 --lc-ctype=C --lc-collate=C testrestore`
68 . As the `postgres` user, restore the logical backup into your newly created database using
69 the `pg_restore` command. You can use the `-j` parameter to use more CPU cores at a time to make
70 your recovery operation faster. If your target database is hosted on a different server, you can
71 use the `-U <user-name>`  and `-h <hostname>` options to connect to that server. For example,
72 to restore the logical backup from a file named evergreen_20121212.dump into the "testrestore"
73 database on a system with 2 CPU cores: `pg_restore -j 2 -d testrestore evergreen_20171212.dump`
74
75 Creating physical database backups with support for point-in-time recovery
76 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77
78 While logical database backups require very little space, they also have the disadvantage of
79 taking a great deal of time to restore for anything other than the smallest of Evergreen systems.
80 Physical database backups are little more than a copy of the database file system, meaning that
81 the space required for each physical backup will match the space used by your production database.
82 However, physical backups offer the great advantage of almost instantaneous recovery, because the
83 indexes already exist and simply need to be validated when you begin database recovery. Your
84 backup server should match the configuration of your master server as closely as possible including
85 the version of the operating system and PostgreSQL.
86
87 Like logical backups, physical backups also represent a snapshot of the data at the point in time
88 at which you began the backup. However, if you combine physical backups with write-ahead-log (WAL)
89 segment archiving, you can restore a version of your database that represents any point in time
90 between the time the backup began and the time at which the last WAL segment was archived, a
91 feature referred to as point-in-time recovery (PITR). PITR enables you to undo the damage that an
92 accidentally or deliberately harmful UPDATE or DELETE statement could inflict on your production
93 data, so while the recovery process can be complex, it provides fine-grained insurance for the
94 integrity of your data when you run upgrade scripts against your database, deploy new custom
95 functionality, or make global changes to your data.
96
97 To set up WAL archiving for your production Evergreen database, you need to modify your PostgreSQL
98 configuration (typically located on Debian and Ubuntu servers in
99 `/etc/postgresql/<version>/postgresql.conf`):
100
101 . Change the value of `archive_mode` to on 
102 . Set the value of archive_command to a command that accepts the parameters `%f` (representing the
103 file name of the WAL segment) and %p (representing the complete path name for the WAL segment,
104 including the file name). You should copy the WAL segments to a remote file system that can be read
105 by the same server on which you plan to create your physical backups. For example, if `/data/wal`
106 represents a remote file system to which your database server can write, a possible value of
107 archive_command could be: `test ! -f /data/wal/%f && cp %p /data/wal/%f`, which effectively tests
108 to see if the destination file already exists, and if it does not, copies the WAL segment to that
109 location. This command can be and often is much more complex (for example, using `scp` or `rsync`
110 to transfer the file to the remote destination rather than relying on a network share), but you
111 can start with something simple.
112
113 Once you have modified your PostgreSQL configuration, you need to restart the PostgreSQL server
114 before the configuration changes will take hold:
115 . Stop your OpenSRF services.
116 . Restart your PostgreSQL server.
117 . Start your OpenSRF services and restart your Apache HTTPD server.
118
119 To create a physical backup of your production Evergreen database:
120
121 . From your backup server, issue the
122 `pg_basebackup -x -D <data-destination-directory> -U <user-name> -h <hostname> <database-name>`
123 command to create a physical backup of database <database-name> on your backup server.
124
125 You should establish a process for creating regular physical backups at periodic intervals,
126 bearing in mind that the longer the interval between physical backups, the more WAL segments
127 the backup database will have to replay at recovery time to get back to the most recent changes
128 to the database. For example, to be able to relatively quickly restore the state of your database
129 to any point in time over the past four weeks, you might take physical backups at weekly intervals,
130 keeping the last four physical backups and all of the corresponding WAL segments.
131
132 Creating a replicated database
133 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134
135 If you have a separate server that you can use  to run a replica of your database, consider
136 replicating your database to that server. In the event that your primary database server suffers a
137 hardware failure, having a database replica gives you the ability to fail over to your database
138 replica with very little downtime and little or no data loss. You can also improve the performance of
139 your overall system by directing some read-only operations, such as reporting, to the database replica.
140 In this section, we describe how to replicate your database using PostgreSQL's streaming replication
141 support.
142
143 You need to prepare your master PostgreSQL database server to support streaming replicas with several
144 configuration changes. The PostgreSQL configuration file is typically located on Debian and Ubuntu
145 servers at `/etc/postgresql/<version>/postgresql.conf`. The PostgreSQL host-based authentication
146 (`pg_hba.conf`) configuration file is typically located on Debian and Ubuntu servers at
147 `/etc/postgresql/<version>/pg_hba.conf`. Perform the following steps on your master database server:
148
149 . Turn on streaming replication support. In postgresql.conf on your master database server,
150 change `max_wal_senders` from the default value of 0 to the number of streaming replicas that you need
151 to support. Note that these connections count as physical connections for the sake of the
152 `max_connections` parameter, so you might need to increase that value at the same time.
153 . Enable your streaming replica to endure brief network outages without having to rely on the
154 archived WAL segments to catch up to the master. In `postgresql.conf` on your production database server,
155 change `wal_keep_segments` to a value such as 32 or 64.
156 . Increase the maximum number of log file segments between automatic WAL checkpoints. In `postgresql.conf`
157 on your production database server, change checkpoint_segments from its default of 3 to a value such as
158 16 or 32. This improves the performance of your database at the cost of additional disk space. 
159 . Create a database user for the specific purpose of replication. As the postgres user on the production
160 database server, issue the following commands, where replicant represents the name of the new user:
161 +
162 [source,sql]
163 createuser replicant
164 psql -d <database> ALTER ROLE replicant WITH REPLICATION;
165 +
166 . Enable your replica database to connect to your master database server as a streaming replica. In
167 `pg_hba.conf` on your master database server, add a line to enable the database user replicant to connect
168 to the master database server from IP address 192.168.0.164:
169 +
170 [source,perl]
171 host    replication   replicant       192.168.0.164/32          md5
172 +
173 . To enable the changes to take effect, restart your PostgreSQL database server.
174
175 To avoid downtime, you can prepare your master database server for streaming replication at any maintenance
176 interval; then weeks or months later, when your replica server environment is available, you can begin
177 streaming replication. Once you are ready to set up the streaming replica, perform the following steps on
178 your replica server:
179
180 . Ensure that the version of PostgreSQL on your replica server matches the version running on your production
181 server. A difference in the minor version (for example, 9.1.3 versus 9.1.5) will not prevent streaming
182 replication from working, but an exact match is recommended.
183 . Create a physical backup of the master database server.
184 . Add a `recovery.conf` file to your replica database configuration directory. This file contains the
185 information required to begin recovery once you start the replica database:
186 +
187 [source,perl]
188 # turn on standby mode, disabling writes to the database
189 standby_mode = 'on'
190 # assumes WAL segments are available at network share /data/wal
191 restore_command = 'cp /data/wal/%f %p'
192 # connect to the master database to being streaming replication
193 primary_conninfo = 'host=kochab.cs.uoguelph.ca user=replicant password=<password>
194 +
195 . Start the PostgreSQL database server on your replica server. It should connect to the master. If the
196 physical backup did not take too long and you had a high enough value for `wal_keep_segments` set on your
197 master server, the replica should begin streaming replication. Otherwise, it will replay WAL segments
198 until it catches up enough to begin streaming replication.
199 . Ensure that the streaming replication is working. Check the PostgreSQL logs on your replica server and
200 master server for any errors. Connect to the replica database as a regular database user and check for
201 recent changes that have been made to your master server.
202
203 Congratulations, you now have a streaming replica database that reflects the latest changes to your Evergreen
204 data! Combined with a routine of regular logical and physical database backups and WAL segment archiving
205 stored on a remote server, you have a significant insurance policy for your system's data in the event that
206 disaster does strike.
207