Get a backup with mysqldump (Docker's MySQL)

Introduction

You need a backup

Considering the environment created so far and the future use, it is necessary to back up the data in case something goes wrong.

-I installed Docker on Raspberry Pi 3 -Launched Redmine with Docker on Raspberry Pi 3

First of all, study while looking at the following articles.

-10th Backup/Restore Trap in Docker Environment (4/4)

What is the image? Only Redmine and MySQL.

$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED       SIZE
redmine             latest    f2b5b69f9ad5   4 weeks ago   440MB
hypriot/rpi-mysql   latest    4f3cbdbc3bdb   2 years ago   209MB

What is the container that is up? This is also only Redmine and MySQL.

$ docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS        PORTS                    NAMES
5190a7c0980c   hypriot/rpi-mysql   "/entrypoint.sh mysq…"   19 hours ago   Up 19 hours   3306/tcp                 redmine-db
a2afe2cb982a   redmine             "/docker-entrypoint.…"   19 hours ago   Up 19 hours   0.0.0.0:3000->3000/tcp   redmine

The MySQL database running in the started MySQL container (redmine-db) is stored in the/var/lib/mysql directory. If you check the contents, you can see that there are various contents.

$ docker exec -it redmine-db ls -l /var/lib/mysql
total 36876
-rw-rw---- 1 mysql mysql  5242880 Jan 10 01:10 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Jan  9 06:55 ib_logfile1
-rw-rw---- 1 mysql mysql 27262976 Jan 10 01:10 ibdata1
drwx------ 2 mysql mysql     4096 Jan  9 06:55 mysql
drwx------ 2 mysql mysql     4096 Jan  9 06:55 performance_schema
drwx------ 2 mysql mysql     4096 Jan  9 06:57 redmine

In the article, I'm trying to tar the contents from here and import it ... but I'll check it on the desk.

Now that you've learned about the idea of ​​** data volumes ** in Docker, let's look at the data volumes set in redmine-db. I checked and found out the data volume, but ** it is a specification that cannot be backed up/restored with docker export/import **. (Even with docker commit, it cannot be imaged including the data volume) I even found out that data volumes need to be backed up / restored individually.

$ docker inspect redmine-db
[
    {

~~~~~~~~~~~Abbreviation~~~~~~~~~~~

            "Image": "hypriot/rpi-mysql",
            "Volumes": {
                "/var/lib/mysql": {}
            },

~~~~~~~~~~~Abbreviation~~~~~~~~~~~

    }
]

Access Docker's MySQL

Log in to Docker's MySQL. You will be asked for your password, so if you forget it, you will not be able to log in.

$ docker exec -it redmine-db mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.60-0+deb7u1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Check the database that exists now.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redmine            |
+--------------------+
4 rows in set (0.00 sec)

Check the list of existing Redmine tables.

mysql> show tables in redmine;
+-------------------------------------+
| Tables_in_redmine                   |
+-------------------------------------+
| ar_internal_metadata                |
| attachments                         |
| auth_sources                        |
| boards                              |
| changes                             |
| changeset_parents                   |
| changesets                          |
| changesets_issues                   |
| comments                            |
| custom_field_enumerations           |
| custom_fields                       |
| custom_fields_projects              |
| custom_fields_roles                 |
| custom_fields_trackers              |
| custom_values                       |
| documents                           |
| email_addresses                     |
| enabled_modules                     |
| enumerations                        |
| groups_users                        |
| import_items                        |
| imports                             |
| issue_categories                    |
| issue_relations                     |
| issue_statuses                      |
| issues                              |
| journal_details                     |
| journals                            |
| member_roles                        |
| members                             |
| messages                            |
| news                                |
| open_id_authentication_associations |
| open_id_authentication_nonces       |
| projects                            |
| projects_trackers                   |
| queries                             |
| queries_roles                       |
| repositories                        |
| roles                               |
| roles_managed_roles                 |
| schema_migrations                   |
| settings                            |
| time_entries                        |
| tokens                              |
| trackers                            |
| user_preferences                    |
| users                               |
| versions                            |
| watchers                            |
| wiki_content_versions               |
| wiki_contents                       |
| wiki_pages                          |
| wiki_redirects                      |
| wikis                               |
| workflows                           |
+-------------------------------------+
56 rows in set (0.00 sec)

Check the list of existing users now.

mysql> select host,user from mysql.user;
+------+------+
| host | user |
+------+------+
| %    | root |
+------+------+
1 row in set (0.00 sec)

Backup-restore

Take a backup with a dump file

First, create a user for backup. It may be used for other than backup, but ...

Enter the user name in ** username ** and the password in ** password ** and execute to create a user. Then grant permissions with GRANT. Make sure that the user you created (named ruser this time) exists.

mysql> create user 'username'@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'ruser'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user from mysql.user;
+------+-------+
| host | user  |
+------+-------+
| %    | root  |
| %    | ruser |
+------+-------+
2 rows in set (0.00 sec)

mysqldumpTake a backup with. I repeated trial and error until I arrived at this command, but I omitted it.

$ docker exec -it redmine-db mysqldump redmine -uruser -ppassword > /backup/testbackup.sql

Check the contents to see if it is properly removed. It looks okay.

$ cat /backup/testbackup.sql 
-- MySQL dump 10.13  Distrib 5.5.60, for debian-linux-gnu (armv7l)
--
-- Host: localhost    Database: 
-- ------------------------------------------------------
-- Server version	5.5.60-0+deb7u1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

~~~~~~~~~~~Abbreviation~~~~~~~~~~~

--
-- Dumping data for table `workflows`
--

LOCK TABLES `workflows` WRITE;
/*!40000 ALTER TABLE `workflows` DISABLE KEYS */;
/*!40000 ALTER TABLE `workflows` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2021-01-10 12:35:45

When restoring

You can restore with the following command. I was able to restore it properly.

$ docker exec -i redmine-db mysql redmine -uruser -ppassword < /backup/testbackup.sql

Trial and error mysqldump etc.

I was able to get the dump file with the following command. (This is the First Version, and the one listed above is the Second Version) I tried various trials and errors and tried my best.

$ docker exec -it redmine-db sh -c 'exec /usr/bin/mysqldump --all-databases -uruser -ppassword --single-transaction' > /backup/testbackup.sql

In addition, (1) check the running container → (2) stop the container → (3) check that it has stopped → (4) delete the container → (5) check the deletion ... If it is running, the STATUS will be * up *, and if it is stopped, it will be * Exited *.

$ docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS       PORTS                    NAMES
5190a7c0980c   hypriot/rpi-mysql   "/entrypoint.sh mysq…"   31 hours ago   Up 8 hours   3306/tcp                 redmine-db
a2afe2cb982a   redmine             "/docker-entrypoint.…"   31 hours ago   Up 8 hours   0.0.0.0:3000->3000/tcp   redmine
$ docker stop 5190a7c0980c a2afe2cb982a
5190a7c0980c
a2afe2cb982a
$ docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS                     PORTS     NAMES
5190a7c0980c   hypriot/rpi-mysql   "/entrypoint.sh mysq…"   31 hours ago   Exited (0) 4 seconds ago             redmine-db
a2afe2cb982a   redmine             "/docker-entrypoint.…"   31 hours ago   Exited (1) 8 seconds ago             redmine
$ docker rm 5190a7c0980c a2afe2cb982a
5190a7c0980c
a2afe2cb982a
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

At the end

You can now get a backup safely. If you do it properly, you should create a shell that runs regularly with cron and back it up ... but once later.

Recommended Posts

Get a backup with mysqldump (Docker's MySQL)
Create a MySQL environment with Docker from 0-> 1
Let's get started with Java-Create a development environment ②
Let's get started with Java-Create a development environment ①
Create a simple bulletin board with Java + MySQL
When deleting a record with MySQL workbench, javax.persistence.EntityNotFoundException occurs
A story about trying to get along with Mockito
How to get started with creating a Rails app
[Docker] Connection with MySQL
[Rails] Development with MySQL
Get started with Gradle