Showing posts with label Mysql Backup of multiple Database and Extarct one DB backup form backup file. Show all posts
Showing posts with label Mysql Backup of multiple Database and Extarct one DB backup form backup file. Show all posts

Saturday, September 2, 2023

Mysql Backup of multiple Database and Extarct one DB backup form backup file





Use the'mysqldump' command to export a copy of a selected number of your MySQL databases. This is how you do it:

1. Open a terminal, commonly referred to as a command prompt.

2.'mysqldump' to Dump Selected Databases:


   You can specify multiple databases with the'mysqldump' command by listing them one after the other. 'username' should be replaced with your MySQL username, 'password' with your MySQL password (you'll be required to provide it), and 'db1', 'db2', etc. should be replaced with the names of the databases you wish to dump:

mysqldump -u username -p --databases db1 db2 > selected_databases.sql


This command will create a file named `selected_databases.sql` containing the SQL dump of the specified databases. If you want to dump all databases on the server, you can use the `--all-databases` option:

   mysqldump -u username -p --all-databases > all_databases.sql

3. Enter Password: When you run the command, you'll be prompted to enter your MySQL password for authentication.

4. Verify the Dump: You can verify the dump by checking the `selected_databases.sql` file in your current directory. It should contain the SQL statements for the selected databases.

Make sure that the MySQL user you are using has the necessary privileges to access and create dumps for the selected databases. Additionally, it's essential to keep the generated SQL dump file in a secure location as it may contain sensitive information.

Please replace `username`, `password`, `db1`, `db2`, and the output file name (`selected_databases.sql`) with your specific details and preferences.



To extract one specific database from a multi-database SQL backup file using the `sed` command, you can use the following approach. This method assumes that the database's SQL dump is separated by delimiter statements (`CREATE DATABASE` and `USE` statements) and that the database name is known. 


1. Identify the Database Name: Determine the name of the specific database you want to extract from the backup file. You'll need this name for the `sed` command.


2. Use `sed` to Extract the Database: You can use `sed` to filter out the SQL statements related to the database you want to extract. Replace `your_database_name` with the actual name of the database you want to extract and `backup_file.sql` with the name of your backup file:


   sed -n '/^-- Current Database: `your_database_name`/,/^-- Current Database: `/p' backup_file.sql > extracted_database.sql

 This command uses `sed` to search for lines between `-- Current Database: 'your_database_name'` and the next `-- Current Database: '...'` line in the `backup_file.sql`. It then writes the extracted SQL to a new file called `extracted_database.sql`.

3. Verify the Extracted Database: You can now verify the `extracted_database.sql` file to ensure that it contains the SQL statements for the specific database.

Please note that this method assumes that the SQL dump in the backup file is structured with `CREATE DATABASE` and `USE` statements to switch between databases. The effectiveness of this method may depend on how the backup file was originally created. Always make sure you have a backup of your original backup file before manipulating it with `sed` to avoid data loss.