Created
August 24, 2024 09:57
-
-
Save NorbertFeria/b29ee42e33ff1c5ab7b395f660a55e38 to your computer and use it in GitHub Desktop.
dump sql file and place it on a tar.gz file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Notes | |
| echo "NOTES:" | |
| echo "This script will perform a MySQL dump of your database and compress the file." | |
| echo "PRESS ANY KEY TO CONTINUE" | |
| read -n 1 -s -r -p | |
| # Default values | |
| DEFAULT_DBNAME="mydatabase" | |
| DEFAULT_DBUSER="root" | |
| DEFAULT_DBPASS="1001" | |
| DEFAULT_BACKUP_DIR="backups" | |
| # Prompt for database name | |
| read -p "Type DB NAME [${DEFAULT_DBNAME}] followed by [ENTER]: " dbname | |
| dbname=${dbname:-$DEFAULT_DBNAME} | |
| # Prompt for database user | |
| read -p "Type USER [${DEFAULT_DBUSER}] followed by [ENTER]: " dbuser | |
| dbuser=${dbuser:-$DEFAULT_DBUSER} | |
| # Prompt for database password | |
| read -p "Type PASSWORD [hidden] followed by [ENTER]: " dbpass | |
| echo "" | |
| dbpass=${dbpass:-$DEFAULT_DBPASS} | |
| # Validate inputs | |
| if [ -z "$dbname" ] || [ -z "$dbuser" ] || [ -z "$dbpass" ]; then | |
| echo "Error: Database name, user, and password must not be empty." | |
| exit 1 | |
| fi | |
| # Set start time | |
| starttime=$(date "+%m-%d-%Y %H:%M:%S") | |
| # Perform MySQL dump | |
| echo "Performing MySQL dump..." | |
| mysqldump --no-tablespaces -u$dbuser -p$dbpass $dbname > $dbname.sql | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Failed to dump the database." | |
| exit 1 | |
| fi | |
| # Check if backup directory exists or create it | |
| if [ ! -d "$DEFAULT_BACKUP_DIR" ]; then | |
| echo "The backup directory '$DEFAULT_BACKUP_DIR' does not exist. Creating it now..." | |
| mkdir -p "$DEFAULT_BACKUP_DIR" | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Could not create backup directory '$DEFAULT_BACKUP_DIR'." | |
| exit 1 | |
| fi | |
| fi | |
| # Compress the SQL dump file | |
| echo "Compressing the dump file..." | |
| tar -czvf "${DEFAULT_BACKUP_DIR}/${dbname}.tar.gz" "$dbname.sql" | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Failed to compress the dump file." | |
| exit 1 | |
| fi | |
| # Remove the SQL dump file after compression | |
| rm "$dbname.sql" | |
| if [ $? -ne 0 ]; then | |
| echo "Warning: Failed to delete the SQL dump file." | |
| fi | |
| echo "Backup completed successfully at ${DEFAULT_BACKUP_DIR}/${dbname}.tar.gz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment