|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# dump-tables.sh |
| 4 | +# Descr: Dump MySQL table data into separate SQL files for every database. |
| 5 | +# Usage: Run without args for usage info. |
| 6 | +# Author: sysadminstory |
| 7 | +# Notes: |
| 8 | +# * Script will prompt for password for db access. |
| 9 | +# * Script will restart the MySQL service if a dump fails |
| 10 | +# * Output files are written from the directory specified on command-line. |
| 11 | + |
| 12 | +usage() |
| 13 | +{ |
| 14 | + echo "MySQL Rescue dumper dump script" |
| 15 | + echo "" |
| 16 | + echo "Usage: " |
| 17 | + echo " $(basename $0) -h <db_host> -u <db_user> -o <output_dir> -r <restart_command> [-d <delay>]" |
| 18 | + echo " $(basename $0) -h" |
| 19 | + echo "" |
| 20 | + echo "Options:" |
| 21 | + echo " -s <db_host> Database Host" |
| 22 | + echo " -u <db_user> Database Username" |
| 23 | + echo " -o <output_dir> Output Directory" |
| 24 | + echo " -r <restart_command> MySQL Service restart command" |
| 25 | + echo " -d <delay> Delay in seconds to wait after restarting MySQL Service [default: 5]" |
| 26 | + echo " -h Show this help" |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +# Clear variables |
| 31 | +unset DBHOST |
| 32 | +unset DBUSER |
| 33 | +unset DIR |
| 34 | +unset RESTART |
| 35 | +unset DELAY |
| 36 | + |
| 37 | +# Set default values |
| 38 | +DELAY=5 |
| 39 | + |
| 40 | +# Handle arguments |
| 41 | +while getopts "s:u:o:r:d:h" o; do |
| 42 | + case "${o}" in |
| 43 | + s) |
| 44 | + DBHOST=${OPTARG} |
| 45 | + ;; |
| 46 | + u) |
| 47 | + DBUSER=${OPTARG} |
| 48 | + ;; |
| 49 | + o) |
| 50 | + DIR=${OPTARG} |
| 51 | + ;; |
| 52 | + r) |
| 53 | + RESTART=${OPTARG} |
| 54 | + ;; |
| 55 | + d) |
| 56 | + DELAY=${OPTARG} |
| 57 | + ;; |
| 58 | + *|h) |
| 59 | + usage |
| 60 | + ;; |
| 61 | + esac |
| 62 | +done |
| 63 | + |
| 64 | +# Check if all parameters are set |
| 65 | +if [[ -z "$DBHOST" || -z "$DBUSER" || -z "$DIR" || -z "$RESTART" || -z "$DELAY" ]] |
| 66 | +then |
| 67 | + usage |
| 68 | +fi |
| 69 | + |
| 70 | +# If the destination directory doesn't exist, create it |
| 71 | +if [ ! -d "$DIR" ] |
| 72 | +then |
| 73 | + echo "Creating output directory '$DIR'" |
| 74 | + mkdir -p $DIR |
| 75 | +fi |
| 76 | + |
| 77 | +# Ask for the password |
| 78 | +read -p "Database Server Password :" -s DBPASS |
| 79 | + |
| 80 | +# Get the list of database excluding 'information_schema' |
| 81 | +DBS=$(mysql -B -s -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e 'show databases' | grep -v information_schema) |
| 82 | + |
| 83 | +# Init global statistics vars |
| 84 | +TOTAL_TABLE_COUNT=0 |
| 85 | +TOTAL_TABLE_COUNT_FAILED=0 |
| 86 | +TOTAL_TABLE_COUNT_SUCCESS=0 |
| 87 | +FAILED_TABLES="" |
| 88 | + |
| 89 | +for DB in $DBS |
| 90 | +do |
| 91 | + echo "Dumping tables into separate SQL files for database '$DB' into the directory '$DIR'" |
| 92 | + |
| 93 | + # Init database statistics vars |
| 94 | + TABLE_COUNT=0 |
| 95 | + TABLE_COUNT_FAILED=0 |
| 96 | + TABLE_COUNT_SUCCESS=0 |
| 97 | + |
| 98 | + # Get the list of tables |
| 99 | + TABLES=$(mysql -NBA -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -D $DB -e 'show tables') |
| 100 | + for TABLE in $TABLES |
| 101 | + do |
| 102 | + echo "Dumping Table : $DB.$TABLE" |
| 103 | + mysqldump -f -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DB $TABLE --result-file=$DIR/$DB.$TABLE.sql |
| 104 | + |
| 105 | + # If the dumps fails |
| 106 | + if [ $? -ne 0 ] |
| 107 | + then |
| 108 | + echo "Dump of table $DB.$TABLE failed !" |
| 109 | + echo "Restarting MySQL using command '$RESTART'" |
| 110 | + # Try to restart the MySQL daelon using the command provided |
| 111 | + $RESTART |
| 112 | + echo "Wating $DELAY seconds before continuing ..." |
| 113 | + sleep 10 |
| 114 | + TABLE_COUNT_FAILED=$(( TABLE_COUNT_FAILED + 1 )) |
| 115 | + FAILED_TABLES=$"$FAILED_TABLES\n$DB.$TABLE" |
| 116 | + else |
| 117 | + |
| 118 | + TABLE_COUNT_SUCCESS=$(( TABLE_COUNT_SUCCESS + 1 )) |
| 119 | + fi |
| 120 | + TABLE_COUNT=$(( TABLE_COUNT + 1 )) |
| 121 | + done |
| 122 | + |
| 123 | + echo "$TABLE_COUNT_SUCCESS/$TABLE_COUNT table(s) dumped from database '$DB' into the directory '$DIR' ($TABLE_COUNT_FAILED table(s) failed)" |
| 124 | + |
| 125 | + # Calculate global statistics |
| 126 | + TOTAL_TABLE_COUNT=$(( TOTAL_TABLE_COUNT + TABLE_COUNT )) |
| 127 | + TOTAL_TABLE_COUNT_FAILED=$(( TOTAL_TABLE_COUNT_FAILED + TABLE_COUNT_FAILED )) |
| 128 | + TOTAL_TABLE_COUNT_SUCCESS=$(( TOTAL_TABLE_COUNT_SUCCESS + TABLE_COUNT_SUCCESS )) |
| 129 | +done |
| 130 | + |
| 131 | +# Show the statistics |
| 132 | +echo "Statistics :" |
| 133 | +echo "Tried to dump $TOTAL_TABLE_COUNT table(s)" |
| 134 | +echo "$TOTAL_TABLE_COUNT_FAILED table(s) dump failed" |
| 135 | +echo "$TOTAL_TABLE_COUNT_SUCCESS table(s) dump were successful" |
| 136 | +# Show the info about the failed dumps if needed |
| 137 | +if [ "$TOTAL_TABLE_COUNT_FAILED" -gt 0 ] |
| 138 | +then |
| 139 | + echo |
| 140 | + echo "The following table dump failed :" |
| 141 | + echo -e $FAILED_TABLES |
| 142 | +fi |
0 commit comments