-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-tfstate.js
39 lines (33 loc) · 997 Bytes
/
backup-tfstate.js
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
const process = require("process");
const childProcess = require("child_process");
const exec = command => {
childProcess.execSync(command);
};
const pad = x => {
return String(x).length === 1 ? `0${x}` : String(x);
};
const timestamp = () => {
const now = new Date();
const hh = pad(now.getHours());
const mm = pad(now.getMinutes());
const ss = pad(now.getSeconds());
return `[${hh}:${mm}:${ss}]`;
};
const log = message => {
console.log(`${timestamp()} ${message}`);
};
process.env.BORG_RELOCATED_REPO_ACCESS_IS_OK = "yes";
process.chdir(`${__dirname}/../infra`);
const tfstate = "terraform.tfstate";
const repo = ".tfstate-backup";
const gdrive = "gdrive:arrakis-infra/.tfstate-backup";
try {
log("Backing up infra/terraform.tfstate");
exec(`borg create ${repo}::{utcnow} ${tfstate}`);
log("Syncing borg repo infra/.tfstate-backup to Google Drive");
exec(`rclone sync ${repo} ${gdrive}`);
log("Done!");
} catch (error) {
log("Failed");
console.log(error);
}