Add a backblaze backup script

This commit is contained in:
Wojciech Kozlowski 2019-12-14 16:51:01 +01:00
parent 2c3c38f106
commit 92970a673f
6 changed files with 67 additions and 19 deletions

10
ansible/b2.cred.j2 Normal file
View File

@ -0,0 +1,10 @@
#!/bin/sh
# Backblaze B2 configuration variables
B2_ACCOUNT="{{ b2_key_id }}"
B2_KEY="{{ b2_app_key }}"
B2_BUCKET="loki-backup"
# GPG key (last 8 characters)
GPG_KEY="{{ gpg_key_id }}"
PASSPHRASE="{{ gpg_passphrase }}"

View File

@ -6,3 +6,4 @@ WorkingDirectory={{ loki_dir }}
Type=oneshot
ExecStartPre={{ loki_dir }}/registry-cleaner.sh
ExecStart={{ loki_dir }}/backup.sh -b
ExecStartPost={{ loki_dir }}/b2-backup.sh

View File

@ -58,27 +58,15 @@
git:
repo: https://github.com/Wojtek242/loki.git
dest: "{{ loki_dir }}"
update: no
register: loki_git
- block:
- name: Update
command: ./update.sh
args:
chdir: "{{ loki_dir }}"
rescue:
- debug:
msg: "Failed to pull containers from registry - will build locally"
- name: Build locally
- name: Build containers locally
command: make build-all
args:
chdir: "{{ loki_dir }}"
when: loki_git is changed
# Hosts file must be added after the first update as otherwise the initial
# container pull will always fail
- name: Add hosts file
template:
src: ./etc/hosts.j2
@ -105,12 +93,22 @@
# Loki backup service.
# -------------------------------------------------------------------------
- name: Install duplicity
apt:
name: duplicity
- name: Create GitLab credentials file
template:
src: ./gitlab.cred.j2
dest: "{{ loki_dir }}/gitlab.cred"
mode: 0644
- name: Create B2 credentials file
template:
src: ./b2.cred.j2
dest: "{{ loki_dir }}/b2.cred"
mode: 0644
- name: Install Loki backup service
template:
src: ./etc/systemd/system/loki-backup.service.j2

View File

@ -38,3 +38,9 @@ domains:
loki_dir:
gitlab_username:
gitlab_access_token:
# Backblaze setup
b2_key_id:
b2_app_key:
gpg_key_id:
gpg_passphrase:

22
b2-backup.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/sh
# Import all account and GPG variables
source ./b2.cred
# Local directory to backup
LOCAL_DIR="/media/usb0/backup"
# Perform a full backup
duplicity full \
--encrypt-sign-key $GPG_KEY \
${LOCAL_DIR} b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
# Cleanup failures
duplicity cleanup --force \
--encrypt-sign-key $GPG_KEY \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
# Show collection-status
duplicity collection-status \
--encrypt-sign-key $GPG_KEY \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}

View File

@ -11,6 +11,8 @@ NC='\033[00m'
SCRIPT=$(readlink -f $0)
DIRNAME=$(dirname $SCRIPT)
BACKUP_DIR="/media/usb0/backup"
# -----------------------------------------------------------------------------
# Get the list of volumes.
# -----------------------------------------------------------------------------
@ -81,6 +83,15 @@ function backup {
volumes=$1
# Remove old backup directory
if [ ! -d ${BACKUP_DIR} ]; then
rm -f ${BACKUP_DIR}/*.tar
rmdir ${BACKUP_DIR}
fi
# Make sure directory exists
mkdir ${BACKUP_DIR}
for vol in "${volumes[@]}"
do
echo -e "${CYAN}[${SCRIPT}] Back up ${YELLOW}${vol}${CYAN} volume${NC}"
@ -88,7 +99,7 @@ function backup {
set -o xtrace
docker run --rm \
-v loki_${vol}:/opt/${vol} \
-v /media/usb0:/opt/backup \
-v ${BACKUP_DIR}:/opt/backup \
debian:stable-slim \
bash -c "cd /opt/${vol} && tar cf /opt/backup/${vol}.tar ."
set +o xtrace
@ -111,7 +122,7 @@ function restore {
set -o xtrace
docker run --rm \
-v loki_${vol}:/opt/${vol} \
-v /media/usb0:/opt/backup \
-v ${BACKUP_DIR}:/opt/backup \
debian:stable-slim \
bash -c "cd /opt/${vol} && tar xf /opt/backup/${vol}.tar"
set +o xtrace