Commit 00f58d0b authored by Kirill Smelkov's avatar Kirill Smelkov

gitlab-backup: pull|restore: Cleanup $tmpd in defer-style

Similarly to previous patch, let's cleanup gitlab-backup temporary
folder always unconditionally in the presence of errors. Keeping $tmpd
on error was not preventing further gitlab-backup run to proceed, but it
can quickly eat up disk space if there are many such runs. If debugging
is needed one can comment the cleanup, but by default let's be
production friendly out of the box.

Based on patch by @alain.takoudjou:
kirr/git-backup!4

Original description from Alain:

---- 8< ----
When script exit, remove tmp backup folder which are not longuer needed.
Keep this folder when backup is failing will contribute to fill the disk
of server. backup.locked is also removed, because we want to
automatically retry gitlab-backup if previous backup failed, without
human action. If the file is not removed automatically, backup is
blocked until someone remove it.
parent 2cc61da3
#!/bin/bash -e #!/bin/bash -e
# pull/restore gitlab data into/from git-backup # pull/restore gitlab data into/from git-backup
# Copyright (C) 2015-2016 Nexedi SA and Contributors. # Copyright (C) 2015-2020 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -110,6 +110,8 @@ backup_pull() { ...@@ -110,6 +110,8 @@ backup_pull() {
# 1. dump all gitlab data except repositories & db # 1. dump all gitlab data except repositories & db
echo " * Dumping gitlab data (except repositories & db)" echo " * Dumping gitlab data (except repositories & db)"
tmpd=$(mktemp -d `pwd`/gitlab-backup.XXXXXX) tmpd=$(mktemp -d `pwd`/gitlab-backup.XXXXXX)
trap 'rm -rf "$tmpd"' EXIT
gitlab-rake gitlab:backup:create SKIP=repositories,db | tee "$tmpd/gitlab_backup_create.out" gitlab-rake gitlab:backup:create SKIP=repositories,db | tee "$tmpd/gitlab_backup_create.out"
backup_tar=`grep "^Creating backup archive: .* done" "$tmpd/gitlab_backup_create.out"` || \ backup_tar=`grep "^Creating backup archive: .* done" "$tmpd/gitlab_backup_create.out"` || \
die "E: Cannot detect backup tar" die "E: Cannot detect backup tar"
...@@ -218,7 +220,6 @@ backup_pull() { ...@@ -218,7 +220,6 @@ backup_pull() {
# mark backup_tar as pulled and cleanup # mark backup_tar as pulled and cleanup
mv "$backup_tar" "$backup_tar.pulled" mv "$backup_tar" "$backup_tar.pulled"
fi fi
rm -rf "$tmpd"
echo OK echo OK
} }
...@@ -235,6 +236,7 @@ backup_restore() { ...@@ -235,6 +236,7 @@ backup_restore() {
# 1. extract all gitlab data except repositories # 1. extract all gitlab data except repositories
echo " * Extracting gitlab data (except repositories)" echo " * Extracting gitlab data (except repositories)"
tmpd=$(mktemp -d `pwd`/gitlab-backup.XXXXXX) tmpd=$(mktemp -d `pwd`/gitlab-backup.XXXXXX)
trap 'rm -rf "$tmpd"' EXIT
$GIT_BACKUP restore $HEAD gitlab/misc:"$tmpd/gitlab_backup" $GIT_BACKUP restore $HEAD gitlab/misc:"$tmpd/gitlab_backup"
backup_info="$tmpd/gitlab_backup/backup_information.yml" backup_info="$tmpd/gitlab_backup/backup_information.yml"
...@@ -308,8 +310,6 @@ backup_restore() { ...@@ -308,8 +310,6 @@ backup_restore() {
test -e "$backup_tar" && die "E: $backup_tar already exists" test -e "$backup_tar" && die "E: $backup_tar already exists"
tar -C "$tmpd/gitlab_backup" -cf "$backup_tar" . tar -C "$tmpd/gitlab_backup" -cf "$backup_tar" .
rm -rf "$tmpd" # tmpd no longer needed
# 4. extract repositories into .../repositories.<timestamp> # 4. extract repositories into .../repositories.<timestamp>
echo " * Extracting repositories" echo " * Extracting repositories"
reposX="${GITLAB_REPOS_PATH}.${backup_created_at}" reposX="${GITLAB_REPOS_PATH}.${backup_created_at}"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment