Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Restoring MySQL Backups From Command Line

Discussion in 'Database Discussion' started by Pandora, Jul 27, 2013.

  1. Pandora

    Pandora Registered

    Jul 24, 2013
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator

    I have the following script to restore a backup from a dump. Syntax for executing this script might be:

    ./ 07-24-13 july24
    where 07-24-13 is part of the backup.sql.gz file name and july24 is the MySQL DB to be created to hold the data

    Here's the script:

    # Database name
    # Does the backup source even exist?
    if [ -f $FILE ];
       echo "Backup source found."
       echo "Backup file not found.  Please try again."
       exit 1
    # Find the backup file based on the args
    echo "Creating database ${DBNAME}..."
    mysql -u root -pPASS -e "create database if not exists ${DBNAME};"
    # Do the restoration
    echo "Restoring database..."
    echo "Please wait..."
    gunzip < $FILE | mysql -u root -pPASS ${DBNAME}
    echo Done!
    What I am having problems with is GRANT permissions for the database, to get this manually-created database to show up in cPanel and/or phpMyAdmin. I don't really want to create a global MySQL root user if I don't have to for security purposes.

    What are the series of GRANT queries that I need to run to make this database accessible to a user? Let me know what the best solution is.

    Thanks much.
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Apr 11, 2011
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    Hello :)

    You can utilize the database mapping utility to map the database to an account. EX:

    root@vps [~]# /usr/local/cpanel/bin/dbmaptool
    /usr/local/cpanel/bin/dbmaptool cpuser --type mysql|pg --dbusers 'user1, user2' --dbs 'db1, db2'
               These flags are treated individually. This tool will not map a virtual user to a database.
               /usr/local/cpanel/bin/dbmaptool user1 --type mysql --dbs 'db1' --dbusers 'virt1'
               /usr/local/cpanel/bin/dbmaptool user1 --type mysql --dbs 'db1' && /usr/local/cpanel/bin/dbmaptool user1 --type mysql --dbusers 'virt1'
               The above two commands are analogous; result in cpuser 'user1' having privileges for 'db1' and having a virtual user named 'virt1.
    Thank you.
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice