root/trunk/tools/zbackup.sh

Revision 177, 2.1 kB (checked in by ssteinpreis, 4 years ago)

tid6031 modified to take postgres database port as command line option, default port 5432

  • Property svn:executable set to *
Line 
1 #!/bin/sh
2
3 BASESNAP=lastfull
4 BACKUPCONF=/home/postgres/etc/zbackup.conf
5 BFILE=/home/postgres/pgshared3-backup.filelist
6 RUN=true
7 PGBINDIR=/opt/pgsql/bin
8 DATABASE_PORT=5432
9
10 usage() {
11 cat <<EOF
12 $0: [-f] [-n] -p <port_number> [-s] -l <label>
13         -n              just describe the process (don't do it).
14         -p              port which database runs on (default 5432)
15         -s              use ZFS send.
16         -f              force a backup even if one is running.
17         -l <label>      backup using label: <label>
18 EOF
19 }
20
21   set -- `getopt fisnp:l: $*`
22   if [ $? != 0 ]
23   then
24     usage
25     exit 2
26   fi
27   for i in $*
28   do
29     case $i in
30       -f)           FORCE=yes; shift;;
31       -s)           SEND=yes; shift;;
32       -l)           BACKUPLABEL=$2; shift 2;;
33       -p)           DATABASE_PORT=$2; shift 2;;
34       -n)           RUN=false; shift;;
35       --)           shift; break;;
36     esac
37   done
38
39 if test -z "$BACKUPLABEL"; then
40   usage
41   exit
42 fi
43
44 echo "$0:"
45 echo "  backuplabel: $BACKUPLABEL"
46 SNAPNAME=$BASESNAP
47 echo "  full"
48
49 postgres_start_backup() {
50   echo "starting postgres backup on label $BACKUPLABEL"
51   $RUN && su - postgres -c "$PGBINDIR/psql -p $DATABASE_PORT -c \"SELECT pg_start_backup('$BACKUPLABEL');\""
52 }
53 postgres_stop_backup() {
54   echo "stopping postgres backup on label $BACKUPLABEL"
55   $RUN && su - postgres -c "$PGBINDIR/psql -p $DATABASE_PORT -c \"SELECT pg_stop_backup();\""
56 }
57
58 snap() {
59   for line in `sed -e 's/ /:/g;' < $BACKUPCONF`
60   do
61     DO=`echo $line | awk -F: '{print $1;}'`
62     ZFS=`echo $line | awk -F: '{print $2;}'`
63     MOUNT=`echo $line | awk -F: '{print $3;}'`
64     if test "$DO" = "Y"; then
65       echo "zfs snapshot $ZFS@$SNAPNAME"
66       $RUN && /sbin/zfs snapshot $ZFS@$SNAPNAME
67     fi
68   done
69 }
70
71 clear_full() {
72     for line in `sed -e 's/ /:/g;' < $BACKUPCONF`
73     do
74       DO=`echo $line | awk -F: '{print $1;}'`
75       ZFS=`echo $line | awk -F: '{print $2;}'`
76       if test "$DO" = "Y"; then
77         echo "zfs destroy $ZFS@$SNAPNAME"
78         $RUN && /sbin/zfs destroy $ZFS@$SNAPNAME
79       fi
80     done
81 }
82
83 # This clears the full snapshot only if -i (and -n) isn't specified
84 clear_full
85 echo "Backing up as '$BACKUPLABEL'"
86 postgres_start_backup
87 snap
88 postgres_stop_backup
89
Note: See TracBrowser for help on using the browser.