Bash script for backup wordpress database

I want to take a mysql database backup of my wordpress sites. So i created i bash script for this purpose but what is the best case to deal with db password?

Case1: use .my.cnf to store password for every site and call it like this

“mysqldump --defaults-file=/home/hestiacpuser/web/mydomain.com/.my.cnf -u ${DBUSER} ${DATABASE} > sqlbackup.sql”

Case2: use .my.cnf for root user and take db backups as mysql root user?

Case3: take database credentials from wp-config.php file using code like this

“WPDBPASS=”$(cat ./wp-config.php | grep DB_PASSWORD | cut -d ' -f 4)"

Case 3 is less secure but as db credentials are stored as plain text in wp-config.php file. Are there any additional risks? What if i unset the variable WPDBPASS?

Thanks

edit 1. I checked mysql root password and found that it isnt very complex. it is something like this “erkmnftsfl” (only letters)

#!/bin/bash

# Directory where the backup files will be stored
BACKUP_DIR=/path/to/backup/directory

# List of domains
DOMAINS=("domain1.com" "domain2.com" ...)

for domain in ${DOMAINS[@]}; do
    # Get the DB credentials
    DBUSER=$(grep 'DB_USER' /home/hestiacpuser/web/$domain/wp-config.php | cut -d "'" -f 4)
    DATABASE=$(grep 'DB_NAME' /home/hestiacpuser/web/$domain/wp-config.php | cut -d "'" -f 4)

    # Generate the backup
    mysqldump --defaults-file=/home/hestiacpuser/web/$domain/.my.cnf -u ${DBUSER} ${DATABASE} > $BACKUP_DIR/${DATABASE}_$(date +%Y%m%d).sql
done

So this is case1 but why do you prefer case1 than case2 or 3?

you don’t need a root to do backup