Created
January 7, 2022 22:00
-
-
Save nadyshalaby/d4a61cb48e5ad3e01968bb4e4fc5caae to your computer and use it in GitHub Desktop.
Shell script to ease the database backup/restore process (Data Only)
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/sh | |
| # This script is used to as a seeder for the database. | |
| # It will dump a database data only or populate it. | |
| # Show a drawing of word "MySQL Dumper Script by @nadyshalaby" on the screen. | |
| echo " | |
| _____________________________________ | |
| < MySQL Dumper Script by @nadyshalaby > | |
| ------------------------------------- | |
| \ | |
| \ | |
| _____ _________ | |
| / \_/ | | |
| | || | |
| | || | |
| | ###\ /### | | | |
| | 0 \/ 0 | | | |
| /| | | | |
| / | < |\ \ | |
| | /| | | | | |
| | | \_______/ | | | | |
| | | | / / | |
| /|| /||| | |
| ----------------| | |
| | | | | | |
| *** *** | |
| /___\ /___\ | |
| " | |
| # Show a welcome message to the user and ask for the database name. | |
| echo "Welcome to the database seeder." | |
| echo "Please enter the database name you want to use." | |
| read -p "Database name: " dbname | |
| # Check if the database name is empty. | |
| if [ -z "$dbname" ]; then | |
| echo "The database name is empty." | |
| exit 1 | |
| fi | |
| user=root | |
| password=root | |
| host=localhost | |
| # Show the default values for the user, password and host. | |
| echo "The default values are: " | |
| echo "user: $user" | |
| echo "password: $password" | |
| echo "host: $host" | |
| # Ask the user if he wants to change the user, password or host. | |
| echo "Do you want to change the user, password or host?" | |
| read -p "Change user, password or host? (y/n): " change | |
| # Ask the user for the user, password and host. | |
| if [ $change = "y" ]; then | |
| echo "Please enter the user." | |
| read -p "User: " user | |
| echo "Please enter the password." | |
| read -p "Password: " password | |
| echo "Please enter the host." | |
| read -p "Host: " host | |
| fi | |
| # Check if the user, password or host is empty. | |
| if [ -z "$user" ] || [ -z "$password" ] || [ -z "$host" ]; then | |
| echo "The user, password or host should not be empty." | |
| exit 1 | |
| fi | |
| db_passwd=$password | |
| db_name=$dbname | |
| db_user=$user | |
| db_host=$host | |
| # Create 'dump' directory if it doesn't exist | |
| if [ ! -d dump ]; then | |
| mkdir dump | |
| fi | |
| # Check the user input if he wants to dump the database or to restore it | |
| echo "Do you want to dump the database (data only) or restore it? (d/r)" | |
| read answer | |
| # Date and time for the file name | |
| date=$(date +%Y-%m-%d_%H-%M-%S) | |
| # If the user wants to dump the database | |
| if [ $answer = 'd' ]; then | |
| # Dump the database | |
| mysqldump -u $db_user -p$db_passwd -h $db_host --skip-triggers --compact --no-create-info $db_name >"dump/$db_name-$date.sql" | |
| # Check if the dump was successful | |
| if [ $? -eq 0 ]; then | |
| echo "Database dump successful" | |
| else | |
| echo "Database dump failed" | |
| fi | |
| # If the user wants to restore the database | |
| elif [ $answer = 'r' ]; then | |
| # Ask the user to choose a file to restore from the 'dump' directory | |
| echo "Please choose a file to restore from the 'dump' directory." | |
| # List all files in the 'dump' directory | |
| ls dump | |
| # Ask the user for the file name | |
| echo "Please enter the file name you want to restore without (.sql) extension." | |
| read -p "File name: " file | |
| # Check if the file exists | |
| if [ -f "dump/$file.sql" ]; then | |
| # Restore the database | |
| mysql -u $db_user -p$db_passwd -h $db_host $db_name <"dump/$file.sql" | |
| # Check if the restore was successful | |
| if [ $? -eq 0 ]; then | |
| echo "Database restore successful" | |
| else | |
| echo "Database restore failed" | |
| fi | |
| else | |
| echo "File not found" | |
| fi | |
| else | |
| echo "Invalid input" | |
| fi | |
| # End of script | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment