-
Notifications
You must be signed in to change notification settings - Fork 17
/
ACS::Redis::CreateBackupAndUploadToOSS.yml
130 lines (129 loc) · 3.89 KB
/
ACS::Redis::CreateBackupAndUploadToOSS.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
FormatVersion: OOS-2019-06-01
Description:
en: Create redis backup and upload to OSS
zh-cn: 创建Redis备份并上传到OSS
name-en: ACS::Redis::CreateBackupAndUploadToOSS
name-zh-cn: 创建Redis备份并上传到OSS
Parameters:
regionId:
Label:
en: RegionId
zh-cn: 地域ID
Type: String
AssociationProperty: RegionId
instanceId:
Label:
en: InstanceId
zh-cn: 实例ID
Type: String
AssociationProperty: ALIYUN::Redis::Instance::InstanceId
AssociationPropertyMetadata:
RegionId: ${regionId}
OSSRegionId:
Label:
en: OSSRegionId
zh-cn: OSS bucket所在地域ID
Type: String
AssociationProperty: RegionId
OSSBucketName:
Label:
en: OSSBucketName
zh-cn: OSS Bucket 名称
Type: String
AssociationProperty: ALIYUN::OSS::Bucket::BucketName
AssociationPropertyMetadata:
RegionId: ${OSSRegionId}
Default: ''
Tasks:
- Name: CreateBackup
Action: ACS::ExecuteApi
Description:
en: Create backup
zh-cn: 创建Redis备份
Properties:
Service: r-kvstore
API: CreateBackup
Parameters:
RegionId: '{{ regionId }}'
InstanceId: '{{ instanceId }}'
Outputs:
BackupJobID:
Type: String
ValueSelector: .BackupJobID
- Name: WaitForBackupCreated
Action: ACS::WaitFor
Description:
en: Wait for backup created
zh-cn: 等待备份创建完成
Retries: 30
DelayType: Exponential
Delay: 2
BackOff: 2
Properties:
Service: r-kvstore
API: DescribeBackupTasks
Parameters:
RegionId: '{{ regionId }}'
InstanceId: '{{ instanceId }}'
BackupJobId: '{{ CreateBackup.BackupJobID }}'
DesiredValues:
- Finished
NotDesiredValues: []
StopRetryValues: []
PropertySelector: .BackupJobs[].BackupProgressStatus
- Name: GetBackupDownloadUrl
Action: ACS::ExecuteApi
Description:
en: Get backup download url
zh-cn: 获取备份下载地址
Properties:
Service: r-kvstore
API: DescribeBackups
Parameters:
RegionId: '{{ regionId }}'
InstanceId: '{{ instanceId }}'
BackupJobId: '{{ CreateBackup.BackupJobID }}'
StartTime:
Fn::FormatUTCTime:
- Fn::AddHour:
- '{{ ACS::CurrentUTCTime }}'
- -1
- '%Y-%m-%dT%H:%MZ'
EndTime:
Fn::FormatUTCTime:
- '{{ ACS::CurrentUTCTime }}'
- '%Y-%m-%dT%H:%MZ'
Outputs:
BackupDownloadURL:
Type: List
ValueSelector: .Backups.Backup[].BackupDownloadURL
- Name: UploadBackupToOSS
Action: ACS::FC::ExecuteScript
Description:
en: Upload backup to OSS
zh-cn: 上传备份到OSS
Properties:
runtime: python3.10
handler: index.handler
role: acs:ram::{{ACS::AccountId}}:role/AliyunFcDefaultRole
script: |-
import oss2
import requests
def handler(event, context):
auth = oss2.StsAuth(context.credentials.access_key_id, context.credentials.access_key_secret, context.credentials.security_token)
endpoint = 'https://oss-{{OSSRegionId}}.aliyuncs.com'
bucket = oss2.Bucket(auth, endpoint, '{{OSSBucketName}}')
unique_identifier = '{{ACS::TaskLoopItem}}'.split('?')[0].split('/')[-1]
execution_id = '{{ACS::ExecutionId}}'.split('.')[0]
input = requests.get('{{ACS::TaskLoopItem}}')
bucket.put_object(f'Backup_{{InstanceId}}/{{ACS::CurrentDate}}/{execution_id}/{unique_identifier}', input)
Loop:
Items: '{{ GetBackupDownloadUrl.BackupDownloadURL }}'
RateControl:
Mode: Concurrency
MaxErrors: 0
Concurrency: 20
Outputs:
OSSObjectURL:
Type: String
Value: 'https://oss.console.aliyun.com/bucket/oss-{{OSSRegionId}}/{{OSSBucketName}}/object?path=Backup_{{InstanceId}}/{{ACS::CurrentDate}}/'