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