forked from coop-cloud/backup-bot-two
Compare commits
19 Commits
Author | SHA1 | Date |
---|---|---|
3wordchant | 68e37f5c23 | |
3wc | 4d39d84733 | |
3wc | e5b9bc0446 | |
3wc | ec4c4509dc | |
3wc | 26162a9e38 | |
3wc | bd581fd8d7 | |
3wc | e77432e3ab | |
3wc | 001a654e37 | |
3wc | c5574edc54 | |
3wc | 50e4d68717 | |
3wc | c7830ceb6f | |
3wc | b6f859efbb | |
Moritz | 7f14698824 | |
Moritz | 2a9a98172f | |
Moritz | 282215cf9c | |
Moritz | ae7a14b6f1 | |
Moritz | 8acdb20e5b | |
Moritz | 5582744073 | |
3wc | 84d606fa80 |
21
.drone.yml
21
.drone.yml
|
@ -2,11 +2,16 @@
|
|||
kind: pipeline
|
||||
name: linters
|
||||
steps:
|
||||
- name: run shellcheck
|
||||
image: koalaman/shellcheck-alpine
|
||||
commands:
|
||||
- shellcheck backup.sh
|
||||
|
||||
trigger:
|
||||
branch:
|
||||
- main
|
||||
- name: publish image
|
||||
image: plugins/docker
|
||||
settings:
|
||||
username: 3wordchant
|
||||
password:
|
||||
from_secret: git_coopcloud_tech_token_3wc
|
||||
repo: git.coopcloud.tech/coop-cloud/backup-bot-two
|
||||
tags: 2.0.0
|
||||
registry: git.coopcloud.tech
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- pull_request
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# Change log
|
||||
|
||||
## 2.0.0 (unreleased)
|
||||
|
||||
- Rewrite from Bash to Python
|
||||
- Add support for push notifications (#24)
|
|
@ -0,0 +1,11 @@
|
|||
FROM docker:24.0.7-dind
|
||||
|
||||
RUN apk add --upgrade --no-cache restic bash python3 py3-pip py3-click py3-docker-py py3-json-logger curl
|
||||
|
||||
# Todo use requirements file with specific versions
|
||||
RUN pip install --break-system-packages resticpy==1.0.2
|
||||
|
||||
COPY backupbot.py /usr/bin/backup
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
|
||||
ENTRYPOINT /entrypoint.sh
|
1
abra.sh
1
abra.sh
|
@ -1,4 +1,3 @@
|
|||
export ENTRYPOINT_VERSION=v1
|
||||
export BACKUPBOT_VERSION=v1
|
||||
export SSH_CONFIG_VERSION=v1
|
||||
|
||||
|
|
19
backupbot.py
19
backupbot.py
|
@ -42,7 +42,7 @@ sys.excepthook = handle_exception
|
|||
@click.option('-l', '--log', 'loglevel')
|
||||
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
||||
@click.option('service', '--host', '-h', envvar='SERVICE')
|
||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY', required=True)
|
||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY')
|
||||
def cli(loglevel, service, repository, machine_logs):
|
||||
global SERVICE
|
||||
if service:
|
||||
|
@ -54,21 +54,25 @@ def cli(loglevel, service, repository, machine_logs):
|
|||
if not isinstance(numeric_level, int):
|
||||
raise ValueError('Invalid log level: %s' % loglevel)
|
||||
logger.setLevel(numeric_level)
|
||||
logHandler = logging.StreamHandler()
|
||||
if machine_logs:
|
||||
logHandler = logging.StreamHandler()
|
||||
formatter = jsonlogger.JsonFormatter(
|
||||
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
||||
logHandler.setFormatter(formatter)
|
||||
logger.addHandler(logHandler)
|
||||
logger.addHandler(logHandler)
|
||||
|
||||
export_secrets()
|
||||
init_repo()
|
||||
|
||||
|
||||
def init_repo():
|
||||
repo = os.environ['RESTIC_REPOSITORY']
|
||||
if repo:= os.environ.get('RESTIC_REPOSITORY_FILE'):
|
||||
# RESTIC_REPOSITORY_FILE and RESTIC_REPOSITORY are mutually exclusive
|
||||
del os.environ['RESTIC_REPOSITORY']
|
||||
else:
|
||||
repo = os.environ['RESTIC_REPOSITORY']
|
||||
restic.repository = repo
|
||||
logger.debug(f"set restic repository location: {repo}")
|
||||
restic.repository = repo
|
||||
restic.password_file = '/var/run/secrets/restic_password'
|
||||
try:
|
||||
restic.cat.config()
|
||||
|
@ -146,7 +150,7 @@ def copy_secrets(apps):
|
|||
if (app_name in apps and
|
||||
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
||||
if not container_by_service.get(s.name):
|
||||
logger.error(
|
||||
logger.warning(
|
||||
f"Container {s.name} is not running, secrets can not be copied.")
|
||||
continue
|
||||
container_id = container_by_service[s.name].id
|
||||
|
@ -157,6 +161,7 @@ def copy_secrets(apps):
|
|||
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
||||
continue
|
||||
dst = SECRET_PATH + sec['SecretName']
|
||||
logger.debug("Copy Secret {sec['SecretName']}")
|
||||
copyfile(src, dst)
|
||||
|
||||
|
||||
|
@ -184,6 +189,8 @@ def run_commands(commands):
|
|||
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
||||
while True:
|
||||
try:
|
||||
logger.info("Start volume backup")
|
||||
logger.debug(backup_paths)
|
||||
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
||||
logger.summary("backup finished", extra=result)
|
||||
return
|
||||
|
|
20
compose.yml
20
compose.yml
|
@ -2,7 +2,7 @@
|
|||
version: "3.8"
|
||||
services:
|
||||
app:
|
||||
image: docker:24.0.7-dind
|
||||
image: git.coopcloud.tech/coop-cloud/backup-bot-two:2.0.0
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock"
|
||||
- "/var/lib/docker/volumes/:/var/lib/docker/volumes/"
|
||||
|
@ -19,15 +19,7 @@ services:
|
|||
- coop-cloud.${STACK_NAME}.version=0.1.0+latest
|
||||
- coop-cloud.${STACK_NAME}.timeout=${TIMEOUT:-300}
|
||||
- coop-cloud.backupbot.enabled=true
|
||||
configs:
|
||||
- source: entrypoint
|
||||
target: /entrypoint.sh
|
||||
mode: 0555
|
||||
- source: backupbot
|
||||
target: /usr/bin/backup
|
||||
mode: 0555
|
||||
entrypoint: ['/entrypoint.sh']
|
||||
#entrypoint: ['tail', '-f','/dev/null']
|
||||
#entrypoint: ['tail', '-f','/dev/null']
|
||||
healthcheck:
|
||||
test: "pgrep crond"
|
||||
interval: 30s
|
||||
|
@ -42,11 +34,3 @@ secrets:
|
|||
|
||||
volumes:
|
||||
backups:
|
||||
|
||||
configs:
|
||||
entrypoint:
|
||||
name: ${STACK_NAME}_entrypoint_${ENTRYPOINT_VERSION}
|
||||
file: entrypoint.sh
|
||||
backupbot:
|
||||
name: ${STACK_NAME}_backupbot_${BACKUPBOT_VERSION}
|
||||
file: backupbot.py
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e -o pipefail
|
||||
|
||||
apk add --upgrade --no-cache restic bash python3 py3-pip py3-click py3-docker-py py3-json-logger curl
|
||||
|
||||
# Todo use requirements file with specific versions
|
||||
pip install --break-system-packages resticpy==1.0.2
|
||||
set -e
|
||||
|
||||
if [ -n "$SSH_HOST_KEY" ]
|
||||
then
|
||||
|
|
Loading…
Reference in New Issue