-
Notifications
You must be signed in to change notification settings - Fork 2
/
init_borgbase_repository.py
executable file
·145 lines (123 loc) · 3.6 KB
/
init_borgbase_repository.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
from borgbase_api_client.client import GraphQLClient
from borgbase_api_client.mutations import REPO_ADD, SSH_ADD
import os
import sys
import subprocess
import time
os.environ["BORG_PASSPHRASE"] = ""
os.environ["BORG_NEW_PASSPHRASE"] = ""
CONFIG_DIR = "/root/"
BORG_ENCRYPTION = "repokey"
BORGMATIC_CONFIG = CONFIG_DIR + ".config/borgmatic/config.yaml"
def repo_exists(client, name):
query = """
{
repoList {
id
name
}
}
"""
res = client.execute(query)
for repo in res["data"]["repoList"]:
if repo["name"] == name:
return repo["id"]
return ""
def repo_hostname(client, repo_id):
query = """
{
repoList {
id
name
server {
hostname
}
}
}
"""
res = client.execute(query)
for repo in res["data"]["repoList"]:
if repo["id"] == repo_id:
return repo["server"]["hostname"]
def create_repo(client, name):
new_key_vars = {
"name": "Key for " + name,
"keyData": open(CONFIG_DIR + ".ssh/" + name + "_id.pub").readline().strip(),
}
res = client.execute(SSH_ADD, new_key_vars)
new_key_id = res["data"]["sshAdd"]["keyAdded"]["id"]
new_repo_vars = {
"name": name,
"quotaEnabled": False,
"appendOnlyKeys": [new_key_id],
"region": "eu",
"alertDays": 1,
"quota": 2048,
"quotaEnabled": True,
}
res = client.execute(REPO_ADD, new_repo_vars)
return res["data"]["repoAdd"]["repoAdded"]["id"]
def main(borgbase_api_client, name, know_hosts_file):
repo_id = repo_exists(borgbase_api_client, name)
if repo_id:
print("Repo exists with name", name)
else:
print("Repo not exists with name", name, ", create it ...")
repo_id = create_repo(borgbase_api_client, name)
# waiting a couple of second to prevent borgmatic fail
time.sleep(2)
hostname = repo_hostname(borgbase_api_client, repo_id)
repo_path = repo_id + "@" + hostname + ":repo"
with open(KNOWN_HOSTS_FILE, "w") as outfile:
subprocess.run(["ssh-keyscan", "-H", hostname], stdout=outfile)
print("Use repo path :", repo_path)
with open(BORGMATIC_CONFIG, "w") as FILE:
FILE.write(
f"""
location:
source_directories:
- /storage
repositories:
- {repo_path}
storage:
encryption_passphrase: ""
relocated_repo_access_is_ok: true
unknown_unencrypted_repo_access_is_ok: true
borg_base_directory: "/repo"
borg_cache_directory: "/cache"
archive_name_format: '{name}__backup__{{now}}'
retention:
keep_within: 48H
keep_daily: 7
keep_weekly: 4
keep_monthly: 12
keep_yearly: 1
prefix: {name}__backup__
"""
)
print("Init Borgmatic ...")
subprocess.call(
[
"borgmatic",
"-c",
BORGMATIC_CONFIG,
"-v",
"1",
"init",
"--encryption",
BORG_ENCRYPTION,
]
)
if __name__ == "__main__":
TOKEN = os.environ.get("BORGBASE_KEY")
# MYSQL_USER = os.environ.get("MYSQL_USER")
# MYSQL_DB = os.environ.get("MYSQL_DB")
BACKUP_NAME = os.environ.get("BORGBASE_NAME")
KNOWN_HOSTS_FILE = os.environ.get("KNOWN_HOSTS_FILE")
if TOKEN and BACKUP_NAME and KNOWN_HOSTS_FILE:
main(GraphQLClient(TOKEN), BACKUP_NAME, KNOWN_HOSTS_FILE)
else:
sys.exit(
"Environnement variables missing, check BORGBASE_KEY, BORGBASE_NAME and KNOWN_HOSTS_FILE"
)