sql docker容器_了解SQL Server Docker容器中的备份和还原操作

sql docker容器

In this 17th article of the series (see the full article index at bottom), we will discuss the concepts of database backup-and-restore operations on SQL Server Docker containers. This is certainly the era of containers era and it is buzz right now, so let us understand the importance of backup-and-restore the databases on the Docker containers.

在该系列的 17篇文章中(请参阅底部的完整文章索引),我们将讨论SQL Server Docker容器上的数据库备份和还原操作的概念。 这肯定是容器时代,现在很流行,所以让我们了解在Docker容器上备份和还原数据库的重要性。

Docker containers are systems that are small, light weight, isolated, and segmented subsystems built on the Linux or Windows host system. It’s just running an application on the host machine.

Docker容器是在Linux或Windows主机系统上构建的小型,轻量,隔离和分段子系统的系统。 它只是在主机上运行一个应用程序。

This article discusses the following topics:

本文讨论以下主题:

  • Pre-requisites

    先决条件
  • Steps to initiate a database backup and copy the backup file across the containers

    启动数据库备份并跨容器复制备份文件的步骤
  • Step by step to restore a database

    逐步还原数据库
  • Pulling the latest SQL Server 2017 container images from the docker registry

    从Docker注册表中提取最新SQL Server 2017容器映像
  • Running the downloaded Docker container image using docker run command

    使用docker run命令运行下载的Docker容器映像
  • Details the steps to create the database in the container

    详细说明在容器中创建数据库的步骤
  • Explain data persistence in Docker

    解释Docker中的数据持久性
  • And more…

    和更多…

先决条件 (Pre-requisites)

The list describes essential prerequisites to follow along the article

该列表描述了本文遵循的基本前提条件

  1. Docker engine 1.8+ on Linux/Mac/Windows

    Linux / Mac / Windows上的Docker引擎1.8+
  2. Min 2 GB disk space and Min 2 GB RAM

    最少2 GB磁盘空间和最少2 GB RAM
  3. Open the Super User console

    打开超级用户控制台
  4. Basic understanding of Docker containers

    对Docker容器的基本了解

入门 (Getting started)

To build the SQL Server 2017 container use the docker pull command and specify the reference to the latest available Docker container image from the Microsoft repo registry.

要构建SQL Server 2017容器,请使用docker pull命令并从Microsoft存储库注册表中指定对最新可用Docker容器映像的引用。

docker pull microsoft/mssql-server-linux:2017-latest

docker pull microsoft / mssql-server-linux:2017-latest

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第1张图片

To run the SQL Server Docker container, in the background, use docker run command.

要在后台运行SQL Server Docker容器,请使用docker run命令。

docker run -e ‘ACCEPT_EULA=Y’ -e ‘MSSQL_SA_PASSWORD=SQLShack$2018’ –name shackdemo1 -p 1401:1433 -d microsoft/mssql-server-linux:latest

docker run -e'ACCEPT_EULA = Y'-e'MSSQL_SA_PASSWORD = SQLShack $ 2018'–name shackdemo1 -p 1401:1433 -d microsoft / mssql-server-linux:latest

To instantiate a bash session, run the bash executable using docker exec command.

要实例化bash会话,请使用docker exec命令运行bash可执行文件。

docker exec –it shackdemo1 bash

docker exec –it shackdemo1 bash

Now, a Bash session is opened for SQLShackDemo1 container. Connect to the SQL instance using sqlcmd and create a new database named SQLShackDemo.

现在,将为SQLShackDemo1容器打开一个Bash会话。 使用sqlcmd连接到SQL实例并创建一个名为SQLShackDemo的新数据库。

Next, create a sample table SQLAuthor and insert few dummy records into the table.

接下来,创建一个示例表SQLAuthor并将少量虚拟记录插入表中。

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第2张图片

Let us create a database backup using backup database command

让我们使用backup database命令创建数据库备份

BACKUP DATABASE [SQLShackDemo] TO DISK = N’/var/opt/mssql/backup/SQLShackDemo.bak’ WITH FORMAT, INIT, COMPRESSION,STATS = 10

备份数据库[SQLShackDemo]到磁盘= N'/ var / opt / mssql / backup / SQLShackDemo.bak',格式,INIT,COMPRESSION,STATS = 10

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第3张图片

Let’s exit the docker session. We need to make sure that the backup file is not trapped within the container.  It is very easy to move files in-and-out of the container using the docker cp command. 

让我们退出docker会话。 我们需要确保备份文件没有被困在容器中。 使用docker cp命令将文件移入和移出容器非常容易。

Let’s take a look at the docker cp command. It takes two parameters.

让我们看一下docker cp命令。 它有两个参数。

  1. First, the container name, shackdemo1, followed by colon and then path of the backup file to copy.

    首先,容器名称shackdemo1,然后是冒号,然后是要复制的备份文件的路径。
  2. Second, the host path to copy the file.

    其次,复制文件的主机路径。

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第4张图片

Now, browse the host path to check the attributes of the copied backup file.

现在,浏览主机路径以检查复制的备份文件的属性。

ls –l /tmp/SQLShackDemo.bak

ls –l /tmp/SQLShackDemo.bak

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第5张图片

In this section, create a new container named shackdemo2. As we can see that, the newly created SQL Server 2017 Docker container instance just have the system databases.

在本节中,创建一个名为shackdemo2的新容器 如我们所见,新创建SQL Server 2017 Docker容器实例仅包含系统数据库。

[root@localhost thanvitha]# docker run -e ‘ACCEPT_EULA=Y’ -e ‘MSSQL_SA_PASSWORD=SQLShack$2018’ –name shackdemo2 -p 1402:1433 -d microsoft/mssql-server-linux:latest 714269288a08da8775eb59dad6c5d4c2b2d6bad833043e81fcf4afc6586be7aa [root@localhost thanvitha]# docker exec -it shackdemo2 bash root@714269288a08:/# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA Password:

[root @ localhost thanvitha]#docker run -e'ACCEPT_EULA = Y'-e'MSSQL_SA_PASSWORD = SQLShack $ 2018'–name shackdemo2 -p 1402:1433 -d microsoft / mssql-server-linux:latest 714269288a08da8775eb59dad6c5d4c2b2d6bad833aaroot81 #docker exec -it shackdemo2 bash root @ 714269288a08:/#/ opt / mssql-tools / bin / sqlcmd -S localhost -U SA密码:

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第6张图片

Now, exit the SQL Docker container instance, shackdemo2. Copy the backup file from the host machine to the shackdemo2 container.

现在,退出SQL Docker容器实例shackdemo2 。 将备份文件从主机复制到shackdemo2容器。

Let’s take a look at copying backup files into the new container using the same docker cp command. We’ll use docker cp again, but we’ll reverse the parameters. 

让我们来看看使用相同的docker cp命令将备份文件复制到新容器中。 我们将再次使用docker cp,但是我们将反转参数。

  1. First goes the file path of the host followed by a space and then the container name shackdemo2, a colon and the path where we want copy the file.

    首先是主机的文件路径,后跟一个空格,然后是容器名称shackdemo2,冒号和我们要复制文件的路径。
  2. sqldemo2:var/opt/mssql/data/SQLShackDemo.bak sqldemo2:var / opt / mssql / data / SQLShackDemo.bak

Docker cp /tmp/SQLShackDemo.bak sqldemo2:var/opt/mssql/data/

Docker cp /tmp/SQLShackDemo.bak sqldemo2:var / opt / mssql / data /

Next, run the database restore command to restore the database.

接下来,运行database restore命令以还原数据库。

RESTORE DATABASE [SQLShackDemo] FROM DISK = N’/var/opt/mssql/data/SQLShackDemo.bak’ with REPLACE

从磁盘还原数据库[SQLShackDemo] = N'/ var / opt / mssql / data / SQLShackDemo.bak',并带有REPLACE

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第7张图片

We can see that SQLShackDemo database is listed. Let’s query the tables to view the sample data.

我们可以看到列出了SQLShackDemo数据库。 让我们查询表以查看样本数据。

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第8张图片

数据外化 (Data externalization)

Let’s walk through the details of how to externalize the application database data file. As long as the containers remain intact with the host, the data will remain safe even if the container is stopped or restarted. However, if you remove the container your databases get removed with it and it’ll be gone forever. 

让我们详细介绍如何外部化应用程序数据库数据文件。 只要容器与主机完好无损,即使容器停止或重新启动,数据也将保持安全。 但是,如果您删除该容器,则数据库也将随之删除,并且它将永远消失。

Let’s discuss the Docker’s solution that keeps the data safe across containers. Using Docker data volume (-v) option, it is that simple to share the data. During the SQL Server container creation process, map to the SQL Server database file directory using –v parameter. 

让我们讨论一下Docker解决方案,该解决方案确保跨容器的数据安全。 使用Docker数据量(-v)选项,共享数据非常简单。 在SQL Server容器创建过程中,使用–v参数映射到SQL Server数据库文件目录。

To create a new container named sqldemo use the following docker run command.

要创建一个名为sqldemo的新容器, 使用以下docker run命令。

#docker run -e'ACCEPT_EULA=Y' -e'MSSQL_SA_PASSWORD=thanVitha@2015' --name sqldemo  –v sqlservervolume:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest

Now, check the status of the newly created docker container sqldemo

现在,检查新创建的docker容器sqldemo的状态

#docker ps –a

-v, this is where we’re going to specify the volume information. Create a new volume sqlservervolume and map it with the internal directory structure of SQL Server instance, or the Linux instance, where SQL Server stores its data and log files. In this case, the default volume location /var/opt/mssql is mentioned.

-v,这是我们要指定音量信息的地方。 创建一个新的卷sqlservervolume,并将其与SQL Server实例(或Linux实例)的内部目录结构进行映射,在该实例中SQL Server存储其数据和日志文件。 在这种情况下,将提到默认的卷位置/ var / opt / mssql。

Now, we’ve created the Sqldemo container. Now, instead of storing database files within the container, the data files are stored /var/opt/mssql directory, the container data is now externalized. It’s stored in the name sqlservervolume data volume. We can take a look at volumes using docker volume ls. 

现在,我们创建了Sqldemo容器。 现在,将数据文件存储在/ var / opt / mssql目录中,而不是将数据库文件存储在容器中,现在将容器数据外部化。 它存储在名称sqlservervolume数据卷中。 我们可以看看使用docker volume ls的卷。

#docker volume ls

Let’s open a docker session and create a database using docker exec and sqlcmd commands.

让我们打开一个docker会话并使用docker exec和sqlcmd命令创建一个数据库。

#docker exec –it sqldemo bash
#/opt/mssql-tools/bin/sqlcmd –U SA –P thanVitha@2015
1>create database sqlvolumetestDB;
2>go

So far, we’ve created a database sqlvolumetestDB. Let’s go ahead quit SQL shell and exit out of the Linux shell. Now, go ahead and remove the container. Before that though, stop the container using docker stop command. Once it’s stopped, remove the sqldemo container using docker rm command but the volume still exists.

到目前为止,我们已经创建了一个数据库sqlvolumetestDB 。 让我们继续退出SQL Shell,退出Linux Shell。 现在,继续并卸下容器。 在此之前,请使用docker stop命令停止容器。 停止后,使用docker rm命令删除sqldemo容器,但该卷仍然存在。

Now, create a new SQL container and link the existing volume to the SQL container. The new SQL instance is going to be Newsqldemo.

现在,创建一个新SQL容器并将现有卷链接到SQL容器。 新SQL实例将是Newsqldemo。

#docker run -e'ACCEPT_EULA=Y' -e'MSSQL_SA_PASSWORD=thanVitha@2015' --name Newsqldemo –v sqlservervolume:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest

Let’s go into the Newsqldemo container and make sure that we still have the database that we’d created earlier in the demo.

让我们进入Newsqldemo容器,并确保我们仍然具有在演示中先前创建的数据库。

#docker exec –it sqldemo bash
#/opt/mssql-tools/bin/sqlcmd –U SA –P thanVitha@2015
1>select name from sys.databases
2>go
1>use sqlvolumetestDB
2>go
1>create table dummy(id int, name char(20));
2>go
1>insert into dummy values(1,’Prashanth’),(2,’thanVitha’);
2>go

Once we get into the SQL shell, query the sys.databases system object. This will list all the databases of the Newsqldemo instance.

进入SQL Shell之后,查询sys.databases系统对象。 这将列出Newsqldemo实例的所有数据库。

sql docker容器_了解SQL Server Docker容器中的备份和还原操作_第9张图片

摘要 (Summary)

Docker containers are definitely a top choice and talking point for many developers for its rapid development and seamless deployment process.

Docker容器的快速开发和无缝部署过程无疑是许多开发人员的首选和讨论点。

Docker doesn’t give you a way of saying mount this guest file system into the host, preserving what was on the guest. It always chooses to mount the host file-system over the guest file-system.

Docker并没有说出将来宾文件系统挂载到主机中的方法,以保留来宾文件上的内容。 它始终选择将主机文件系统挂载到来宾文件系统上。

You can also have multiple containers linked to this single volume in order to have them share a common storage resource. With Docker volumes, you can save data across multiple Docker containers or share data between several containers at the same time.

您也可以将多个容器链接到该单个卷,以使它们共享公共存储资源。 借助Docker卷,您可以跨多个Docker容器保存数据,或同时在多个容器之间共享数据。

目录 (Table of contents)

Database Backup and Restore process in SQL Server – series intro
An overview of the process of SQL Server backup-and-restore
Understanding the SQL Server Data Management Life Cycle
Understanding SQL Server database recovery models
Understanding SQL Server Backup Types
Backup and Restore (or Recovery) strategies for SQL Server database
Discussing Backup and Restore Automation using SQLCMD and SQL Server agent
Understanding Database snapshots vs Database backups in SQL Server
SqlPackage.exe – Automate SQL Server Database Restoration using bacpac with PowerShell or Batch techniques
Smart database backup in SQL Server 2017
How to perform a Page Level Restore in SQL Server
Backup Linux SQL Server databases using PowerShell and Windows task scheduler
SQL Server Database backup and restore operations using the Cloud
Tail-Log Backup and Restore in SQL Server
SQL Server Database Backup and Restore reports
Database Filegroup(s) and Piecemeal restores in SQL Server
In-Memory Optimized database backup and restore in SQL Server
Understanding Backup and Restore operations in SQL Server Docker Containers
Backup and Restore operations with SQL Server 2017 on Docker containers using Azure Data Studio
Interview questions on SQL Server database backups, restores and recovery – Part I
Interview questions on SQL Server database backups, restores and recovery – Part II
Interview questions on SQL Server database backups, restores and recovery – Part III
Interview questions on SQL Server database backups, restores and recovery – Part IV
SQL Server中的数据库备份和还原过程–系列简介
SQL Server备份和还原过程概述
了解SQL Server数据管理生命周期
了解SQL Server数据库恢复模型
了解SQL Server备份类型
SQL Server数据库的备份和还原(或恢复)策略
讨论使用SQLCMD和SQL Server代理进行备份和还原自动化
了解SQL Server中的数据库快照与数据库备份
SqlPackage.exe –使用bacpac和PowerShell或Batch技术自动执行SQL Server数据库还原
SQL Server 2017中的智能数据库备份
如何在SQL Server中执行页面级还原
使用PowerShell和Windows任务计划程序备份Linux SQL Server数据库
使用CloudSQL Server数据库备份和还原操作
SQL Server中的尾日志备份和还原
SQL Server数据库备份和还原报告
SQL Server中的数据库文件组和零碎还原
在SQL Server中进行内存优化的数据库备份和还原
了解SQL Server Docker容器中的备份和还原操作
使用Azure Data Studio在Docker容器上使用SQL Server 2017进行备份和还原操作
有关SQL Server数据库备份,还原和恢复的面试问题–第一部分
有关SQL Server数据库备份,还原和恢复的面试问题–第二部分
有关SQL Server数据库备份,还原和恢复的面试问题–第三部分
有关SQL Server数据库备份,还原和恢复的面试问题–第IV部分

参考资料 (References)

  • Restore a SQL Server database in a Linux Docker container 在Linux Docker容器中还原SQL Server数据库
  • Yes to databases in containers – Microsoft SQL Server available on docker store 对容器中的数据库是– Microsoft SQL Server可在docker store中使用
  • microsoft/mssql-server-windows-developer microsoft / mssql-server-windows-developer

翻译自: https://www.sqlshack.com/understanding-backup-and-restore-operations-in-sql-server-docker-containers/

sql docker容器

你可能感兴趣的:(数据库,java,linux,大数据,mysql)