Could someone please point out what I’m doing wrong to complete this task? I continue to get the failed task error message:
Seems like ‘/opt/scripts/database.sh’ script execution failed or print messages are incorrect.
When I execute the script, it outputs that the database already exists, and that the import was completed.
Here’s my script:
#!/bin/bash
DB_NAME="kodekloud_db01"
BACKUP_DIR="/opt/db_backups"
BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}.sql"
DB_DUMP="${BACKUP_DIR}/db.sql"
USER_NAME="kodekloud_roy"
USER_PASSWORD="asdfgdsd"
# a. Check if database exists
mysql -u root -e "USE ${DB_NAME};" &>/dev/null
if [[ $? -eq 0 ]]; then
echo "Database already exists"
# b. Check if database has tables
data_table_results=$(mysql -u root -N "${DB_NAME}" -e "SHOW TABLES;")
if [[ -n "$data_table_results" ]]; then
echo "database is not empty"
else
mysql -u root "${DB_NAME}" < "${DB_DUMP}"
echo "imported database dump into ${DB_NAME} database"
fi
else
mysql -u root -e "CREATE DATABASE \`${DB_NAME}\`;"
echo "Database ${DB_NAME} has been created"
fi
# a. Create user with wildcard host and grant access on the database only
mysql -u root -e "CREATE USER IF NOT EXISTS '${USER_NAME}'@'%' IDENTIFIED BY '${USER_PASSWORD}';"
mysql -u root -e "GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${USER_NAME}'@'%';"
mysql -u root -e "FLUSH PRIVILEGES;"
# c. Take a MySQL dump
mysqldump -u root "${DB_NAME}" > "${BACKUP_FILE}"
IMPORTANT: The instructions for the task are poorly formatted, and it’s not clear what the task check is actually evaluating. I’ve seen other posts mentioning formatting (uppercase vs lowercase), but the echo in my script match what the instructions request as outputs.
Thanks



