Changeset 177 for trunk/tools

Show
Ignore:
Timestamp:
07/14/10 19:42:44 (4 years ago)
Author:
ssteinpreis
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/zbackup.sh

    r10 r177  
    22 
    33BASESNAP=lastfull 
    4 INCREMENTAL=no 
    5 BACKUPCONF=/etc/zbackup.conf 
    6 BFILE=/var/log/pgods-backup.filelist 
    7 BPCMD=/usr/openv/netbackup/bin/bpbackup 
    8 BPLOG=/var/log/netbackup_backup.log 
    9 BPCLASS=PGods 
    10 BPSCHED=PGODS.UserBackup 
     4BACKUPCONF=/home/postgres/etc/zbackup.conf 
     5BFILE=/home/postgres/pgshared3-backup.filelist 
    116RUN=true 
    12 INCDUMPDIR=/tmp 
     7PGBINDIR=/opt/pgsql/bin 
     8DATABASE_PORT=5432 
    139 
    1410usage() { 
    1511cat <<EOF 
    16 $0: [-f] [-i] [-n] [-s] -l <label> 
     12$0: [-f] [-n] -p <port_number> [-s] -l <label> 
    1713        -n              just describe the process (don't do it). 
     14        -p              port which database runs on (default 5432) 
    1815        -s              use ZFS send. 
    1916        -f              force a backup even if one is running. 
    20         -i              perform an incremental since the last full (implies -s). 
    2117        -l <label>      backup using label: <label> 
    2218EOF 
    2319} 
    2420 
    25   set -- `getopt fisnl: $*` 
     21  set -- `getopt fisnp:l: $*` 
    2622  if [ $? != 0 ] 
    2723  then 
     
    3430      -f)           FORCE=yes; shift;; 
    3531      -s)           SEND=yes; shift;; 
    36       -i)           INCREMENTAL=yes; shift;; 
    3732      -l)           BACKUPLABEL=$2; shift 2;; 
     33      -p)           DATABASE_PORT=$2; shift 2;; 
    3834      -n)           RUN=false; shift;; 
    3935      --)           shift; break;; 
     
    4238 
    4339if test -z "$BACKUPLABEL"; then 
    44   BACKUPLABEL=`date +%Y%m%d` 
     40  usage 
     41  exit 
    4542fi 
    4643 
    4744echo "$0:" 
    4845echo "  backuplabel: $BACKUPLABEL" 
    49 if test "$INCREMENTAL" = "yes"; then 
    50   SNAPNAME=$BACKUPLABEL 
    51   BFILE="$BFILE.i" 
    52   echo "  incremental" 
    53 else 
    54   SNAPNAME=$BASESNAP 
    55   echo "  full" 
    56 fi 
    57  
    58 sanity() { 
    59   MATCHCNT=0 
    60   SNAPCNT=0 
    61   if test -f $BFILE -a "x$FORCE" != "xyes" ; then 
    62     echo "backup in progress ($BFILE exists)." 
    63     exit 
    64   fi 
    65   for line in `zfs list -H | awk '{print $1":"$5;}'` 
    66   do 
    67     ZFS=`echo $line | awk -F: '{print $1;}'` 
    68     MOUNT=`echo $line | awk -F: '{print $2;}'` 
    69     MATCH=`grep '^[YN] '$ZFS' '$MOUNT'$' $BACKUPCONF` 
    70     USE=`echo $MATCH | awk '{print $1;}'` 
    71     SNAP=`echo $ZFS | awk -F@ '{print $2;}'` 
    72     # Here, if we have the snap and we're in "full" node, we die 
    73     if test "X$SNAP" = "X$BASESNAP"; then 
    74       if test "$INCREMENTAL" != "yes" ; then 
    75         echo "Base snapshot already exists." 
    76         exit 
    77       fi 
    78       # These are the base snapshots off which we'll do incrementals 
    79       if test -z "$MATCH"; then 
    80         SNAPCNT=`expr $SNAPCNT + 1` 
    81       fi 
    82     fi 
    83     if test -z "$MATCH" -a -z "$SNAP" ; then 
    84       echo "WARNING: $ZFS on $MOUNT is not present in $BACKUPCONF" 
    85     fi 
    86     if test -z "$SNAP" -a "$USE" = "Y" ; then 
    87       # These are the "real" mounts we want to backup 
    88       MATCHCNT=`expr $MATCHCNT + 1` 
    89     fi 
    90   done 
    91  
    92   # or, if we don't have the snaps and we're in incremental mode, we die 
    93   if test "$SNAPCNT" != "$MATCHCNT" -a "$INCREMENTAL" = "yes"; then 
    94     echo "We don't have base snaps for incremental backups." 
    95     exit 
    96   fi 
    97 
     46SNAPNAME=$BASESNAP 
     47echo "  full" 
    9848 
    9949postgres_start_backup() { 
    10050  echo "starting postgres backup on label $BACKUPLABEL" 
    101   $RUN && su - postgres -c "/bin/psql -c \"CHECKPOINT;\"" 
    102   $RUN && su - postgres -c "/bin/psql -c \"SELECT pg_start_backup('$BACKUPLABEL');\"" 
     51  $RUN && su - postgres -c "$PGBINDIR/psql -p $DATABASE_PORT -c \"SELECT pg_start_backup('$BACKUPLABEL');\"" 
    10352} 
    10453postgres_stop_backup() { 
    10554  echo "stopping postgres backup on label $BACKUPLABEL" 
    106   $RUN && su - postgres -c "/bin/psql -c \"SELECT pg_stop_backup();\"" 
     55  $RUN && su - postgres -c "$PGBINDIR/psql -p $DATABASE_PORT -c \"SELECT pg_stop_backup();\"" 
    10756} 
    10857 
     
    12170 
    12271clear_full() { 
    123   if test "$INCREMENTAL" != "yes"; then 
    12472    for line in `sed -e 's/ /:/g;' < $BACKUPCONF` 
    12573    do 
     
    13179      fi 
    13280    done 
    133   fi 
    134 } 
    135  
    136 backup() { 
    137   BACKEDUP="no" 
    138   $RUN && touch $BFILE 
    139   for line in `sed -e 's/ /:/g;' < $BACKUPCONF` 
    140   do 
    141     DO=`echo $line | awk -F: '{print $1;}'` 
    142     ZFS=`echo $line | awk -F: '{print $2;}'` 
    143     MOUNT=`echo $line | awk -F: '{print $3;}'` 
    144     if test "$DO" = "Y"; then 
    145       if test "$INCREMENTAL" = "yes"; then 
    146         FILE=`echo $ZFS | sed -e 's/\//:/g;'` 
    147         echo "/sbin/zfs send -i $ZFS@$BASESNAP $ZFS@$SNAPNAME | /bin/bzip2 -3c >> $INCDUMPDIR/$FILE.$SNAPNAME.incremental" 
    148         $RUN && mkfifo $INCDUMPDIR/$FILE.$SNAPNAME.incremental 
    149         $RUN && /sbin/zfs send -i $ZFS@$BASESNAP $ZFS@$SNAPNAME >> $INCDUMPDIR/$FILE.$SNAPNAME.incremental & 
    150         $RUN && $BPCMD -w -c $BPCLASS -s $BPSCHED -L $BPLOG $INCDUMPDIR/$FILE.$SNAPNAME.incremental & 
    151       elif test "$SEND" = "yes"; then 
    152         FILE=`echo $ZFS | sed -e 's/\//:/g;'` 
    153         echo "/sbin/zfs send $ZFS@$SNAPNAME >> $INCDUMPDIR/$FILE.$SNAPNAME.full" 
    154         $RUN && mkfifo $INCDUMPDIR/$FILE.$SNAPNAME.full 
    155         $RUN && /sbin/zfs send $ZFS@$SNAPNAME >> $INCDUMPDIR/$FILE.$SNAPNAME.full & 
    156         $RUN && $BPCMD -w -c $BPCLASS -s $BPSCHED -L $BPLOG $INCDUMPDIR/$FILE.$SNAPNAME.full & 
    157       else 
    158         # Normal tar-like backups happen "on the spot" and in parallel 
    159         echo $MOUNT/.zfs/snapshot/$SNAPNAME 
    160         $RUN && $BPCMD -w -c $BPCLASS -s $BPSCHED -L $BPLOG $MOUNT/.zfs/snapshot/$SNAPNAME & 
    161       fi 
    162     fi 
    163   done 
    164  
    165   # Wait for our ZFS send commands to finish (they should be done already) 
    166   wait 
    167   $RUN && rm $BFILE 
    16881} 
    16982 
    17083# This clears the full snapshot only if -i (and -n) isn't specified 
    17184clear_full 
    172 sanity 
    17385echo "Backing up as '$BACKUPLABEL'" 
    17486postgres_start_backup 
    17587snap 
    17688postgres_stop_backup 
    177 backup 
    17889