mysqldupm can be a very handy tool, and you will see it while working on this task. You will deal with 3 databases, all hosted on the same server:
ShopDBis a production database for online shopping. This is the database with real user data.ShopDBReserveis a copy of the production database used for fast disaster recovery.ShopDBDevelopmentis a database used by the development team.
In this task, develop an automated procedure that runs every night. The procedure consists of 2 activities:
- Creating a full database backup from the
ShopDBdatabase and restoring it to theShopDBReservefor fast disaster recovery. - Moving data from the production database
ShopDBto theShopDBDevelopment(database schema should not be copied from production to development environment).
The activities will be executed on a schedule as a cron job, and you need to write a bash script, which a job will use.
- Install and configure a MySQL database server on a Virtual Machine.
- Fork this repository.
Write a bash script in the task.sh file. The script should implement a procedure for a backup and restoration of a database, described below:
-
Read database username and password from environment variables
DB_USER and DB_PASSWORD. -
Create a full backup of the database
ShopDBusingmysqldump, and restore it to the databaseShopDBReserve. -
Create a data backup from the database
ShopDB, and restore it to the databaseShopDBDevelopment.
To test this script, you will need to do some preparations:
-
Connect to your database server and drop the database
ShopDBif you have it already. -
Create databases, described in the task, using the
create-database.sqlscript. -
Create a database user which has just enough permissions to complete the required actions:
CREATE USER 'backup'@'%' IDENTIFIED BY '<password>';
-- configure minimum requored permissions for performing a db backup using mysqldump:
GRANT SELECT, LOCK TABLES, SHOW VIEW, PROCESS ON *.* TO 'backup'@'%';
-- configure required permissions to restore a database
GRANT ALL ON ShopDBReserve.* TO 'backup'@'%';
-- configure required permissions to restore only data
GRANT INSERT, LOCK TABLES, ALTER ON ShopDBDevelopment.* TO 'backup'@'%';
- Connect to your database server using SSH and export environment variables with database credentials:
export DB_USER="backup"
export DB_PASSWORD="<db-password>"
-
Run your script without arguments.
-
Query the data in the databases: the
Productstable should be present in both theShopDBReserveandShopDBDevelopmentdatabases, and it should have the same amount of records as in theShopDBdatabase.