aboutsummaryrefslogtreecommitdiff
path: root/lib/backup/postgres.sh
blob: 953c467e95ec9d4522fe8655e27c12fd43845d31 (plain)
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
#! /bin/bash

# postgres_check item
postgres_check() {
  eval database=\$${1}_database
  test -z "$database" && error "${1}_database not set" && return 1
  return 0
}

# postgres_backup item
postgres_backup() {
  local database

  logit "backing up PostgreSQL $1"
  eval database=\$${1}_database
  test -z "$database" && abend 1 "${1}_database not set"
  if [ -z "$dry_run" ]; then
    su postgres -c "pg_dump $verbose $database" > $backup_snapshot_dir/$1-$week-$round-$level    
  else
    echo "su postgres -c \"pg_dump $verbose $database\" > $backup_snapshot_dir/$1-$week-$round-$level"
  fi

  if [ $? -ne 0 ]; then
    tarerror=$((tarerror + 1))
    echo >&2 "`date`: failed"
  else
    echo "`date`: creating $1-$week-$round-$level.$tar_suffix"
    $dry_run tar $verbose $taroptions \
	      -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix \
              -C $backup_snapshot_dir $1-$week-$round-$level
    tarcode $?
    $dry_run rm $snapshotdir/dbdump-$week-$round-$level
  fi
}

postgres_restore() {
  local u database

  eval database=\$${1}_database
  logit "restoring PostgreSQL database $database"
  u=$(umask)
  trap "umask $u" 1 2 3 13 15
  umask 077
  $dry_run tar $verbose $taroptions \
           -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix
  e=$?
  tarcode $e
  if [ $e -eq 0 ]; then
    logit "restoring database from the dump"
    if [ -n "$dry_run" ]; then
      cat <<-EOT
	su postgres -c "dropdb $database"
	su postgres -c "createdb $database"
	su postgres -c "psql -d $database -f $1-$week-$round-$level"
	rm $1-$week-$round-$level
EOT
    elif [ -r $1-$week-$round-$level ]; then
      su postgres -c "dropdb $database"
      su postgres -c "createdb $database"
      su postgres -c "psql -d $database -f $1-$week-$round-$level" > db-$1.log
      if grep ERROR db-$1.log >/dev/null; then
        error "errors occurred during restore; see db-$1.log for details"
	error "dump preserved in file $1-$week-$round-$level"
	tarerror=$((tarerror + 1))
      else
        rm $1-$week-$round-$level
      fi
    fi
  fi
  umask $u
  trap - 1 2 3 13 15
}

Return to:

Send suggestions and report system problems to the System administrator.