-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup-pgsql-databases
executable file
·64 lines (56 loc) · 1.51 KB
/
backup-pgsql-databases
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
#!/opt/local/bin/bash
#
# Dumps all databases belonging to a user to seperate dump files.
#
# Copyright (c) 2013 Jacques Marneweck. All rights reserved.
#
# Expects that you have a ~/.pgpass configured to allow this script
# to connect to PostgreSQL in order to dump databases.
#
if [[ "$1" == "-v" ]]; then
shift;
export export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -o errexit
set -o pipefail
#
# Check that ~/.pgpass exists
#
if [[ ! -f ~/.pgpass ]]; then
echo "You need to create the ~/.pgpass file into place"
exit 1
fi
#
# Check that the config file exitss
#
if [[ ! -f config ]]; then
echo "You need to copy config.example to config and customise to your liking."
exit 1
fi
#
# Load the configuration file (see config.example for a sample configuration
# file)
#
source config
#
# Check that the backups directory exists if not create it and lock down
# permissions
#
if [[ ! -d $BACKUP_BASE_DIR ]]; then
/usr/bin/mkdir $BACKUP_BASE_DIR
/usr/bin/chmod 700 $BACKUP_BASE_DIR
fi
if [[ ! -d $BACKUP_BASE_DIR/pgsql ]]; then
/usr/bin/mkdir $BACKUP_BASE_DIR/pgsql
fi
#
# Clean up the pgsql dumps directory to the specified number of days worth of
# PostgreSQL dump files to keep around.
#
cd ${BACKUP_BASE_DIR}/pgsql
/opt/local/bin/gfind *.sql.bz2 -mtime +${PGSQL_DAYS_TO_KEEP} -delete
#
# Upload the backup files to a remote server
#
/opt/local/bin/rsync -avz ${RSYNC_OPTS} ${BACKUP_BASE_DIR}/pgsql ${BACKUP_USER}@${BACKUP_HOST}:${BACKUP_REMOTE_DIR}