aboutsummaryrefslogtreecommitdiff
path: root/lib/beam/mysql.sh
blob: e8ec32cf51a950cefe0ee37afc7d759a175144e5 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! /bin/sh
# This file is part of BEAM
# Copyright (C) 2012-2014 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/>.

# Configuration keywords:
#
# item_type=mysql                               [mandatory]
# item_database=STRING                          [optional]
# item_defaults_file=STRING                     [optional]
# item_alldb=single|monolithic|split|individual [optional]
#

# 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
}

# dump_mysql_db cmd stem
dump_mysql_db() {
    if [ -z "$dry_run" ]; then
	$1 $db > $backup_tmp_dir/$2-$week-$round-$level
    else
        echo "$1 $db > $backup_tmp_dir/$2-$week-$round-$level"
    fi

    if [ $? -ne 0 ]; then
	tarerror=$((tarerror + 1))
        logit "failed"
    else
	logit "creating $2-$week-$round-$level.$tar_suffix"
	$dry_run tar $verbose $taroptions \
	      -f $backup_archive_dir/$2-$week-$round-$level.$tar_suffix \
              -C $backup_tmp_dir $2-$week-$round-$level
        tarcode $?
        $dry_run rm $backup_tmp_dir/$2-$week-$round-$level
    fi
}    

restore_mysql_db() {
    local u
    
    logit "restoring MySQL database $1"
    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
}

# mysql_backup item
mysql_backup() {
    local creds cmd database alldb db stem

    eval database=\$${1}_database
    if [ -z "$database" ]; then
	logit "backing up all MySQL databases"
    else
	logit "backing up MySQL database $database"
    fi
    cmd="mysqldump"
    eval defaults_file=\$${1}_defaults_file
    if [ -n "$defaults_file" ]; then
	creds="--defaults-file=$defaults_file"
	cmd="$cmd $creds"
    fi
    cmd="$cmd --add-drop-database"
    cmd="$cmd --single-transaction"
    
    if test -z "$database"; then
	eval alldb=\$${1}_alldb
	case $alldb in
	single|monolithic)
	    cmd="$cmd --all-databases";;
	split|individual)
	    alldb=split
            cmd="$cmd --databases"
	    database=$(mysql $creds -e "show databases" -B --skip-column-names);;
        "") cmd="$cmd --all-databases";;
	*)  error "unknown value: ${1}_alldb=$alldb, assuming 'single'"
	    cmd="$cmd --all-databases";;
	esac	    
    else
	cmd="$cmd --databases"
    fi

    if [ -z "$database" ]; then
        dump_mysql_db "$cmd" "$1"
    else
	for db in $database
        do
	    if [ "$alldb" = split ]; then
		stem=$1-$db
	        logit "dumping $db"
	    else
	        stem=$1
	    fi
            dump_mysql_db "$cmd" "$stem"
        done
    fi
}

mysql_restore() {
    local database dbname remote

    eval database=\$${1}_database
    if test -z "$database"; then
	eval alldb=\$${1}_alldb
	case $alldb in
	split|individual)
		beam_exec find $backup_local_archive_dir \
		       -name $1-'*'-$week-$round-$level.$tar_suffix \
		       -printf '%f\\n' | \
		while read file
		do
		    restore_mysql_db $(expr "$file" : "$1-\(.[^-]*\)-.*")
                done		    
                ;;
        *)      restore_mysql_db $1
	esac	    
    else
	restore_mysql_db $database
    fi
}

Return to:

Send suggestions and report system problems to the System administrator.