-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpg_backup.sh
564 lines (504 loc) · 16.5 KB
/
pg_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/bin/sh
#
# Postgresql backup script
#
# Author
# |
# +-- speedboy (speedboy_420 at hotmail dot com)
# +-- spiderr (spiderr at bitweaver dot org)
# +-- Sebastien Maillard (sebastien dot maillard at elico-corp dot com)
#
# Last modified
# |
# +-- 16-10-2001
# +-- 16-12-2007
# +-- 02-02-2016
#
# Version
# |
# +-- 1.1.3
#
# Description
# |
# +-- A bourne shell script that automates the backup, vacuum and
# analyze of databases running on a postgresql server.
#
# Tested on
# |
# +-- Postgresql
# | |
# | +-- 9.4
# | +-- 8.1.9
# | +-- 7.1.3
# | +-- 7.1.2
# | +-- 7.1
# | +-- 7.0
# |
# +-- Operating systems
# | |
# | +-- CentOS 5 (RHEL 5)
# | +-- Linux Redhat 6.2
# | +-- Linux Mandrake 7.2
# | +-- FreeBSD 4.3
# | +-- Cygwin (omit the column ":" because NT doesn't quite digest it)
# |
# +-- Shells:
# |
# +-- sh
# +-- bash
# +-- ksh
#
# Requirements
# |
# +-- grep, awk, sed, echo, bzip2, chmod, touch, mkdir, date, psql,
# test, expr, dirname, find, tail, du, pg_dump, vacuum_db
#
# Installation
# |
# +-- Set the path and shell you wish to use for this script on the
# | first line of this file. Keep in mind this script has only been
# | tested on the shells above in the "Tested on" section.
# |
# +-- Set the configuration variables below in the configuration
# | section to appropriate values.
# |
# +-- Remove the line at the end of the configuration section so that
# | the script will run.
# |
# +-- Now save the script and perform the following command:
# |
# | chmod +x ./pg_backup.sh
# |
# +-- Now run the configuration test:
# |
# | ./pg_backup.sh configtest
# |
# | This will test the configuration details.
# |
# +-- Once you have done that add similiar entries given as examples
# below to the crontab (`crontab -e`) without the the _first_ #
# characters.
#
# # Run a backup of the remote database host 'foodb', likely on a private network
# 00 03 * * * export PGHOST=foodb; export PGBACKUPDIR=/bak/backups ; nice /server/postgres/pg_backup.sh b 2 >> /var/log/pgsql/pg_backup.log 2>&1
# # Run a backup of the local database host 'foodb' to a custom backup directory
# 00 04 * * * export PGBACKUPDIR=/bak/backups ; nice /usr/bin/pg_backup b 2 >> /var/log/pgsql/pg_backup.log 2>&1
#
# Restoration
# |
# +-- Restoration can be performed by using psql or pg_restore.
# | Here are two examples:
# |
# | a) If the backup is plain text:
# |
# | Firstly bunzip2 your backup file (if it was bzip2ped).
# |
# | nunzip2 backup_file.bz2
# | psql -U postgres database < backup_file
# |
# | b) If the backup is not plain text:
# |
# | Firstly bunzip2 your backup file (if it was bzip2ed).
# |
# | bunzip2 backup_file
# | pg_restore -d database -F {c|t} backup_file
# |
# | Note: {c|t} is the format the database was backed up as.
# |
# | pg_restore -d database -F t backup_file_tar$PARAM_PGHOST
# |
# +-- Refer to the following url for more pg_restore help:
#
# http://www.postgresql.org/idocs/index.php?app-pgrestore.html
#
# To Do List
# 1. db_connectivity() is BROKEN, if the script can not create a
# connection to postmaster it should die and write to the logfile
# that it could not connect.
# 2. make configtest check for every required binary
#
########################################################################
# Start configuration #
########################################################################
#
##################
# Authentication #
##################
#
# Postgresql username to perform backups under.
if [ -z $PGUSER ]; then
PGUSER="$POSTGRES_USER"
fi
##################
# Locations #
##################
#
# Location to place backups.
if [ -z $PGBACKUPDIR ]; then
PGBACKUPDIR="/mnt/pg_backup"
fi
# Location of the psql binaries.
if [ -z $PGBINDIR ]; then
PGBINDIR="/usr/bin"
fi
##################
# Permissions #
##################
#
# Permissions for the backup location.
permissions_backup_dir="0755"
# Permissions for the backup files.
permissions_backup_file="0644"
##################
# Other options #
##################
#
# Databases to exclude from the backup process (separated by a space)
exclusions="postgres $PGUSER"
# Backup format
# |
# +-- p = plain text : psql database < backup_file
# +-- t = tar : pg_restore -F t -d database backup_file
# +-- c = custom : pg_restore -F c -d database backup_file
#
backup_format="p"
# Backup large objects
backup_large_objects="no"
# bzip2 the backups
backup_bzip2="yes"
# Date format for the backup
# |
# +-- %d-%m-%Y = DD-MM-YYYY
# +-- %Y-%m-%d = YYYY-MM-DD
# +-- %A-%b-%Y = Tuesday-Sep-2001
# +-- %A-%Y-%d-%m-%Y = Tuesday-2001-18-09-2001
# |
# +-- For more date formats type:
#
# date --help
#
backup_date_format="%Y-%m-%d"
# You must comment out the line below before using this script
#echo "You must set all values in the configuration section in this file then run ./pg_backup.sh configtest before using this script" && exit 1
########################################################################
# End configuration #
########################################################################
#
#################
# Variables #
#################
#
version="1.1.3"
current_time=`date +%H%M`
date_info=`date +$backup_date_format`
# Export the variables
export PGUSER
#################
# Checking #
#################
#
# Check the backup format
if [ "$backup_format" = "p" ]; then
backup_type="Plain text SQL"
backup_args="-F $backup_format"
elif [ "$backup_format" = "t" ]; then
backup_type="Tar"
if [ "$backup_large_objects" = "yes" ]; then
backup_args="-b -F $backup_format"
else
backup_args="-F $backup_format"
fi
elif [ "$backup_format" = "c" ]; then
backup_type="Custom"
if [ "$backup_large_objects" = "yes" ]; then
backup_args="-b -F $backup_format"
else
backup_args="-F $backup_format"
fi
else
backup_format="c"
backup_args="-F $backup_format"
backup_type="Custom"
fi
#################
# Functions #
#################
#
# Obtain a list of available databases with reference to the user
# defined exclusions
db_connectivity() {
tmp=`echo "($exclusions)" | sed 's/\ /\|/g'`
if [ "$exclusions" = "" ]; then
databases=`$PGBINDIR/psql -U $PGUSER --tuples-only -P format=unaligned -c "SELECT datname FROM pg_database WHERE NOT datistemplate"`
else
# databases=`$PGBINDIR/psql -U $PGUSER -q -c "\l" template1 | sed -n 4,/\eof/p | grep -v rows\) | grep -Ev $tmp | awk {'print $1'} || echo "Database connection could not be established at $timeinfo"`
databases=`$PGBINDIR/psql -U $PGUSER --tuples-only -P format=unaligned -c "SELECT datname FROM pg_database WHERE NOT datistemplate" | grep -Ev $tmp`
fi
}
# Setup the permissions according to the Permissions section
set_permissions() {
# Make the backup directories and secure them.
mkdir -m $permissions_backup_dir -p "$PGBACKUPDIR"
# Make the backup tree
chmod -f $permissions_backup_dir "$PGBACKUPDIR"
}
# Run backup
run_b() {
for i in $databases; do
start_time=`date '+%s'`
timeinfo=`date '+%T %x'`
OF="$PGBACKUPDIR/$date_info-pg_db-$i.backup"
"$PGBINDIR/pg_dump" $backup_args $i > "$OF"
if [ "$backup_bzip2" = "yes" ]; then
bzip2 "$OF"
chmod $permissions_backup_file "$OF.bz2"
else
chmod $permissions_backup_file "$OF"
fi
finish_time=`date '+%s'`
duration=`expr $finish_time - $start_time`
echo "Backup complete (duration $duration seconds) at $timeinfo for schedule $current_time on database: $i, format: $backup_type"
echo "Backup File Name: $OF"
done
exit 1
}
# Run backup and vacuum
run_bv() {
for i in $databases; do
start_time=`date '+%s'`
timeinfo=`date '+%T %x'`
OF="$PGBACKUPDIR/$date_info-pg_db-$i.backup"
"$PGBINDIR/vacuumdb" -U $PGUSER $i >/dev/null 2>&1
"$PGBINDIR/pg_dump" $backup_args $i > "$OF"
if [ "$backup_bzip2" = "yes" ]; then
bzip2 "$OF"
chmod $permissions_backup_file "$OF.bz2"
else
chmod $permissions_backup_file "$OF"
fi
finish_time=`date '+%s'`
duration=`expr $finish_time - $start_time`
echo "Backup and Vacuum complete (duration $duration seconds) at $timeinfo for schedule $current_time on database: $i, format: $backup_type"
echo "Backup File Name: $OF"
done
exit 1
}
# Run backup, vacuum and analyze
run_bva() {
for i in $databases; do
start_time=`date '+%s'`
timeinfo=`date '+%T %x'`
OF="$PGBACKUPDIR/$date_info-pg_db-$i.backup"
"$PGBINDIR/vacuumdb" -z -U $PGUSER $i >/dev/null 2>&1
"$PGBINDIR/pg_dump" $backup_args $i > "$OF"
if [ "$backup_bzip2" = "yes" ]; then
bzip2 "$OF"
chmod $permissions_backup_file "$OF.bz2"
else
chmod $permissions_backup_file "$OF"
fi
finish_time=`date '+%s'`
duration=`expr $finish_time - $start_time`
echo "Backup, Vacuum and Analyze complete (duration $duration seconds) at $timeinfo for schedule $current_time on database: $i, format: $backup_type"
echo "Backup File Name: $OF"
done
exit 1
}
# Run vacuum
run_v() {
for i in $databases; do
start_time=`date '+%s'`
timeinfo=`date '+%T %x'`
"$PGBINDIR/vacuumdb" -U $PGUSER $i >/dev/null 2>&1
finish_time=`date '+%s'`
duration=`expr $finish_time - $start_time`
echo "Vacuum complete (duration $duration seconds) at $timeinfo for schedule $current_time on database: $i "
done
exit 1
}
# Run vacuum and analyze
run_va() {
for i in $databases; do
start_time=`date '+%s'`
timeinfo=`date '+%T %x'`
"$PGBINDIR/vacuumdb" -z -U $PGUSER $i >/dev/null 2>&1
finish_time=`date '+%s'`
duration=`expr $finish_time - $start_time`
echo "Vacuum and Analyze complete (duration $duration seconds) at $timeinfo for schedule $current_time on database: $i "
done
exit 1
}
# Print information regarding available backups
print_info() {
echo "Postgresql backup script version $version"
echo ""
echo "Available backups:"
echo ""
if [ ! -d $PGBACKUPDIR ] ; then
echo "There are currently no available backups"
echo ""
exit 0
else
echo "$i `du -h \"$i\" | tail -n 1 | awk {'print $1'}`"
echo ""
fi
exit 1
}
# Print configuration test
print_configtest() {
echo "Postgresql backup script version $version"
echo ""
echo "Configuration test..."
echo ""
# Check database connectivity information
echo -n "Database username : "
echo "$PGUSER"
echo -n "Database connectivity : "
$PGBINDIR/psql -U $PGUSER -q -c "select now()" $PGUSER > /dev/null 2>&1 && echo "Yes" || echo "Connection could not be established..."
# Backup information
echo ""
echo "Backup information:"
echo ""
echo -n "Backup format : "
if [ "$backup_format" = "p" ]; then
echo "Plain text SQL"
elif [ "$backup_format" = "t" ]; then
echo "Tar"
else
echo "Custom"
fi
echo -n "Backup large objects : "
if [ "$backup_large_objects" = "yes" ]; then
echo "Yes"
else
echo "No"
fi
echo -n "bzip2 backups : "
if [ "$backup_bzip2" = "yes" ]; then
echo "Yes"
else
echo "No"
fi
echo -n "Backup date format : $date_info"
echo ""
# File locations
echo -n "Backup directory : "
echo "$PGBACKUPDIR"
echo -n "Postgresql binaries : "
echo "$PGBINDIR"
# Backup file permissions
echo -n "Backup directory permissions : "
echo "$permissions_backup_dir"
echo -n "Backup file permissions : "
echo "$permissions_backup_file"
# Databases that will be backed up
echo -n "Databases that will be backed up : "
echo ""
if [ "$databases" = "" ]; then
echo " none"
else
for i in $databases; do
echo " $i"
done
echo ""
fi
# Databases that will not be backed up
echo -n "Databases that will not be backed up :"
echo ""
if [ "$exclusions" = "" ]; then
echo " none"
else
for i in $exclusions; do
echo " $i"
done
echo ""
fi
# Check if the backups location is writable
echo "Checking permissions:"
echo ""
echo -n "Write access : $PGBACKUPDIR: "
# Needed to create/write to the dump location"
test -w "$PGBACKUPDIR" && echo "Yes" || echo "No"
# Check if the binaries are executable
echo -n "Execute access : $PGBINDIR/psql: "
test -x $PGBINDIR/psql && echo "Yes" || echo "No"
echo -n "Execute access : $PGBINDIR/pg_dump: "
test -x $PGBINDIR/pg_dump && echo "Yes" || echo "No"
echo -n "Execute access : $PGBINDIR/vacuumdb: "
test -x $PGBINDIR/vacuumdb && echo "Yes" || echo "No"
echo ""
exit 1
}
# Print help
print_help() {
echo "Postgresql backup script version $version"
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " b, Backup ALL databases"
echo " bv, Backup and Vacuum ALL databases"
echo " bva, Backup, Vacuum and Analyze ALL databases"
echo " v, Vacuum ALL databases"
echo " va, Vacuum and Analyze ALL databases"
echo " info, Information regarding all available backups"
echo " configtest, Configuration test"
echo " help, This message"
echo ""
exit 1
}
case "$1" in
# Run backup
b)
db_connectivity
set_permissions
run_b
exit 1
;;
# Run backup and vacuum
bv)
db_connectivity
set_permissions
run_bv
exit 1
;;
# Run backup, vacuum and analyze
bva)
db_connectivity
set_permissions
run_bva
exit 1
;;
# Run vacuum
v)
db_connectivity
set_permissions
run_v
exit 1
;;
# Run vacuum and analyze
va)
db_connectivity
set_permissions
run_va
exit 1
;;
# Print info
info)
set_permissions
print_info
exit 1
;;
# Print configtest
configtest)
db_connectivity
set_permissions
print_configtest
exit 1
;;
# Default
*)
print_help
exit 1
;;
esac