Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.11 KB | None | 0 0
  1. #!/bin/bash
  2. # Daniel Verner
  3. # CarrotPlant LLC
  4. # 2011
  5. # Backup each mysql databases into a different file, rather than one big file
  6. # Optionally files can be gzipped (dbname.gz)
  7. #
  8. # Usage: dump_all_databases [ -u username -o output_dir -z ]
  9. #
  10. #       -u username to connect mysql server
  11. #       -o [output_dir] optional the output directory where to put the files
  12. #       -z gzip enabled
  13. #
  14. # Note: The script will prompt for a password, you cannot specify it as command line argument for security reasons
  15. #
  16. # based on the solution from: sonia 16-nov-05 (http://soniahamilton.wordpress.com/2005/11/16/backup-multiple-databases-into-separate-files/)
  17.  
  18.  
  19. PROG_NAME=$(basename $0)
  20. USER="root"
  21. PASSWORD="not the real password"
  22. filename=$(date +%Y-%m-%d-%H)
  23. OUTPUTFILE=${PWD}
  24. OUTPUTDIR="/tmp/dbdump$filename"
  25. GZIP_ENABLED=0
  26. GZIP=""
  27.  
  28. MYSQLDUMP="/usr/bin/mysqldump"
  29. MYSQL="/usr/bin/mysql"
  30.  
  31. umask 007
  32.  
  33.  
  34. while getopts u:o:z OPTION
  35. do
  36.     case ${OPTION} in
  37.         u) USER=${OPTARG};;
  38.         o) OUTPUTFILE=${OPTARG};;
  39.         z) GZIP_ENABLED=1;;
  40.         ?) echo "Usage: ${PROG_NAME} [ -u username -o output_file -z ]"
  41.            exit 2;;
  42.     esac
  43. done
  44.  
  45. if [ "$USER" != '' ] && [ "$PASSWORD" == '' ]; then
  46.  
  47. echo "Enter password for" $USER":"
  48. oldmodes=`stty -g`
  49. stty -echo
  50. read PASSWORD
  51. stty $oldmodes
  52.  
  53. fi
  54.  
  55. if [ ! -d "$OUTPUTDIR" ]; then
  56.     mkdir -p $OUTPUTDIR
  57. fi
  58.  
  59. # get a list of databases
  60. databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
  61.  
  62. # dump each database in turn
  63. for db in $databases; do
  64. #    echo $db
  65.         if [ $GZIP_ENABLED == 1 ]; then
  66.                 $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db | gzip -9 > "$OUTPUTDIR/$db.sql.gz"
  67.         else
  68.             $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db > "$OUTPUTDIR/$db.sql"
  69.         fi
  70. done
  71.  
  72. cd "$OUTPUTDIR"
  73.  
  74. if [ $GZIP_ENABLED == 1 ]; then
  75.         tar -cf "$filename.tar" *.sql.gz
  76.         rm -f *.sql.gz
  77. else
  78.         tar -cf "$filename.tar" *.sql
  79.         rm -f *.sql
  80. fi
  81. mv "$filename.tar" "$OUTPUTFILE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement