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