NAVER Cloud
CloudDB for MySQL 백업 파일 오브젝트 스토리지로 내보내기 자동 설정
한크크
2024. 3. 21. 19:12
반응형
- CDB MySQL 의 경우 일 백업 시간과 보관 주기를 지정할 수 있으나 보관 주기 이후 백업 데이터 보관을 위한 export to object storage 기능에 대한 스케줄을 콘솔에서 설정할 수 없음
- export to object storage 기능을 사용하기 위해서는 cdb mysql 에서 제공하는 api 를 활용하여 사용자가 별도로 설정해야 함
1. exportBackupToObjectStorage api 사용을 위한 준비
exportBackupToObjectStorage api 실행을 위한 필수 파라미터
- fileName (백업 파일 이름)
- bucketName (백업 파일을 저장할 오브젝트 스토리지 버킷명)
- cloudMysqlInstanceNo (백업 소스인 CDB MySQL 인스턴스 번호) 임
exportBackupToObjectStorage api 를 수행하기 전에
- getCloudMysqlInstanceList 를 통해 cloudMysqlInstanceNo 를 확인하고 ( https://api.ncloud-docs.com/docs/database-vmysql-getcloudmysqlinstancelist )
getCloudMysqlInstanceList api 실행
import hashlib
import hmac
import base64
import requests
import time
import json
def make_signature(access_key, secret_key, method, uri, timestamp):
secret_key_bytes = bytes(secret_key, 'UTF-8')
message = method + " " + uri + "\n" + timestamp + "\n" + access_key
message = bytes(message, 'UTF-8')
signing_key = base64.b64encode(hmac.new(secret_key_bytes, message, digestmod=hashlib.sha256).digest())
return signing_key.decode('UTF-8')
# Replace with your actual access_key and secret_key
access_key = "XXXXXXXXXXXXX" ### key 수정
secret_key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYY" ### key 수정
# API endpoint and method
api_url = "https://ncloud.apigw.ntruss.com/vmysql/v2/getCloudMysqlInstanceList"
method = "GET"
region_code = "KR"
uri = f"/vmysql/v2/getCloudMysqlInstanceList?regionCode={region_code}"
# Generate timestamp
timestamp = str(int(time.time() * 1000))
# Generate signature
signature = make_signature(access_key, secret_key, method, uri, timestamp)
# Print the generated signature
print("Generated Signature:", signature)
print(timestamp)
# Request headers
headers = {
"Content-Type": "application/json",
"x-ncp-iam-access-key": access_key,
"x-ncp-apigw-timestamp": timestamp,
"x-ncp-apigw-signature-v2": signature
}
# Send the request
full_url = f"{api_url}?regionCode={region_code}"
response = requests.get(full_url, headers=headers)
# Print the response
print("Response Status Code:", response.status_code)
print("Response Headers:", response.headers)
- getCloudMysqlBackupDetailList 를 통해 fileName 을 확인해야 함 (https://api.ncloud-docs.com/docs/database-vmysql-getcloudmysqlbackupdetaillist)
- cloudMysqlInstanceNo 만 확인되면 getCloudMysqlBackupDetailList 수행 후 fileName 값을 활용하여 바로 exportBackupToObjectStorage api 를 실행할 수 있음
getCloudMysqlBackupDetailList 실행 후 exportBackupToObjectStorage api 실행
import hashlib
import hmac
import base64
import requests
import time
import xml.etree.ElementTree as ET
def make_signature(access_key, secret_key, method, uri, timestamp):
secret_key_bytes = bytes(secret_key, 'UTF-8')
message = method + " " + uri + "\n" + timestamp + "\n" + access_key
message = bytes(message, 'UTF-8')
signing_key = base64.b64encode(hmac.new(secret_key_bytes, message, digestmod=hashlib.sha256).digest())
return signing_key.decode('UTF-8')
# Replace with your actual access_key and secret_key
access_key = "XXXXXXXXXXXXX" ### key 수정
secret_key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYY" ### key 수정
# API endpoint and method for the first request
api_url = "https://ncloud.apigw.ntruss.com/vmysql/v2/getCloudMysqlBackupDetailList"
method = "GET"
region_code = "KR"
cloud_mysql_instance_no = "23189074"
uri = f"/vmysql/v2/getCloudMysqlBackupDetailList?regionCode={region_code}&cloudMysqlInstanceNo={cloud_mysql_instance_no}"
# Generate timestamp
timestamp = str(int(time.time() * 1000))
# Generate signature
signature = make_signature(access_key, secret_key, method, uri, timestamp)
# Request headers
headers = {
"Content-Type": "application/json",
"x-ncp-iam-access-key": access_key,
"x-ncp-apigw-timestamp": timestamp,
"x-ncp-apigw-signature-v2": signature
}
# Send the first request
full_url = f"{api_url}?regionCode={region_code}&cloudMysqlInstanceNo={cloud_mysql_instance_no}"
response = requests.get(full_url, headers=headers)
# Check if the response is successful
if response.status_code == 200:
# Parse the XML response
root = ET.fromstring(response.text)
# Assuming that the fileName is located in a specific element, for example <fileName>FILE_NAME</fileName>
file_name = root.find('.//fileName').text
# API endpoint and method for the second request
api_url_export = "https://ncloud.apigw.ntruss.com/vmysql/v2/exportBackupToObjectStorage"
bucket_name = "user-bucket"
uri_export = f"/vmysql/v2/exportBackupToObjectStorage?regionCode={region_code}&fileName={file_name}&bucketName={bucket_name}&cloudMysqlInstanceNo={cloud_mysql_instance_no}"
# Generate new timestamp and signature for the second request
timestamp_export = str(int(time.time() * 1000))
signature_export = make_signature(access_key, secret_key, method, uri_export, timestamp_export)
# Request headers for the second request
headers_export = {
"Content-Type": "application/json",
"x-ncp-iam-access-key": access_key,
"x-ncp-apigw-timestamp": timestamp_export,
"x-ncp-apigw-signature-v2": signature_export
}
# Send the second request
full_url_export = f"{api_url_export}?regionCode={region_code}&fileName={file_name}&bucketName={bucket_name}&cloudMysqlInstanceNo={cloud_mysql_instance_no}"
response_export = requests.get(full_url_export, headers=headers_export)
# Print the response from the second request
print("Response Status Code:", response_export.status_code)
print("Response Headers:", response_export.headers)
print("Response Body:", response_export.text)
else:
print("Failed to get backup details. Status Code:", response.status_code)
print("Response Body:", response.text)
2. Cloud Functions 의 Cron Trigger 를 활용한 스케줄 설정
- Cloud Functions → Trigger 에서 cron 타입의 Trigger 를 생성한다. 이 떄 Export 를 실행할 스케줄도 설정한다.
- 백업 파일을 Export 할 action 을 저장할 패키지를 생성 후 action도 생성
- Cron 이 정상적으로 수행되면 Object storage 에 백업 파일이 업로드 된 것을 확인할 수 있음
반응형