{"id":13702586,"url":"https://github.com/fbrettnich/hcloud-snapshot-as-backup","last_synced_at":"2025-05-05T04:31:16.852Z","repository":{"id":43343891,"uuid":"382615960","full_name":"fbrettnich/hcloud-snapshot-as-backup","owner":"fbrettnich","description":"Hetzner Cloud - Automatic Snapshots as Backups for more flexibility","archived":false,"fork":false,"pushed_at":"2024-10-21T15:36:05.000Z","size":584,"stargazers_count":103,"open_issues_count":3,"forks_count":19,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-22T02:41:07.635Z","etag":null,"topics":["backup","backup-script","backups","cloud","cron","docker","docker-compose","hacktoberfest","hetzner","hetzner-api","hetzner-cloud","labels","python","snapshot","snapshots"],"latest_commit_sha":null,"homepage":"https://hub.docker.com/r/fbrettnich/hcloud-snapshot-as-backup","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fbrettnich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://hetzner.cloud/?ref=J8cY7mGeB78f"]}},"created_at":"2021-07-03T12:57:16.000Z","updated_at":"2024-10-21T15:36:08.000Z","dependencies_parsed_at":"2024-01-14T19:14:11.616Z","dependency_job_id":"38d5a605-cab4-4a1a-aeed-8d0bf493f258","html_url":"https://github.com/fbrettnich/hcloud-snapshot-as-backup","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbrettnich%2Fhcloud-snapshot-as-backup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbrettnich%2Fhcloud-snapshot-as-backup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbrettnich%2Fhcloud-snapshot-as-backup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbrettnich%2Fhcloud-snapshot-as-backup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fbrettnich","download_url":"https://codeload.github.com/fbrettnich/hcloud-snapshot-as-backup/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224424605,"owners_count":17308925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backup","backup-script","backups","cloud","cron","docker","docker-compose","hacktoberfest","hetzner","hetzner-api","hetzner-cloud","labels","python","snapshot","snapshots"],"created_at":"2024-08-02T21:00:38.572Z","updated_at":"2024-11-13T09:31:04.024Z","avatar_url":"https://github.com/fbrettnich.png","language":"Python","funding_links":["https://hetzner.cloud/?ref=J8cY7mGeB78f"],"categories":["Tools"],"sub_categories":["Rust"],"readme":"# Hetzner Cloud | Snapshot-as-Backup  \nThis script automatically creates snapshots of your Hetzner Cloud Servers and deletes the old ones.  \nSnapshots then work like the automatic backups offered directly by Hetzner, with the advantage that more backups and at self-defined times can be created.  \nFor ease of use, the script works with the powerful labels directly in the Hetzner Cloud Console. Just add labels to the servers and you are done.  \n\n- [Installation](#installation)\n  - [With Docker (Recommended)](#installation-with-docker-recommended)\n  - [With Docker Compose](#installation-with-docker-compose)\n  - [Without Docker](#installation-without-docker)\n- [Configuration](#configuration)\n  - [Generate Hetzner Cloud Console API-Key](#generate-hetzner-cloud-console-api-key)\n  - [Choose how many backups you want to keep by default](#choose-how-many-backups-you-want-to-keep-by-default)\n  - [Add label to servers that should be backed up](#add-label-to-servers-that-should-be-backed-up)\n  - [Choose a name for your snapshots (optional)](#choose-a-name-for-your-snapshots-optional)\n  - [Docker: Specify how often the script should be executed via cron](#docker-specify-how-often-the-script-should-be-executed-via-cron)\n- [About Labels](#about-labels)\n- [Why is this script useful?](#why-is-this-script-useful)\n\n## Installation\n\n### Installation: With Docker (Recommended)\n**1. Run the docker image**  \n```\ndocker run -d --name hcloud-snapshot-as-backup \\\n  --restart unless-stopped \\\n  -v /etc/localtime:/etc/localtime:ro \\\n  -e API_TOKEN= \\\n  -e SNAPSHOT_NAME=\"%name%-%timestamp%\" \\\n  -e LABEL_SELECTOR=\"AUTOBACKUP\" \\\n  -e KEEP_LAST=3 \\\n  -e CRON=\"0 1 * * *\" \\\n  fbrettnich/hcloud-snapshot-as-backup\n```\n\nPut your [Hetzner Cloud Console API-Key](#generate-hetzner-cloud-console-api-key) after `API_TOKEN=` in line 4.\n\nOptional: Set `CRON` to `false` to disable CronScheduler in the container and schedule outside of the container, especially for using services like [Amazon ECS scheduled tasks](https://docs.aws.amazon.com/AmazonECS/latest/userguide/scheduled_tasks.html) or [Google Cloud Run jobs](https://cloud.google.com/run/docs/create-jobs).\n\n---\n\n### Installation: With Docker Compose\n**1. Download [docker-compose.yml](https://github.com/fbrettnich/hcloud-snapshot-as-backup/blob/main/docker-compose.yml)**  \n```\ncurl -sSL https://raw.githubusercontent.com/fbrettnich/hcloud-snapshot-as-backup/main/docker-compose.yml \u003e docker-compose.yml\n```\n\n**2. Edit configuration**  \nOpen the `docker-compose.yml` file to complete the configuration.  \nYou can find more information about the configuration below ([click here](#configuration))\n\n\n**3. Run docker-compose**  \n````\ndocker-compose up -d\n````\n\n---\n\n### Installation: Without Docker\n**1. Clone this repository**  \n```\napt install git\ngit clone https://github.com/fbrettnich/hcloud-snapshot-as-backup.git /opt/hcloud-snapshot-as-backup\n```\n\n**2. Install requirements**  \n```\napt install python3 python3-pip\npip3 install -r /opt/hcloud-snapshot-as-backup/requirements.txt\n```\n\n**3. Copy config.json**  \n```\ncd /opt/hcloud-snapshot-as-backup/\ncp config-example.json config.json\n```\n\n**4. Edit configuration**  \nOpen the `config.json` file to complete the configuration.  \nYou can find more information about the configuration below ([click here](#configuration))\n\n**5. Run script**  \n```\npython3 /opt/hcloud-snapshot-as-backup/snapshot-as-backup.py\n```\n\nTo execute the script automatically, you can create a crontab: [Crontab Generator](https://crontab-generator.org/)\n\n````\ncrontab -e\n````\n\nAdd the crontab at the end of the file:  \n```\n0 1 * * * python3 /opt/hcloud-snapshot-as-backup/snapshot-as-backup.py \u003e/dev/null 2\u003e\u00261\n```\nThis crontab executes the script every day at 1am.  \n\n## Configuration  \nDepending on how you run the script, you will need to configure.  \nWhen you run the script using Docker, the options are set by environment variables.  \nWhen you run the script via command/crontab, the options are set via the configuration file ([`config.json`](https://github.com/fbrettnich/hcloud-snapshot-as-backup/blob/main/config-example.json)).  \n\n### Generate Hetzner Cloud Console API-Key  \n1. Login to [Hetzner Cloud Console](https://console.hetzner.cloud/)\n2. Select your project\n3. Click \"Access\" on the left side\n4. Switch to \"API-Tokens\" at the top\n5. Click on \"Create API Token\" and create a new token with read \u0026 write permission\n6. Copy the key and paste it into the environment variables under `API_TOKEN` or in the config under `api-token`\n\n![Create Hetzner API Token Gif](https://raw.githubusercontent.com/fbrettnich/hcloud-snapshot-as-backup/main/.github/images/create-hetzner-api-token.gif)\n\n### Choose how many backups you want to keep by default  \nYou can specify in the environment variables under `KEEP_LAST` or in the config under `keep-last` how many backups you want to keep per server by default.  \nLabels can be used to overwrite the default value directly in the Hetzner Cloud Console.  \nAdd the label `AUTOBACKUP.KEEP-LAST` with the value that should apply to the server.  \nThe newest ones are kept and old ones are deleted.  \n\n### Add label to servers that should be backed up  \nAll servers that should be included by the script and automatic snapshot backups should be created must have the label `AUTOBACKUP` with the value `true`.  \n\n### Change default label to something else\nYou 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 the `LABEL_SELECTOR` environment variable. When changing the `label_selector`, you must also use this for the `KEEP-LAST` setting. \n\n### Choose a name for your snapshots (optional)  \nSpecify how you want your snapshots to be named in the environment variables under `SNAPSHOT_NAME` or under `snapshot-name` in the config.  \nBy default they are named `\u003cserver name\u003e-\u003ctimestamp\u003e`.  \n\n| Placeholder | Value                     |\n|-------------|---------------------------|\n| %id%        | ID of the server          |\n| %name%      | Name of the server        |\n| %timestamp% | Current timestamp         |\n| %date%      | Current date (`%Y-%m-%d`) |\n| %time%      | Current time (`%H:%M:%S`) |\n\n### Docker: Specify how often the script should be executed via cron\nIf you run the script in Docker, a cron may be specified in the environment variables under `CRON`.  \nThe cron is used to define how often and at which times the script should be executed.  \nOptionally, set `CRON` to `false` to disable CronScheduler in the container and schedule outside of the container.\n\nHere you can create a cron: [Cron Generator](https://crontab.guru/#0_1_*_*_*)  \n\nFor example:  \n```\n0 1 * * *\n```\nThis cron executes the script every day at 1am.  \n\n## About Labels  \nThis script works with the powerful Hetzner Labels.  \n- **Server**\n  - `AUTOBACKUP`\n    - Server with label `AUTOBACKUP` and value `true` are included by the script\n    - This makes it possible to specify directly in the Hetzner Cloud Console for which servers automatic snapshot backups should be created\n  - `AUTOBACKUP.KEEP-LAST` (optional)\n    - The label `AUTOBACKUP.KEEP-LAST` can be used to specify how many backups should be kept for this server\n    - If this label is not specified, the default value from the config is used\n- **Snapshots**\n  - `AUTOBACKUP`\n    - Snapshots are provided with the label `AUTOBACKUP`\n    - This makes it possible to see which snapshot was automatically created by the script\n    - The script ignores all snapshots without this label (Therefore, only old snapshots with this label are deleted)\n    - If you want to keep an automatically generated snapshot, just remove the label `AUTOBACKUP`\n\n![Server-Labels](https://raw.githubusercontent.com/fbrettnich/hcloud-snapshot-as-backup/main/.github/images/server-labels.png \"Hetzner Cloud Console: Server Labels\")\n\n## Why is this script useful?  \n| Advantages                                                                                                                                                                                                                                            | Disadvantages                                                                                                                                    |\n| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |\n| [Snapshot Backups can be created at own times (Backups at given time)](https://docs.hetzner.com/cloud/servers/backups-snapshots/faq/#can-i-pick-my-backup-time)                                                                                       | Snapshots are limited (but higher limit can be requested)                                                                                        |\n| [Any number can be kept (Backups only maximum 7)](https://docs.hetzner.com/cloud/servers/backups-snapshots/overview#limits)                                                                                                                           | [May be more expensive for extremely large servers (lots of storage)](https://docs.hetzner.com/cloud/servers/backups-snapshots/overview#pricing) |\n| [Are cheaper than backups for smaller servers (less storage space)](https://docs.hetzner.com/cloud/servers/backups-snapshots/overview#pricing)                                                                                                        |                                                                                                                                                  |\n| Are not server bound (Backups will be deleted if you delete the cloud server)                                                                                                                                                                         |                                                                                                                                                  |\n| New servers can be created directly from the snapshot                                                                                                                                                                                                 |                                                                                                                                                  |\n| [Snapshots are stored in a different location (same network zone) than the cloud server (Backups only in the same location as the cloud)](https://docs.hetzner.com/cloud/servers/backups-snapshots/faq/#what-location-are-backupssnapshots-stored-in) |                                                                                                                                                  |\n| [Snapshots can be moved to another project](https://docs.hetzner.com/cloud/servers/backups-snapshots/faq/#are-backupssnapshots-moveable)                                                                                                              |                                                                                                                                                  |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbrettnich%2Fhcloud-snapshot-as-backup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffbrettnich%2Fhcloud-snapshot-as-backup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbrettnich%2Fhcloud-snapshot-as-backup/lists"}