From fd32511cf5602dd72f6f6da98c85d8234c596b1e Mon Sep 17 00:00:00 2001 From: RealStickman Date: Sun, 10 Apr 2022 14:56:21 +0200 Subject: [PATCH] Thunderbird backup and restore scripts --- .../scripts/pieces/thunderbird-backup.sh | 55 +++++++++++++++++++ .../scripts/pieces/thunderbird-restore.sh | 34 ++++++++++++ 2 files changed, 89 insertions(+) create mode 100755 arch-config/scripts/pieces/thunderbird-backup.sh create mode 100755 arch-config/scripts/pieces/thunderbird-restore.sh diff --git a/arch-config/scripts/pieces/thunderbird-backup.sh b/arch-config/scripts/pieces/thunderbird-backup.sh new file mode 100755 index 00000000..604899cc --- /dev/null +++ b/arch-config/scripts/pieces/thunderbird-backup.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -euo pipefail + +# prompt for password +echo -n "Password: " +read -s -r pass +echo + +# prompt a second time +echo -n "Repeat Password: " +read -s -r pass2 +echo + +# check correctness +if [ ! "$pass" = "$pass2" ]; then + echo "Passwords don't match!" + exit +fi + +# go to home dir +cd "$HOME" + +# get current date +currdate="$(date +%Y-%m-%d)" + +# create backup directory +mkdir -p "$HOME/thunderbird-backup/default-release" + +# copy stuff to backup directory +# * breaks with quotes +cp -r $HOME/.thunderbird/*.default-release/* "$HOME/thunderbird-backup/default-release" + +# create archive from backup +tar -cv -I"zstd -19 -T0" -f thunderbird-backup-${currdate}.tar.zst thunderbird-backup/default-release + +# remove backup dir +rm -rf "$HOME/thunderbird-backup" + +# encrypt backup archive +echo '$pass' | gpg -c --batch --yes --passphrase-fd 0 thunderbird-backup-${currdate}.tar.zst + +# remove unencrypted archive +rm thunderbird-backup-${currdate}.tar.zst + +# put encrypted archive into backups folder +mv thunderbird-backup-${currdate}.tar.zst.gpg "$HOME/Nextcloud/backups/" + +# remove more than the last 3 backups +#find "$HOME/Nextcloud/backups/" -name "thunderbird-backup-*\.tar.zst.gpg" | sort -r | tail -n +4 +mapfile -t old_backups < <( find "$HOME/Nextcloud/backups/" -name "thunderbird-backup-*\.tar.zst.gpg" | sort -r | tail -n +4 ) + +for backup in "${old_backups[@]}"; do + echo "Removing old backup. $backup" + rm "$backup" +done diff --git a/arch-config/scripts/pieces/thunderbird-restore.sh b/arch-config/scripts/pieces/thunderbird-restore.sh new file mode 100755 index 00000000..84de8038 --- /dev/null +++ b/arch-config/scripts/pieces/thunderbird-restore.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +set -euo pipefail + +# prompt for password +echo -n "Password: " +read -s -r pass +echo + +# go to home dir +cd "$HOME" + +# find latest backup version +latestbackup="$(find "$HOME/Nextcloud/backups/" -name "thunderbird-backup-*\.tar.zst.gpg" | sort | tail -1)" + +# decrypt backup +echo '$pass' | gpg --decrypt-file --batch --yes --passphrase-fd 0 "$latestbackup" + +# name of decrypted file +latestdecrypted="${latestbackup%.gpg}" + +# expand archive +tar -xvf "$latestdecrypted" + +# remove unencrypted archive +rm "$latestdecrypted" + +# remove current thunderbird config +rm -rf $HOME/.thunderbird/*.default-release/* + +# copy configuration +cp -r $HOME/thunderbird-backup/default-release/* $HOME/.thunderbird/*.default-release/ + +# remove folder +rm -rf "$HOME/thunderbird-backup"