Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- MYSQL_SERVER=""
- MYSQL_USER=""
- MYSQL_PASS=""
- MYSQL_DATABASE=""
- TABLES=""
- if [[ $* == *-h* ]]; then
- echo -e "\nUsage:"
- echo -e "\n\tdbdown.sh [-c] [table] [table-2] ... [table-n]\n"
- echo -e " -c\tCompresses resulting dump, using tar and gzip"
- echo -e " If no arguments are specified, performs full database dump"
- echo -e " To dump only select tables, specify their names as descibed above"
- echo -e "\n"
- exit 0
- fi
- if [ -z "$1" ]; then
- echo "Fetching full database dump..."
- FILE_NAME="$MYSQL_DATABASE.sql"
- else
- for table in "$@"
- do
- if ! [[ $table =~ ^-.*$ ]]; then
- TABLES="$TABLES$table "
- fi
- done
- TABLES="$(echo -e "${TABLES}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
- if [ "$#" -gt 2 ]; then
- FILE_NAME="${MYSQL_DATABASE}_multi.sql"
- else
- UNDERSCORED="$(echo -e "${TABLES}" | sed -e 's/[[:space:]]/_/')"
- FILE_NAME="${MYSQL_DATABASE}_${UNDERSCORED}.sql"
- fi
- echo "Fetching databse dump for tables: $TABLES"
- fi
- mysqldump -C -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_SERVER $MYSQL_DATABASE $TABLES>$FILE_NAME
- if [[ $* == *-c* ]]; then
- echo "Compressing..."
- tar czvf $MYSQL_DATABASE.tar.gz $FILE_NAME
- fi
Add Comment
Please, Sign In to add comment