8.1 The need for a database backup
You have learned how to create database schemas, tables, view them. Also fill these tables with data and change them. Now you need to learn the most important thing - to make backups.
The database is very easy to break. The fact is that the database is usually in a state of constant change: something is constantly saved and added to it.
Imagine that you have a tab open in your browser and you do not want the information in it to be lost. Agree that if you just decide never to close it, then this will not be the best option. After all, the browser can freeze, it can be closed by accident, you can lose the light or burn your computer. Windows can install another update, but you never know what.
So, your database is the same tab. And it's only a matter of time when something goes wrong during the next write to the disk. Therefore, you definitely need to learn how to make backups.
Making backups is like brushing your teeth, normal companies make a backup of the contents of each database every night. And since hard drives can also fail and there are fires in data centers, backups are usually stored in 2-3 copies in data centers in different parts of the world.
There are several approaches to creating a backup.
Backup files. Since all database data is stored on disk as a set of files , the easiest way is to simply copy these files somewhere. Or first archive it into one archive, and then copy it somewhere.
This is the fastest way to make a backup, but it requires that the files do not change while they are being copied/archived. The database must be frozen while the copy is being created, otherwise the copy will be crooked. After all, there may be a situation that some of the new data is recorded in the database, and some is not yet.
Backup in the form of SQL queries . As you already know, adding and changing data in the SQL server database is always represented as SQL queries. So you can ask the SQL server to save the contents of a particular table or tables to a file as SQL queries.
This method is slower than the previous one, but it can be used to transfer data between SQL servers from different manufacturers.
Also, not all data always needs to be saved. Often you have a bunch of service or obsolete information in your tables and you can tell the SQL server not to save it.
Also, for tables that store some events with reference to time, you can simply select such events for the last day and store only them.
Various utilities . Companies are willing to pay a lot of money to get a guarantee that their data will remain intact and safe. Therefore, various IT companies began to offer solutions for all occasions. For example, there are programs that can make backup databases in the form of files and at the same time do not require that these files do not change.
For example, you can install special drivers in the operating system and keep track of when, what and where has changed.
8.2 Backing up the database
Workbench is excellent at exporting and importing data from the databases it works with. To do this, you just need to press the menu item: Server-> Data Export
. And you will see something like this:
![Workbench Buckup Data Export](https://cdn.codegym.cc/images/article/3bc1564d-b5cb-4378-9968-570697e9603a/1024.jpeg)
Now let's add some explanation:
![Workbench Buckup Data Export 1](https://cdn.codegym.cc/images/article/821a37d4-c6f3-4ccf-80bf-4873f3d8d061/1024.jpeg)
The order is this:
- First, select the schema, or schemas, that will be exported.
- Then on the right we specify the tables for backup.
- Next, we must choose what exactly we will export:
- data only;
- structure only (tables will be, but empty);
- data and structure.
- Choose how to save the data:
- one file for each table;
- one file for all tables.
- You can also add the schema creation code to the top of the file. This is useful when transferring data between different DBMS.
8.3 Backing up the table schema
Let's back up the schemas only, without the actual data.
I selected Dump Structure Only and clicked Start Export.
![Workbench Buckup Data Export 3](https://cdn.codegym.cc/images/article/18f4743c-0edb-468b-8408-52f59c75a20f/1024.jpeg)
And this is what I found in the file that Workbench made.
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(100) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`created_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
That's right, this is the schema of our tables, if I may say so about a single table.
8.4 Backing up data
Now let's backup only the data, but without the schema structure.
Let's select Dump Data Only and see what they give us:
![Workbench Buckup Data Export Dump Data Only](https://cdn.codegym.cc/images/article/b81bb769-5767-4f8a-9a45-bf36e59746e2/1024.jpeg)
And this is what I found in the file that Workbench made.
--
-- Dumping data for table `user`
--
LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES
(1,'Ivanov Ivan',40,'2022-05-11'),
(2,'Petrov Nikola',1,'2021-05-01'),
(3,'Sidroov Vitaly',8,'2022-05-12');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
Yes, very much like the truth. We also see a special code here that locks the user table. This is done so that you can simply execute this SQL script: it contains all the instructions that will allow any SQL server to correctly restore the data.
8.5 Deploying a backup
And finally, the last thing is restoring the database from a backup. This Action is very simple but useful.
Let's say I want all my students to have a specific database so that you can learn how to write queries on it. Then I'll just give you a link to the file that you deploy locally and that's it.
And even if you accidentally delete some data, tables, or even the entire schema, it can always be restored from a backup.
In order to import data, you need to click on the item in the menu Server-> Data Import
. Then you will see this picture:
![Workbench Buckup import data](https://cdn.codegym.cc/images/article/3e3e48d4-9f99-4fa1-a2cb-121920583570/1024.jpeg)
As in the case of export, you are first prompted to select a data source: a group of files or all data in one file.
Then you need to select the scheme into which the specified data will be uploaded. You can select an existing scheme or create a new one.
After importing, you need to click the update button 🔄 to see the imported table/schema.
![](https://cdn.codegym.cc/images/article/ebf41a3d-9503-4355-a04e-0f59c9cd5c1d/1024.jpeg)
Important! If your backup contains not only data, but also a description of the table structure, then the tables will be recreated (if there are already any in the target schema).
GO TO FULL VERSION