Skip to content
Snippets Groups Projects
mysql.helper.in 6.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
    
    # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
    
    HELPERS="$HELPERS mysql:mysql_database_backup"
    
    elijah's avatar
    elijah committed
    
    
    do_mysql_vserver() {
       choose_one_vserver "$mysql_title"
       [ $? = 0 ] || return 1
       mysql_vsname="vsname = $REPLY"
    }
    
    do_mysql_databases() {
       REPLY=
       while [ -z "$REPLY" ]; do
          formBegin "$mysql_title: databases"
             formItem "Database:"
    
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
             formItem "Database:"
    
          formDisplay
          [ $? = 0 ] || return 1
          mysql_databases="databases = "
          for i in $REPLY; do
    
             [ -n "$i" ] && mysql_databases="$mysql_databases $i"
    
    elijah's avatar
    elijah committed
    }
    
    do_mysql_password() {
    
       inputBox "$mysql_title" "specify a mysql user:"
       [ $? = 1 ] && return
       user=$REPLY
       inputBox "$mysql_title" "specify the mysql user's password:"
       [ $? = 1 ] && return
       password=$REPLY
       do_mysql_final "dbusername = $user\ndbpassword = $password"
    
    elijah's avatar
    elijah committed
    }
    
    do_mysql_debian() {
    
       _DISABLE_HOTCOPY=yes
       do_mysql_final "configfile = /etc/mysql/debian.cnf"
    
    elijah's avatar
    elijah committed
    }
    
    do_mysql_user() {
    
       inputBox "$mysql_title" "what system user does mysql backup use?"
       [ $? = 1 ] && return
       do_mysql_final "user = $REPLY"
    
    elijah's avatar
    elijah committed
    }
    
    do_mysql_final() {
       if [ -z "$_DISABLE_HOTCOPY" ]; then
    
          checkBox "$mysql_title" "check options" \
    
             "sqldump" "create a backup using mysqldump (more compat)." no \
             "hotcopy" "create a backup using mysqlhotcopy (faster)." yes \
             "compress" "compress the sql output files" yes
    
    elijah's avatar
    elijah committed
          status=$?
    
          sqldump="sqldump = no"
          hotcopy="hotcopy = no"
    
    elijah's avatar
    elijah committed
       else
    
          checkBox "$mysql_title" "check options" \
    
             "compress" "compress the sql output files" yes
    
    elijah's avatar
    elijah committed
          status=$?
    
          sqldump="sqldump = yes"
          hotcopy="hotcopy = no"
    
    elijah's avatar
    elijah committed
       fi
    
    
       [ $status = 1 ] && return;
    
    elijah's avatar
    elijah committed
       result="$REPLY"
    
    elijah's avatar
    elijah committed
       for opt in $result; do
          case $opt in
    
             '"sqldump"') sqldump="sqldump = yes";;
             '"hotcopy"') hotcopy="hotcopy = yes";;
             '"compress"') compress="compress = yes";;
    
    elijah's avatar
    elijah committed
          esac
       done
       get_next_filename $configdirectory/20.mysql
    
    elijah's avatar
    elijah committed
       cat >> $next_filename <<EOF
    
    ### backupninja MySQL config file ###
    
    # hotcopy = < yes | no > (default = no)
    # make a backup of the actual database binary files using mysqlhotcopy.
    
    elijah's avatar
    elijah committed
    $hotcopy
    
    
    # sqldump = < yes | no > (default = no)
    # make a backup using mysqldump. this creates text files with sql commands
    # sufficient to recontruct the database.
    #
    $sqldump
    
    
    # sqldumpoptions = <options>
    # (default = --lock-tables --complete-insert --add-drop-table --quick --quote-names)
    # arguments to pass to mysqldump
    # sqldumpoptions = --add-drop-table --quick --quote-names
    
    
    # compress = < yes | no > (default = yes)
    # if yes, compress the sqldump output.
    
    elijah's avatar
    elijah committed
    $compress
    
    
    # dbhost      = <host> (default = localhost)
    
    EOF
       cat >> $next_filename <<EOF
    
    # backupdir = <dir> (default: /var/backups/mysql)
    # where to dump the backups. hotcopy backups will be in a subdirectory
    # 'hotcopy' and sqldump backups will be in a subdirectory 'sqldump'
    $mysql_backupdir
    
    # databases = <all | db1 db2 db3 > (default = all)
    
    # which databases to backup. should either be the word 'all' or a
    
    # space separated list of database names.
    $mysql_databases
    
    EOF
    
    if [ $host_or_vservers == vservers ]
       then
    
          cat >> $next_filename <<EOF
    
    #
    # vsname = <vserver> (no default)
    
    # vsname indicates which vserver to operate on, this is only used if
    
    # vserver is set to yes in /etc/backupninja.conf
    # NOTE: if you do not specify a vsname the host will be operated on
    
    # alsoNOTE: if operating on a vserver, $VROOTDIR will be
    
    # prepended to backupdir.
    
    elijah's avatar
    elijah committed
    EOF
    
       echo -e "$mysql_vsname\n" >> $next_filename
    fi
    
       echo -e $@ >> $next_filename
    
    elijah's avatar
    elijah committed
    }
    
    mysql_wizard() {
    
       # Global variables
       mysql_title="MySQL action wizard"
    
       # backup the host system or a Vserver?
       choose_host_or_one_vserver "$mysql_title"
       [ $? = 0 ] || return 1
       if [ $host_or_vservers == vservers ]
       then
    
          do_mysql_vserver
          [ $? = 0 ] || return 1
    
       # backupdir
       if [ $host_or_vservers == vservers ]
       then
    
          inputBox "$mysql_title" "Directory where to store the backups:`echo \"\n(Relative to chosen vserver's root directory)\"`" "/var/backups/mysql"
    
          inputBox "$mysql_title" "Directory where to store the backups" "/var/backups/mysql"
    
       fi
       [ $? = 1 ] && return
       mysql_backupdir="backupdir = $REPLY"
    
       # databases
       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.\"`"
       if [ $? = 0 ]; then
          mysql_databases="databases = all"
       else
          do_mysql_databases
          [ $? = 0 ] || return 1
       fi
    
    elijah's avatar
    elijah committed
       while true; do
          _DISABLE_HOTCOPY=
    
          menuBoxHelpFile "$mysql_title" "choose a mysql authentication method:" \
    
             user "change to a linux user first." \
             password "manually specify mysql user and password." \
             debian "use default mysql user debian-sys-maint."
    
    elijah's avatar
    elijah committed
          status=$?
          if [ $status = 2 ]; then
    
             # show help.
             helptmp="/tmp/backupninja.help.$$"
             cat > $helptmp <<EOF
    
    elijah's avatar
    elijah committed
    To connect to mysql, backupninja must authenticate.
    There are three possible authentication methods:
    
    USER
    
    With this method, you specify a system user. Backupninja will
    
    elijah's avatar
    elijah committed
    then become this user before running mysqldump or mysqlhotcopy.
    The result is that ~/.my.cnf is used for authentication.
    
    PASSWORD
    With this method, you manually specify a mysql user and
    password in the backup action configuration.
    
    DEBIAN
    With this method, we use the debian-sys-maint user which is
    already defined in /etc/mysql/debian.cnf. If you are running
    debian, this is recommended, because no further configuration
    
    is needed. The drawback is that this is incompatible with
    
    elijah's avatar
    elijah committed
    mysqlhotcopy: you must use mysqldump.
    EOF
    
             dialog --textbox $helptmp 0 0
             rm $helptmp
    
    elijah's avatar
    elijah committed
          fi
    
          [ $status = 1 ] && return;
          result="$REPLY"
          case "$result" in
             "user") do_mysql_user;return;;
             "password") do_mysql_password;return;;
             "debian") do_mysql_debian;return;;
          esac