| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # Copyright 2007 Peter Palfrader <peter@palfrader.org> |
|---|
| 4 | # |
|---|
| 5 | # Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 6 | # a copy of this software and associated documentation files (the |
|---|
| 7 | # "Software"), to deal in the Software without restriction, including |
|---|
| 8 | # without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 9 | # distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 10 | # permit persons to whom the Software is furnished to do so, subject to |
|---|
| 11 | # the following conditions: |
|---|
| 12 | # |
|---|
| 13 | # The above copyright notice and this permission notice shall be |
|---|
| 14 | # included in all copies or substantial portions of the Software. |
|---|
| 15 | # |
|---|
| 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|---|
| 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|---|
| 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 23 | |
|---|
| 24 | # create a backup of the services database, excluding the log (transaction/write ahead logs) directory |
|---|
| 25 | # those are handled separately |
|---|
| 26 | |
|---|
| 27 | set -e |
|---|
| 28 | |
|---|
| 29 | umask 077 |
|---|
| 30 | |
|---|
| 31 | HOST=`hostname` |
|---|
| 32 | CLUSTER="ircservices" |
|---|
| 33 | VERSION="8.3" |
|---|
| 34 | DATE=$(date "+%Y%m%d") |
|---|
| 35 | PORT=5433 |
|---|
| 36 | |
|---|
| 37 | LABEL="$DATE-$HOST-$CLUSTER-$VERSION-backup" |
|---|
| 38 | ID=`sudo -u postgres psql -p "$PORT" --tuples-only --command "SELECT pg_start_backup('$LABEL');"` |
|---|
| 39 | trap "sudo -u postgres psql -q -p '$PORT' --command 'SELECT pg_stop_backup();'" exit |
|---|
| 40 | ID=`echo $ID | sed -e 's/[^a-zA-Z0-9]/_/g'` |
|---|
| 41 | |
|---|
| 42 | cd /var/lib/postgresql |
|---|
| 43 | TARBALL="$LABEL-$ID.tar.bz2" |
|---|
| 44 | if tar cjf "$TARBALL" --exclude "$VERSION/$CLUSTER/pg_xlog" "$VERSION/$CLUSTER"; then |
|---|
| 45 | : # all went well |
|---|
| 46 | else |
|---|
| 47 | if [ "$?" = 1 ]; then |
|---|
| 48 | echo "Ignoring tar exit code of 1" >&2 |
|---|
| 49 | else |
|---|
| 50 | echo "Dying because tar exited with an exit code not in {0, 1}" >&2 |
|---|
| 51 | exit 1 |
|---|
| 52 | fi |
|---|
| 53 | fi |
|---|
| 54 | |
|---|
| 55 | chown postgres: "$TARBALL" |
|---|
| 56 | sudo -u postgres scp "$TARBALL" backup:ircservices-tarballs/ |
|---|
| 57 | rm "$TARBALL" |
|---|