aboutsummaryrefslogtreecommitdiff
path: root/lib/beam/mysql.sh
blob: c172f44839f168592a782b8cf0727e1a031a6542 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /bin/sh
# This file is part of BEAM
# Copyright (C) 2012 Sergey Poznyakoff
#
# BEAM is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# BEAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BEAM.  If not, see <http://www.gnu.org/licenses/>.

# mysql_check item
mysql_check() {
  return 0
}

# mysql_list item prefix
mysql_list() {
  local database
  
  eval database=\$${1}_database
  if [ -z "$database" ]; then
    echo "${2}all MySQL databases"
  else
    echo "${2}MySQL database $database"
  fi
}

# mysql_backup item
mysql_backup() {
  local database

  logit "backing up MySQL database $1"
  eval database=\$${1}_database
  cmd="mysqldump"
  eval defaults_file=\$${1}_defaults_file
  if [ -n "$defaults_file" ]; then
    cmd="$cmd --defaults-file=$defaults_file"
  fi
  cmd="$cmd --add-drop-database"
  if test -z "$database"; then
    cmd="$cmd --all-databases"
  else
    cmd="$cmd --databases"
  fi
  if [ -z "$dry_run" ]; then
    $cmd $database > $backup_snapshot_dir/$1-$week-$round-$level
  else
    echo "$cmd $database > $backup_snapshot_dir/$1-$week-$round-$level"
  fi

  if [ $? -ne 0 ]; then
    tarerror=$((tarerror + 1))
    logit "failed"
  else
    logit "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 $backup_snapshot_dir/$1-$week-$round-$level
  fi
}

mysql_restore() {
  local u database

  eval database=\$${1}_database
  logit "restoring MySQL 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"
    # Stupid lossage: cannot give -A option here, because with it mysql
    # refuses to understand defaults-file.
    cmd="mysql"
    eval defaults_file=\$${1}_defaults_file
    if [ -n "$defaults_file" ]; then
      cmd="$cmd --defaults-file=$defaults_file"
    fi
    if [ -n "$dry_run" ]; then
      echo "$cmd < $1-$week-$round-$level"
    elif [ -r $1-$week-$round-$level ]; then
      $cmd < $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.