Skip to content

Commit

Permalink
Merge pull request #318 from oslokommune/develop
Browse files Browse the repository at this point in the history
Release 2.6.2
  • Loading branch information
aulonm authored Dec 9, 2021
2 parents 1e611a7 + 4c06efd commit 7f5f2a8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format

## [UNRELEASED]

## [2.6.2] 2021-12-09

### Fixed

- Cloud Functions: automatic backup and restore of the database has apparently not been working for a month

## [2.6.1] 2021-11-10

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion functions/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
.runtimeconfig.json
.runtimeconfig-prod.json
.runtimeconfig-dev.json
.runtimeconfig.json
origo-okr-tracker-private-key.json
okr-tracker-production-private-key.json
66 changes: 34 additions & 32 deletions functions/backupAndRestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,50 @@ import config from './config.js';

const storageBucketName = process.env.BACKUP_STORAGE_BUCKET || functions.config().storage.bucket;

export const automatedBackups = () =>
export const automatedBackups =
functions
.region(config.region)
.pubsub.schedule(config.backupFrequency)
.timeZone(config.timeZone)
.onRun(generateBackup);

export const automatedRestore = () => {
return functions.region(config.region).pubsub.topic('restore-backup').onPublish(restoreBackup);
export const automatedRestore =
functions
.region(config.region)
.pubsub.topic('restore-backup')
.onPublish(restoreBackup);

async function restoreBackup() {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/datastore', 'https://www.googleapis.com/auth/cloud-platform'],
});
async function restoreBackup() {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/datastore', 'https://www.googleapis.com/auth/cloud-platform'],
});

const client = await auth.getClient();
const oneDayBefore = new Date();
oneDayBefore.setDate(oneDayBefore.getDate() - 1);
const path = `${oneDayBefore.toISOString().split('T')[0]}`;
const client = await auth.getClient();
const oneDayBefore = new Date();
oneDayBefore.setDate(oneDayBefore.getDate() - 1);
const path = `${oneDayBefore.toISOString().split('T')[0]}`;

const projectId = await auth.getProjectId();
const url = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default):importDocuments`;
const backupRoute = `gs://${storageBucketName}/${path}`;
const projectId = await auth.getProjectId();
const url = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default):importDocuments`;
const backupRoute = `gs://${storageBucketName}/${path}`;

return client
.request({
url,
method: 'POST',
data: {
inputUriPrefix: backupRoute,
},
})
.then(() => {
console.log(`Backup restored from folder ${backupRoute}`);
return Promise.resolve();
})
.catch(async (error) => {
console.error('Error message: ', error.message);
return Promise.reject(new Error({ message: error.message }));
});
}
};
return client
.request({
url,
method: 'POST',
data: {
inputUriPrefix: backupRoute,
},
})
.then(() => {
console.log(`Backup restored from folder ${backupRoute}`);
return Promise.resolve();
})
.catch(async (error) => {
console.error('Error message: ', error.message);
return Promise.reject(new Error({ message: error.message }));
});
}

async function generateBackup() {
const auth = new GoogleAuth({
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "okr-tracker",
"version": "2.6.1",
"version": "2.6.2",
"private": true,
"scripts": {
"dev": "firebase emulators:start --import=./mock_data & vite",
Expand Down

0 comments on commit 7f5f2a8

Please sign in to comment.