Return to Snippet

Revision: 55842
at February 25, 2012 00:12 by ferro


Updated Code
#!/bin/bash
# - To restore the database you can use the command bellow which whill return the file
# whithout send any output to the screen 
# mysql -h 127.0.0.1 -A -u root -e "USE db_name; \. databasefile.sql"

PROGNAME=${0##*/}
PROGVERSION=0.9.1

usage()
{
  cat << EO
        Usage: $PROGNAME --database DBNAME --host HOSTIP_OR_HOSTNAME --user USERNAME [options]
               $PROGNAME --version
               $PROGNAME --help

        Make a non consistent backup
      
        Options:
EO
  cat <<EO | column -s\& -t

        -h|--help & show this output
        -V|-v|--version & show version information
        -B|--database & indicates the database name
        -h|--host & indicates the database server hostname
        -u|--user & Optional param, passes the login user name for the database server
        -p|--pwd & Optional param, sets the password for the user
        --tables & Optional param, if you whishes to dump only some tables use this parameter and passes the 
        & tables names whithin a ' (simple coma) string like "--tables 'tab1 tab2'" or "--tables 'tab1'"


EO
}

ARGS=$(getopt -s bash --options vVB:h:u:p: --longoptions help,version,database:,host:,user:,pwd:,tables: -q -- "$@" )

eval set -- "$ARGS"

while true; do
   case $1 in
      --help)
         usage
         exit 0
         ;;
      -v|-V|--version)
         echo "easydump version: $PROGVERSION"
         exit
         ;;
      -B|--database)
         shift
         DATABASE="$1"
         ;;
      -h|--host)
         shift
         HOST="$1"
         ;;
      -u|--user)
         shift
         USERNAME="$1"
         ;;
      -p|--pwd)
        shift
        PASSWORD="$1"
        ;;
      --tables)
        shift
        TABLES="--tables $1"
        ;;
      --)
         shift
         break
         ;;
      *)
         shift
         break
         ;;
   esac
   shift

done

if [ -z "$DATABASE" ]
then
        usage
        echo " IMPORTANTE: \"database\" parameter missing"
        echo ""
        exit
fi
if [ -z "$HOST" ]
then
        usage
        echo " IMPORTANTE: \"host\" parameter missing"
        echo ""
        exit
fi
if [ -z "$USERNAME" ]
then
        usage
        echo " IMPORTANTE: \"username\" parameter missing"
        echo ""
        exit
fi

/usr/bin/mysqldump \
        --databases $DATABASE \
        --host=$HOST \
        --user=$USERNAME \
        --password=$PASSWORD \
        --log-error=/tmp/mysqldump\-erros.list `# registra os erros do aplicativo em um log para avaliação posterior` \
        --force `# continua o backup mesmo que encontre erros no meio do caminho` \
        --no-create-db `# não criar banco de dados` \
        --skip-lock-tables `# nao travar a tabela (gera um backup não consistente)` \
        --add-drop-table `# adiciona um comando drop table no inicio do script resultante` \
        --skip-comments `#evita adicinar informações extras como nome do servidor, versão do mysqldump e etc` \
        --create-options `# adiciona todas as opções do create table` \
        --disable-keys  `# desliga chaves no retorno do backup deixando o restore mais rapido` \
        --quick `#com isto o mysql não gera um buffer e depois faça o stream deste, ele faz um stream de linha a linha` \
        --extended-insert `# Resulta em um arquivo menor e mais rápido para carga sendo um insert extendido (mais de uma linha da tabela inserida por comando insert)` \
        --hex-blob `#transforma valores binarios em hexa (for example, 'abc' becomes 0x616263)` \
        --set-charset  $TABLES

Revision: 55841
at February 24, 2012 04:20 by ferro


Initial Code
#!/bin/bash
# - To restore the database you can use the command bellow which whill return the file
# whithout send any output to the screen 
# mysql -h 127.0.0.1 -A -u root -e "USE db_name; \. databasefile.sql"

PROGNAME=${0##*/}
PROGVERSION=0.9.0

usage()
{
  cat << EO
        Usage: $PROGNAME [options]

        Make a non consistent backup
      
        Options:
EO
  cat <<EO | column -s\& -t

        -h|--help & show this output
        -V|-v|--version & show version information
        -B|--database & indicates the database name
        -h|--host & indicates the database server hostname
        -u|--user & passes the login user name for the database server
        -p|--pwd & sets the password for the user
        --tables & if you whishes to dump only some tables use this parameter and passes the 
        & tables names whithin a ' (simple coma) string like "--tables 'tab1 tab2'" or "--tables 'tab1'"
        
EO
}

ARGS=$(getopt -s bash --options vVB:h:u:p: --longoptions help,version,database:,host:,user:,pwd:,tables: -q -- "$@" )

eval set -- "$ARGS"

while true; do
   case $1 in
      --help)
         usage
         exit 0
         ;;
      -v|-V|--version)
         echo "easydump version: $PROGVERSION"
         exit
         ;;
      -B|--database)
         shift
         DATABASE="$1"
         ;;
      -h|--host)
         shift
         HOST="$1"
         ;;
      -u|--user)
         shift
         USERNAME="$1"
         ;;
      -p|--pwd)
        shift
        PASSWORD="-p '$1'"
        ;;
      --tables)
        shift
        TABLES="--tables $1"
        ;;
      --)
         shift
         break
         ;;
      *)
         shift
         break
         ;;
   esac
   shift
done


if [ -z "$DATABASE" ]
then
        echo "database parameter missing"
        exit
fi
if [ -z "$HOST" ]
then
        echo "host parameter missing"
        exit
fi
if [ -z "$USERNAME" ]
then
        echo "username parameter missing"
        exit
fi

echo "DB:$DATABASE host:$HOST user:$USERNAME pwd:$PASSWORD -- $TABLES"
exit

mysqldump \
        --database $DATABASE \
        --host $HOST \
        --user $USERNAME $PASSWORD \
        --log-error=./erros.list `# registra os erros do aplicativo em um log para avaliação posterior` \
        --force `# continua o backup mesmo que encontre erros no meio do caminho` \
        --no-create-db `# não criar banco de dados` \
        --skip-lock-tables `# nao travar a tabela (gera um backup não consistente)` \
        --add-drop-table `# adiciona um comando drop table no inicio do script resultante` \
        --skip-comments `#evita adicinar informações extras como nome do servidor, versão do mysqldump e etc` \
        --create-options `# adiciona todas as opções do create table` \
        --disable-keys  `# desliga chaves no retorno do backup deixando o restore mais rapido` \
        --quick `#com isto o mysql não gera um buffer e depois faça o stream deste, ele faz um stream de linha a linha` \
        --extended-insert `# Resulta em um arquivo menor e mais rápido para carga sendo um insert extendido (mais de uma linha da tabela inserida por comando insert)` \
        --hex-blob `#transforma valores binarios em hexa (for example, 'abc' becomes 0x616263)` \
        --set-charset  $TABLES

Initial URL


Initial Description
Use it with MyISAM databases.
Guess this works with InnoDB, didn't tried tough
New version 2011-02-24 11:12

Initial Title
easydump script: Make a non consistent backup

Initial Tags


Initial Language
Bash