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