From 9dd7fa96ee70bc2f66a7ddd54248e7a53b9a068e Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sat, 6 Nov 2021 00:36:19 +0200 Subject: [PATCH] Restore script. --- backup/Dockerfile | 1 + backup/backup | 5 +++- backup/restore | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/backup/Dockerfile b/backup/Dockerfile index fc73888..274cba8 100644 --- a/backup/Dockerfile +++ b/backup/Dockerfile @@ -3,6 +3,7 @@ FROM docker.io/library/alpine:3.14 RUN echo 'https://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories && \ echo 'https://dl-cdn.alpinelinux.org/alpine/edge/community' >> /etc/apk/repositories && \ apk add --update --no-cache \ + findutils \ skopeo \ reg \ ; diff --git a/backup/backup b/backup/backup index fb889fa..fe42de0 100755 --- a/backup/backup +++ b/backup/backup @@ -17,6 +17,8 @@ then exit 1 fi +command -v skopeo >/dev/null || { echo 'skopeo is missing.' >&2; exit 2; } + registry="$1" dest="$2" @@ -44,6 +46,7 @@ NR>2 { } } END { - exit retruncode + if ( exitcode == 1) print "Backup failed for some images." + exit exitcode } ' diff --git a/backup/restore b/backup/restore index 1a24852..6dc6cf0 100755 --- a/backup/restore +++ b/backup/restore @@ -1 +1,62 @@ #!/bin/sh +set -eu + +usage() { + echo "$0: BACKUP_SOURCE REGISTRY_DOMAIN" +} + +if [ "${1:-}" = -h ] || [ "${1:-}" = --help ] +then + usage + exit 0 +fi + +if [ "$#" -ne 2 ] +then + usage + exit 1 +fi + +command -v skopeo >/dev/null || { echo 'skopeo is missing.' >&2; exit 2; } + +src="$1" +registry="$2" + +# There's an assumption here that filenames don't have spaces (or other such +# characters) as the image format prohibits that and I don't want to deal with +# such issues right now. + +images="$(find "$src" -maxdepth 1 -mindepth 1 -type d -printf '%f\n')" +if [ -z "$images" ] +then + echo 'No images found,' >&2 + exit 3 +fi + +returncode=0 +for image in $images +do + tags="$(find "$src/$image" -maxdepth 1 -mindepth 1 -type f -name '*.tar' -printf '%f\n' | sed 's/\.tar$//g')" + if [ -z "$tags" ] + then + echo "No tags found for image $image, skipping." >&2 + continue + fi + for tag in $tags + do + echo "Restoring $image:$tag" >&2 + if skopeo copy "docker-archive://$src/$image/$tag.tar" "docker://$registry/$image:$tag" + then + echo "Restore finished successfully." >&2 + else + echo "Restore failed, continuing with other image." >&2 + returncode=1 + fi + done +done + +if [ "$returncode" -gt 0 ] +then + echo 'Restoration failed for some images.' +fi +exit "$returncode" -- GitLab