Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changeable label_selector #12

Merged
merged 5 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ The newest ones are kept and old ones are deleted.
### Add label to servers that should be backed up
All servers that should be included by the script and automatic snapshot backups should be created must have the label `AUTOBACKUP` with the value `true`.

### Change default label to something else
You can change the default label from `AUTOBACKUP` to something else by changing the `label_selector` value in the config. If in a docker environment you can set `--env LABEL_SELECTOR`. When changing the `label_selector`, you must also use this for the `KEEP-LAST` setting.
rdcuzins marked this conversation as resolved.
Show resolved Hide resolved

### Choose a name for your snapshots (optional)
Specify how you want your snapshots to be named in the environment variables under `SNAPSHOT_NAME` or under `snapshot-name` in the config.
By default they are named `<server name>-<timestamp>`.
Expand Down
3 changes: 2 additions & 1 deletion config-example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"label-selector": "AUTOBACKUP",
"api-token": "",
"snapshot-name": "%name%-%timestamp%",
rdcuzins marked this conversation as resolved.
Show resolved Hide resolved
"keep-last": 3
}
}
16 changes: 10 additions & 6 deletions snapshot-as-backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
base_url = "https://api.hetzner.cloud/v1"
api_token = ""
snapshot_name = ""
label_selector = ""
keep_last_default = 3
headers = {}
servers = {}
Expand All @@ -18,7 +19,7 @@


def get_servers(page=1):
url = base_url + "/servers?label_selector=AUTOBACKUP=true&page=" + str(page)
url = base_url + f"/servers?label_selector={label_selector}=true&page=" + str(page)
r = requests.get(url=url, headers=headers)

if not r.ok:
Expand All @@ -33,8 +34,8 @@ def get_servers(page=1):
servers[s['id']] = s

keep_last = keep_last_default
if "AUTOBACKUP.KEEP-LAST" in s['labels']:
keep_last = int(s['labels']['AUTOBACKUP.KEEP-LAST'])
if f"{label_selector}.KEEP-LAST" in s['labels']:
keep_last = int(s['labels'][f"{label_selector}.KEEP-LAST"])

if keep_last < 1:
keep_last = 1
Expand All @@ -49,7 +50,7 @@ def create_snapshot(server_id, snapshot_desc):
url = base_url + "/servers/" + str(server_id) + "/actions/create_image"
r = requests.post(
url=url,
json={"description": snapshot_desc, "type": "snapshot", "labels": {"AUTOBACKUP": ""}},
json={"description": snapshot_desc, "type": "snapshot", "labels": {f"{label_selector}": ""}},
headers=headers
)

Expand All @@ -62,7 +63,7 @@ def create_snapshot(server_id, snapshot_desc):


def get_snapshots(page=1):
url = base_url + "/images?type=snapshot&label_selector=AUTOBACKUP&page=" + str(page)
url = base_url + f"/images?type=snapshot&label_selector={label_selector}&page=" + str(page)
r = requests.get(url=url, headers=headers)

if not r.ok:
Expand Down Expand Up @@ -151,6 +152,8 @@ def run():
IN_DOCKER_CONTAINER = os.environ.get('IN_DOCKER_CONTAINER', False)

if IN_DOCKER_CONTAINER:

label_selector = os.environ.get('LABEL_SELECTOR', 'AUTOBACKUP')
api_token = os.environ.get('API_TOKEN')
snapshot_name = os.environ.get('SNAPSHOT_NAME', "%name%-%timestamp%")
rdcuzins marked this conversation as resolved.
Show resolved Hide resolved
keep_last_default = int(os.environ.get('KEEP_LAST', 3))
Expand Down Expand Up @@ -178,8 +181,9 @@ def run():
with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.json"), "r") as config_file:
config = json.load(config_file)

label_selector = config['label-selector']
api_token = config['api-token']
snapshot_name = config['snapshot-name']
rdcuzins marked this conversation as resolved.
Show resolved Hide resolved
keep_last_default = int(config['keep-last'])

run()
run()