Guest User

Untitled

a guest
May 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #!/bin/bash
  2. MYSQL_SERVER=""
  3. MYSQL_USER=""
  4. MYSQL_PASS=""
  5. MYSQL_DATABASE=""
  6. TABLES=""
  7. if [[ $* == *-h* ]]; then
  8. echo -e "\nUsage:"
  9. echo -e "\n\tdbdown.sh [-c] [table] [table-2] ... [table-n]\n"
  10. echo -e " -c\tCompresses resulting dump, using tar and gzip"
  11. echo -e " If no arguments are specified, performs full database dump"
  12. echo -e " To dump only select tables, specify their names as descibed above"
  13. echo -e "\n"
  14. exit 0
  15. fi
  16. if [ -z "$1" ]; then
  17. echo "Fetching full database dump..."
  18. FILE_NAME="$MYSQL_DATABASE.sql"
  19. else
  20. for table in "$@"
  21. do
  22. if ! [[ $table =~ ^-.*$ ]]; then
  23. TABLES="$TABLES$table "
  24. fi
  25. done
  26. TABLES="$(echo -e "${TABLES}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  27. if [ "$#" -gt 2 ]; then
  28. FILE_NAME="${MYSQL_DATABASE}_multi.sql"
  29. else
  30. UNDERSCORED="$(echo -e "${TABLES}" | sed -e 's/[[:space:]]/_/')"
  31. FILE_NAME="${MYSQL_DATABASE}_${UNDERSCORED}.sql"
  32. fi
  33. echo "Fetching databse dump for tables: $TABLES"
  34. fi
  35. mysqldump -C -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_SERVER $MYSQL_DATABASE $TABLES>$FILE_NAME
  36. if [[ $* == *-c* ]]; then
  37. echo "Compressing..."
  38. tar czvf $MYSQL_DATABASE.tar.gz $FILE_NAME
  39. fi
Add Comment
Please, Sign In to add comment