└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Database Backup 2 | The **mysqldump** is a command-line utility in MySQL used for creating backups of MySQL databases. 3 | The **mysqldump** tool allows you to dump the structure and/or data of one or more databases into a file, which you can use to restore the databases later. 4 | In practice, you often use the **mysqldump** for **backup** and **restore** operations, **database migration**, and **transferring databases** between servers. 5 | 6 | ## Commands 7 | 8 | Creating a backup of a single database 9 | 10 | ```sql 11 | mysqldump -u username -p db_name > path_to_backup_file 12 | ``` 13 | 14 | Creating a backup of all databases 15 | 16 | ```sql 17 | mysqldump -u username -p -A > path_to_backup_file 18 | ``` 19 | 20 | Creating a backup of data only 21 | 22 | ```bash 23 | mysqldump -u username -p -t db_name > path_to_backup_file 24 | ``` 25 | 26 | ## How to Back Up and Restore a Database in MySQL 27 | 28 | ```sql 29 | CREATE DATABASE hr; 30 | USE hr; 31 | CREATE TABLE IF NOT EXISTS employees ( 32 | id INT AUTO_INCREMENT PRIMARY KEY, 33 | name VARCHAR(255) NOT NULL, 34 | email VARCHAR(255) NOT NULL UNIQUE 35 | ); 36 | INSERT INTO employees (name, email) 37 | VALUES 38 | ('John Doe', 'john.doe@example.com'), 39 | ('Jane Smith', 'jane.smith@example.com'), 40 | ('Bob Johnson', 'bob.johnson@example.com'), 41 | ('Alice Jones', 'alice.jones@example.com'), 42 | ('Charlie Brown', 'charlie.brown@example.com'); 43 | ``` 44 | ### Backing up a database 45 | ```sql 46 | mysqldump -u root -p hr > hr.sql 47 | ``` 48 | Accidentally deleting some rows from a table 49 | ```sql 50 | ???????????????????????????????? 51 | ``` 52 | ## Restoring a database 53 | ```sql 54 | mysql -u root -p hr < hr.sql 55 | ``` 56 | ### Retrieve data from the employees table to verify the restoration 57 | ```sql 58 | USE hr; 59 | SELECT * FROM employees; 60 | ``` 61 | ## How to Back Up and Restore All Databases in MySQL 62 | #### Create sample database 63 | ```sql 64 | CREATE DATABASE sampledb1; 65 | CREATE DATABASE sampledb2; 66 | CREATE DATABASE sampledb3; 67 | ``` 68 | ## Backing up all databases 69 | ```sql 70 | mysqldump -u root -p --all-databases > all_databases.sql 71 | ``` 72 | ## Accidentally removing a database 73 | ```sql 74 | drop database sampledb1; 75 | drop database sampledb2; 76 | ``` 77 | ### Restoring all databases 78 | ```sql 79 | mysql -u root -p < all_databases.sql 80 | ``` 81 | ## Backing Up MySQL Table 82 | ### Create sample database 83 | ```sql 84 | CREATE DATABASE sales; 85 | USE sales; 86 | CREATE TABLE products ( 87 | product_id INT AUTO_INCREMENT PRIMARY KEY, 88 | product_name VARCHAR(255), 89 | unit_price DECIMAL(10, 2) 90 | ); 91 | 92 | CREATE TABLE customers ( 93 | customer_id INT AUTO_INCREMENT PRIMARY KEY, 94 | customer_name VARCHAR(255), 95 | email VARCHAR(255) 96 | ); 97 | 98 | CREATE TABLE orders ( 99 | order_id INT AUTO_INCREMENT PRIMARY KEY, 100 | customer_id INT, 101 | order_date DATE, 102 | FOREIGN KEY (customer_id) REFERENCES customers(customer_id) 103 | ); 104 | 105 | CREATE TABLE order_details ( 106 | order_detail_id INT AUTO_INCREMENT PRIMARY KEY, 107 | order_id INT, 108 | product_id INT, 109 | quantity INT, 110 | total_price DECIMAL(10, 2), 111 | FOREIGN KEY (order_id) REFERENCES orders(order_id), 112 | FOREIGN KEY (product_id) REFERENCES products(product_id) 113 | ); 114 | -- Inserting into Products Table 115 | INSERT INTO products (product_name, unit_price) 116 | VALUES 117 | ('Desktop Computer', 800.00), 118 | ('Tablet', 300.00), 119 | ('Printer', 150.00); 120 | 121 | -- Inserting into Customers Table 122 | INSERT INTO customers (customer_name, email) 123 | VALUES 124 | ('Alice Johnson', 'alice@example.com'), 125 | ('Charlie Brown', 'charlie@example.com'), 126 | ('Eva Davis', 'eva@example.com'); 127 | 128 | -- Inserting into Orders Table 129 | INSERT INTO orders (customer_id, order_date) 130 | VALUES 131 | (1, '2023-02-01'), 132 | (2, '2023-02-02'), 133 | (3, '2023-02-03'); 134 | 135 | -- Inserting into Order_Details Table 136 | INSERT INTO order_details (order_id, product_id, quantity, total_price) 137 | VALUES 138 | -- Order 1 details 139 | (1, 1, 2, 1600.00), 140 | (1, 2, 3, 900.00), 141 | 142 | -- Order 2 details 143 | (2, 2, 2, 600.00), 144 | (2, 3, 1, 150.00), 145 | 146 | -- Order 3 details 147 | (3, 1, 3, 2400.00), 148 | (3, 3, 2, 300.00); 149 | ``` 150 | ### Backing up one table 151 | ```sql 152 | mysqldump -u root -p sales order_details > order_details.sql 153 | ``` 154 | --------------------------------------------------------------------------------