Skip to content

Commit d69b548

Browse files
committed
backup
1 parent dde68f5 commit d69b548

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

data-migration/campaign/src/script.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';
1313
import { $TemplateDtoSchema } from 'nhs-notify-backend-client';
1414
import { z } from 'zod/v4';
1515
import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
16+
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
1617

1718
const $ExpectedTemplate = z.intersection(
1819
$TemplateDtoSchema,
@@ -41,6 +42,8 @@ const sqs = new SQSClient({ region: 'eu-west-2' });
4142

4243
const sts = new STSClient({ region: 'eu-west-2' });
4344

45+
const s3 = new S3Client({ region: 'eu-west-2' });
46+
4447
export async function getAccountId(): Promise<string> {
4548
const callerIdentity = await sts.send(new GetCallerIdentityCommand());
4649
const accountId = callerIdentity.Account;
@@ -50,6 +53,32 @@ export async function getAccountId(): Promise<string> {
5053
return accountId;
5154
}
5255

56+
export async function backupData(
57+
items: Record<string, unknown>[],
58+
clientId: string,
59+
newCampaignId: string,
60+
accountId: string
61+
): Promise<void> {
62+
console.log(`Found ${items.length} results`);
63+
if (items.length <= 0) {
64+
return;
65+
}
66+
67+
const timestamp = new Date().toISOString().replaceAll(/[.:T-]/g, '_');
68+
const bucketName = `nhs-notify-${accountId}-eu-west-2-main-acct-migration-backup`;
69+
const filePath = `campaign-transfer/${environment}/${timestamp}-${clientId}-${newCampaignId}.json`;
70+
71+
s3.send(
72+
new PutObjectCommand({
73+
Bucket: bucketName,
74+
Key: filePath,
75+
Body: JSON.stringify(items),
76+
ContentType: 'application/json',
77+
})
78+
);
79+
console.log(`Backed up data to s3://${bucketName}/${filePath}`);
80+
}
81+
5382
const { environment, file, newCampaignId, clientId, supplier, realRun } = yargs(
5483
hideBin(process.argv)
5584
)
@@ -93,6 +122,27 @@ async function main() {
93122
.map((s) => s.trim())
94123
.filter(Boolean);
95124

125+
const templates: Record<string, unknown>[] = [];
126+
127+
for (const id of templateIds) {
128+
const key = {
129+
id,
130+
owner: `CLIENT#${clientId}`,
131+
};
132+
133+
const getResult = await ddb.send(
134+
new GetCommand({ TableName: tableName, Key: key })
135+
);
136+
137+
const template = $ExpectedTemplate.parse(getResult.Item);
138+
139+
templates.push(template);
140+
}
141+
142+
if (realRun) {
143+
await backupData(templates, clientId, newCampaignId, acct);
144+
}
145+
96146
for (const id of templateIds) {
97147
const key = {
98148
id,

0 commit comments

Comments
 (0)