본문 바로가기

Devops/Docker

[Docker] Docker Oracle 이미지 백업, 복원

Docker를 이용해 Oracle 을 구축하게 되면 오랜 시간 작업한 DB 전체를 그대로 백업하고 복원하고 싶어진다.

Docker 상에 구축된 DB를 백업하고 복원하는 방법을 살펴본다.

 

1. Docker Container 상의 Oracle DB Stop

우선 데이터 유입, 변경을 막기 위해 인스턴스를 중지한다.

 

root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker exec -it local_db bash -c "source /home/oracle/.bashrc; sqlplus sys/Oradoc_db1@ORCLCDB as sysdba"

SQL*Plus: Release 12.2.0.1.0 Production on Tue Dec 3 13:20:24 2024

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Last Successful login time: Sun Dec 01 2024 14:04:50 +00:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> shutdown immediate
;
Database closed.
Database dismounted.
ORACLE instance shut down.
ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect
descriptor

Warning: You are no longer connected to ORACLE.
SQL> SP2-0223: No lines in SQL buffer.
SQL>

 

2. 현재 Container 상태를 Commit 후 tar 파일로 Save

root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker commit -p a032c81def8c local_db_backup_test
sha256:d5d108a291a41c64b28ffdfa0f3dc33852ccd32908fdb0d4d140f1c8261aec31
root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker save -o local_db_backup_test.tar local_db_backup_test
root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker images
REPOSITORY                            TAG       IMAGE ID       CREATED         SIZE
local_db_backup_test                  latest    d5d108a291a4   2 minutes ago   3.98GB
local_db_backup                       latest    423974c5541c   47 hours ago    3.98GB
kindlove/iam-back-msa-onlinewaiting   latest    c8bc51a45fc0   13 days ago     455MB
<none>                                <none>    441f6c396701   13 days ago     455MB
kindlove/iam-back-msa-onlinewaiting   <none>    478ab1295dec   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    5e6cf70d30fb   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    79c72b2f5488   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    437eb7561ada   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    e0e97ade78fb   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    b2b6b1ab842a   13 days ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    da1f0654b695   2 weeks ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    94289545a359   2 weeks ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    58f527821a29   2 weeks ago     433MB
kindlove/iam-back-msa-onlinewaiting   <none>    9c5cedbeb24c   2 weeks ago     433MB
test-postgres-backup                  latest    1bc258f3ca0f   4 weeks ago     487MB
goharbor/redis-photon                 v2.8.2    6f4498a430ca   18 months ago   121MB
goharbor/harbor-registryctl           v2.8.2    fa61a236a6d6   18 months ago   142MB
goharbor/registry-photon              v2.8.2    f80e71363231   18 months ago   79.3MB
goharbor/nginx-photon                 v2.8.2    3d009028f260   18 months ago   120MB
goharbor/harbor-log                   v2.8.2    2914d282d9bf   18 months ago   127MB
goharbor/harbor-jobservice            v2.8.2    40118f1568a8   18 months ago   141MB
goharbor/harbor-core                  v2.8.2    0bbbd1f379fc   18 months ago   165MB
goharbor/harbor-portal                v2.8.2    3e74e0758aa4   18 months ago   127MB
goharbor/harbor-db                    v2.8.2    5126635ae9f0   18 months ago   174MB
kindest/node                          <none>    36d37c652064   20 months ago   936MB
root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm#

 

3. Container 가 Mount하고 있는 저장소 백업

docker inspect 명령으로 현재 Container의 Mount 위치를 파악한다. Mount 된 저장소에 실제 DB 파일이 존재한다.

 

root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker inspect a032c81def8c
[
    {
        "Id": "a032c81def8cfdbf65bb5db0a32b7fe36a807d1d42890302e3d636f5219bf645",
        "Created": "2024-12-03T00:28:29.397158445Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "/bin/bash /home/oracle/setup/dockerInit.sh"
        ],


       ...


        "Mounts": [
            {
                "Type": "volume",
                "Name": "c7d000584c9c7bf7e6c26fd83e155993f1215576303b7d92eb151cb86f03b00a",
                "Source": "/var/lib/docker/volumes/c7d000584c9c7bf7e6c26fd83e155993f1215576303b7d92eb151cb86f03b00a/_data",
                "Destination": "/ORCL",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "a032c81def8c",
 ...

 

Mount된 저장소를 압축한다.

 

root@DESKTOP-GH94F8C:/var/lib/docker/volumes/c7d000584c9c7bf7e6c26fd83e155993f1215576303b7d92eb151cb86f03b00a# tar cvzf data_oracle.tar.gz ./_data/
./_data/
./_data/u04/
./_data/u04/app/
./_data/u04/app/oracle/
./_data/u04/app/oracle/redo/
./_data/u04/app/oracle/redo/redo02.dbf
./_data/u04/app/oracle/redo/redo01.dbf
./_data/u04/app/oracle/redo/redo03.dbf
./_data/u01/
./_data/u01/app/
./_data/u01/app/oracle/
./_data/u01/app/oracle/product/
./_data/u01/app/oracle/product/12.2.0/
./_data/u01/app/oracle/product/12.2.0/dbhome_1/
./_data/u01/app/oracle/product/12.2.0/dbhome_1/dbs/
./_data/u01/app/oracle/product/12.2.0/dbhome_1/dbs/spfileORCLCDB.ora

 

...

 

4. 덤프 된 Container 이미지, 저장소를 복원 (다른 PC 등에서 복원)

앞서 덤프한 Docker 이미지를 docker load 명령으로 불러온다.

 

root@DESKTOP-GH94F8C:/mnt/c/Users/skkkm# docker load < local_db_backup_test.tar
f4df80a6dab1: Loading layer [==================================================>]  347.1kB/347.1kB
Loaded image: local_db_backup_test:latest

 

불러온 이미지로 Oracle Container를 새로 시작한다.

 

root@DESKTOP-GH94F8C:/home/kindlove# docker run -dit --name local_db -p 1521:1521 local_db_backup_test
2ef794a6ae856e2da6c6e58e09ff89d143e69333893cf801b7e95e1c5f085449

 

Docker ps로 상태를 보면 바로 종료되었음을 볼 수 있고 log를 살펴본다.


root@DESKTOP-GH94F8C:/home/kindlove# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                        PORTS                                                             NAMES
2ef794a6ae85   local_db_backup_test        "/bin/sh -c '/bin/ba…"   11 seconds ago   Exited (1) 9 seconds ago                                                                        local_db
f02641a7aa20   test-postgres-backup   "docker-entrypoint.s…"   3 days ago       Exited (255) 21 minutes ago   0.0.0.0:15432->5432/tcp, :::15432->5432/tcp                       test-postgres
4639d8ff18a5   kindest/node:v1.26.3   "/usr/local/bin/entr…"   2 weeks ago      Up 21 minutes                                                                                   temp-worker
8d49227a2a9b   kindest/node:v1.26.3   "/usr/local/bin/entr…"   2 weeks ago      Up 21 minutes                 0.0.0.0:30443-30449->30443-30449/tcp, 127.0.0.1:40453->6443/tcp   temp-control-plane

 

Log를 보면 파일이 없어서 종료가 되었다. 아래 /u01.. 이하 폴더는 덤프 전 Container에서 Mount한 data 이나 새로 로딩한 Container에서는 파일이 없어서 에러가 발생한 것이다.


root@DESKTOP-GH94F8C:/home/kindlove# docker logs -f 2ef794a6ae85
Start up Oracle Database
Tue Dec 3 19:37:46 UTC 2024
start database
start listener
The database is ready for use .
tail: cannot open '/u01/app/oracle/diag/rdbms/orclcdb/ORCLCDB/trace/alert_ORCLCDB.log' for reading: No such file or directory
tail: no files remaining

 

새로 생성한 Container의 mount폴더를 확인한다.

 

root@DESKTOP-GH94F8C:/home/kindlove# docker inspect 2ef794a6ae85
[
    {
        "Id": "2ef794a6ae856e2da6c6e58e09ff89d143e69333893cf801b7e95e1c5f085449",
        "Created": "2024-12-03T19:37:45.45054966Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "/bin/bash /home/oracle/setup/dockerInit.sh"
        ],


     ...


        "Mounts": [
            {
                "Type": "volume",
                "Name": "29ccfd5ec0d24aae0f9dfdf91f16b01812bd6e169f5b73a58d80b9be5f59c74e",
                "Source": "/var/lib/docker/volumes/29ccfd5ec0d24aae0f9dfdf91f16b01812bd6e169f5b73a58d80b9be5f59c74e/_data",

 

앞서 덤프한 이전 Container의 _data 폴더를 새로 생성한 Container의  Mount 폴더에 푼다.

root@DESKTOP-GH94F8C:/var/lib/docker/volumes/29ccfd5ec0d24aae0f9dfdf91f16b01812bd6e169f5b73a58d80b9be5f59c74e# tar xvzf /mnt/e/Data/SWArchitect/PrivateProject/Backup/data_oracle.tar.gz .
    

5. Container 재시작으로 Oracle 정상 복원 확인

 

root@DESKTOP-GH94F8C:~# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                        PORTS
                                           NAMES
2ef794a6ae85   local_db_backup        "/bin/sh -c '/bin/ba…"   11 minutes ago   Exited (1) 11 minutes ago
                                           local_db

f02641a7aa20   test-postgres-backup   "docker-entrypoint.s…"   3 days ago       Exited (255) 33 minutes ago   0.0.0.0:15432->5432/tcp, :::15432->5432/tcp                       test-postgres
4639d8ff18a5   kindest/node:v1.26.3   "/usr/local/bin/entr…"   2 weeks ago      Up 33 minutes
                                           temp-worker
8d49227a2a9b   kindest/node:v1.26.3   "/usr/local/bin/entr…"   2 weeks ago      Up 33 minutes                 0.0.0.0:30443-30449->30443-30449/tcp, 127.0.0.1:40453->6443/tcp   temp-control-plane


root@DESKTOP-GH94F8C:~# docker container restart 2ef794a6ae85
2ef794a6ae85


root@DESKTOP-GH94F8C:~# docker logs -f 2ef794a6ae85
Start up Oracle Database
Tue Dec 3 19:37:46 UTC 2024
start database
start listener
The database is ready for use .
tail: cannot open '/u01/app/oracle/diag/rdbms/orclcdb/ORCLCDB/trace/alert_ORCLCDB.log' for reading: No such file or directory
tail: no files remaining
Start up Oracle Database
Tue Dec 3 19:49:37 UTC 2024
start database
start listener
The database is ready for use .
0x64e1eca0         1  anonymous block
2024-12-03T19:49:48.681015+00:00
Resize operation completed for file# 1, old size 8690200K, new size 8690304K
2024-12-03T19:49:48.734937+00:00
ORCLPDB1(3):Opening pdb with no Resource Manager plan active
Pluggable database ORCLPDB1 opened read write
Starting background process CJQ0
2024-12-03T19:49:48.821151+00:00
CJQ0 started with pid=40, OS id=261
Completed: ALTER DATABASE OPEN

 

저장소를 기존 처럼 맞춰준 후 Container를 재 시작하면 정상적으로 DB Open상태로 됨을 볼 수 있다.

DB에 기존 계정대로 접속해보면 동일하게 실행이 됨을 볼 수 있다.

 

-- The End --