-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmsprime_pg_dump.sh
executable file
·78 lines (60 loc) · 2.03 KB
/
nmsprime_pg_dump.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
#!/bin/bash
# exit on first error
set -euf -o pipefail
DUMPCMD="sudo -u postgres /usr/pgsql-13/bin/pg_dump"
DUMPDIR="/root/db_dumps/manually"
NMSPRIME_DIR="/var/www/nmsprime"
DB="nmsprime"
CCC_DB="nmsprime_ccc"
NETBOX_DB="netbox"
echo
# check if a description is given
# this is expected to be a string containing no spaces
if [ "$#" -eq 1 ]; then
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: $0 [description]"
echo
echo " optional description is expected to be a string without spaces and will be added to filename"
echo
exit 0
else
DESC="__"$1
fi
else
DESC=""
fi
# warn if more than one argument is given (e.g. a description containing spaces
if [ "$#" -gt 1 ]; then
echo "ERROR:"
echo "$0 accepts up to one argument, $# given"
exit 1
fi
if ! test -e $DUMPDIR; then
mkdir -p $DUMPDIR
fi
# get the branch name to add it to dumpfile name
cd $NMSPRIME_DIR
if ! test -e .git; then
BRANCH="default"
else
BRANCH=$(git branch | grep "*" | cut -c 3- | tr '/' '__')
fi
TIMESTAMP="`date +%Y-%m-%dT%H-%M-%S`"
PREFIX=$DB
CCC_PREFIX=$CCC_DB
NETBOX_PREFIX=$NETBOX_DB
SUFFIX=".pg.sql.bz2"
DUMPFILE="$DUMPDIR"/"$TIMESTAMP"__"$BRANCH$DESC"__"$PREFIX$SUFFIX"
CCC_DUMPFILE="$DUMPDIR"/"$TIMESTAMP"__"$BRANCH$DESC"__"$CCC_PREFIX$SUFFIX"
NETBOX_DUMPFILE="$DUMPDIR"/"$TIMESTAMP"__"$NETBOX_PREFIX$SUFFIX"
# hint: We are doing plain dumps to be able to read and even edit them later on
# for compression we use bzip2 (so we can use bzcat and bzless to work with compressed files)
# caveat: bzip2 is really slow – but works for our devel databases
# One can simply extract the file (using bunzip) and restore the non-compressed file using nmsprime_pg_restore.sh as well)
echo "Dumping database $DB to $DUMPFILE…"
$DUMPCMD nmsprime --clean --create --if-exists | bzip2 > $DUMPFILE
echo "Dumping database $CCC_DB to $CCC_DUMPFILE…"
$DUMPCMD nmsprime_ccc --clean --create --if-exists | bzip2 > $CCC_DUMPFILE
echo "Dumping database $NETBOX_DB to $NETBOX_DUMPFILE…"
$DUMPCMD netbox --clean --create --if-exists | bzip2 > $NETBOX_DUMPFILE
exit 0