00eeca0ce15e2f142e0a263f34ba09a17d9caae5
[matthijs/upstream/backupninja.git] / handlers / mysql.helper.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2
3 HELPERS="$HELPERS mysql:mysql_database_backup"
4
5 do_mysql_vserver() {
6    choose_one_vserver "$mysql_title"
7    [ $? = 0 ] || return 1
8    mysql_vsname="vsname = $REPLY"
9 }
10
11 do_mysql_databases() {
12    REPLY=
13    while [ -z "$REPLY" ]; do
14       formBegin "$mysql_title: databases"
15          formItem "Database:"
16          formItem "Database:"
17          formItem "Database:"
18          formItem "Database:"
19          formItem "Database:"
20          formItem "Database:"
21          formItem "Database:"
22          formItem "Database:"
23          formItem "Database:"
24          formItem "Database:"
25       formDisplay
26       [ $? = 0 ] || return 1
27       mysql_databases="databases = "
28       for i in $REPLY; do
29          [ -n "$i" ] && mysql_databases="$mysql_databases $i"
30       done
31    done
32 }
33
34 do_mysql_password() {
35   inputBox "$mysql_title" "specify a mysql user:"
36   [ $? = 1 ] && return
37   user=$REPLY
38   inputBox "$mysql_title" "specify the mysql user's password:"
39   [ $? = 1 ] && return
40   password=$REPLY
41   do_mysql_final "dbusername = $user\ndbpassword = $password"
42 }
43
44 do_mysql_debian() {
45   _DISABLE_HOTCOPY=yes
46   do_mysql_final "configfile = /etc/mysql/debian.cnf"
47 }
48
49 do_mysql_user() {
50   inputBox "$mysql_title" "what system user does mysql backup use?"
51   [ $? = 1 ] && return
52   do_mysql_final "user = $REPLY"
53 }
54
55 do_mysql_final() {
56    if [ -z "$_DISABLE_HOTCOPY" ]; then
57       checkBox "$mysql_title" "check options" \
58          "sqldump" "create a backup using mysqldump (more compat)." no \
59          "hotcopy" "create a backup using mysqlhotcopy (faster)." yes \
60          "compress" "compress the sql output files" yes
61       status=$?
62       sqldump="sqldump = no"
63       hotcopy="hotcopy = no"
64    else
65       checkBox "$mysql_title" "check options" \
66          "compress" "compress the sql output files" yes
67       status=$?
68       sqldump="sqldump = yes"
69       hotcopy="hotcopy = no"
70    fi
71
72    [ $status = 1 ] && return;    
73    result="$REPLY"
74    compress="compress = no"
75    for opt in $result; do
76       case $opt in
77         '"sqldump"') sqldump="sqldump = yes";;
78         '"hotcopy"') hotcopy="hotcopy = yes";;
79         '"compress"') compress="compress = yes";;
80       esac
81    done
82    get_next_filename $configdirectory/20.mysql
83    
84    cat >> $next_filename <<EOF
85 ### backupninja MySQL config file ###
86
87 # hotcopy = < yes | no > (default = no)
88 # make a backup of the actual database binary files using mysqlhotcopy.
89 $hotcopy
90
91 # sqldump = < yes | no > (default = no)
92 # make a backup using mysqldump. this creates text files with sql commands
93 # sufficient to recontruct the database.
94 #
95 $sqldump
96
97 # sqldumpoptions = <options>
98 # (default = --lock-tables --complete-insert --add-drop-table --quick --quote-names)
99 # arguments to pass to mysqldump
100 # sqldumpoptions = --add-drop-table --quick --quote-names
101
102 # compress = < yes | no > (default = yes)
103 # if yes, compress the sqldump output.
104 $compress
105
106 # dbhost      = <host> (default = localhost)
107
108 EOF
109    cat >> $next_filename <<EOF
110
111 # backupdir = <dir> (default: /var/backups/mysql)
112 # where to dump the backups. hotcopy backups will be in a subdirectory
113 # 'hotcopy' and sqldump backups will be in a subdirectory 'sqldump'
114 $mysql_backupdir
115
116 # databases = <all | db1 db2 db3 > (default = all)
117 # which databases to backup. should either be the word 'all' or a 
118 # space separated list of database names.
119 $mysql_databases
120
121 EOF
122
123 if [ $host_or_vservers == vservers ]
124    then
125        cat >> $next_filename <<EOF
126 #
127 # vsname = <vserver> (no default)
128 # vsname indicates which vserver to operate on, this is only used if 
129 # vserver is set to yes in /etc/backupninja.conf
130 # NOTE: if you do not specify a vsname the host will be operated on
131 # alsoNOTE: if operating on a vserver, $VROOTDIR will be 
132 # prepended to backupdir.
133 EOF
134    echo -e "$mysql_vsname\n" >> $next_filename
135 fi
136
137    echo -e $@ >> $next_filename
138    
139    chmod 600 $next_filename
140 }
141
142 mysql_wizard() {
143    
144    # Global variables
145    mysql_title="MySQL action wizard"
146    
147    # backup the host system or a Vserver?
148    choose_host_or_one_vserver "$mysql_title"
149    [ $? = 0 ] || return 1
150    if [ $host_or_vservers == vservers ]
151    then
152        do_mysql_vserver
153        [ $? = 0 ] || return 1
154    fi
155       
156    # backupdir
157    if [ $host_or_vservers == vservers ]
158    then
159        inputBox "$mysql_title" "Directory where to store the backups:`echo \"\n(Relative to chosen vserver's root directory)\"`" "/var/backups/mysql"
160    else
161        inputBox "$mysql_title" "Directory where to store the backups" "/var/backups/mysql"
162    fi
163    [ $? = 1 ] && return
164    mysql_backupdir="backupdir = $REPLY"
165
166    # databases
167    booleanBox "$mysql_title" "Do you want to backup all of the databases? `echo \"\n\nIf not, you'll be offered to choose individual databases to backup.\"`"
168    if [ $? = 0 ]; then
169       mysql_databases="databases = all"
170    else
171       do_mysql_databases
172       [ $? = 0 ] || return 1
173    fi
174    
175    while true; do
176       _DISABLE_HOTCOPY=
177       menuBoxHelpFile "$mysql_title" "choose a mysql authentication method:" \
178         user "change to a linux user first." \
179         password "manually specify mysql user and password." \
180         debian "use default mysql user debian-sys-maint." 
181       status=$?
182       if [ $status = 2 ]; then
183                # show help.
184           helptmp="/tmp/backupninja.help.$$"
185           cat > $helptmp <<EOF
186 To connect to mysql, backupninja must authenticate.
187 There are three possible authentication methods:
188
189 USER
190 With this method, you specify a system user. Backupninja  will
191 then become this user before running mysqldump or mysqlhotcopy.
192 The result is that ~/.my.cnf is used for authentication.
193
194 PASSWORD
195 With this method, you manually specify a mysql user and
196 password in the backup action configuration.
197
198 DEBIAN
199 With this method, we use the debian-sys-maint user which is
200 already defined in /etc/mysql/debian.cnf. If you are running
201 debian, this is recommended, because no further configuration
202 is needed. The drawback is that this is incompatible with 
203 mysqlhotcopy: you must use mysqldump.
204 EOF
205           dialog --textbox $helptmp 0 0
206           rm $helptmp
207       fi
208
209       [ $status = 1 ] && return;
210       result="$REPLY"
211       case "$result" in
212          "user") do_mysql_user;return;;
213          "password") do_mysql_password;return;;
214          "debian") do_mysql_debian;return;;
215       esac
216    done   
217 }