Finish Borg script #2

Merged
lhahn merged 4 commits from borg into main 2024-07-25 21:55:47 +02:00
6 changed files with 159 additions and 53 deletions

View File

@ -17,6 +17,12 @@ backup_run_folder: "{{ cloud_storage }}"
backup_storage_key: "MyStorageKey"
backup_client: true
backup_host: "example"
backup_app: app
backup_cron_specialtime: "daily"
backup_cron_owner: "{{ backup_owner }}"
backup_cron_owner: "{{ backup_owner }}"
backup_script:
prework_backup: |
echo "This is executed before borg backup. Please collect data for backup in path: {{ backup_storage }}"
postwork_restore: |
echo "This is executed after borg restore. Please collect data during restore from path: {{ backup_storage }}"

View File

@ -32,15 +32,15 @@
- name: setup backup script
template:
src: "./usr/local/bin/{{ backup_host }}-backup.sh.j2"
dest: "/usr/local/bin/backup.sh"
src: "./usr/local/bin/cloud_backup.j2"
dest: "/usr/local/bin/cloud_backup"
owner: "{{ backup_owner }}"
group: "{{ backup_group }}"
mode: "0750"
- name: setup cron backup job
cron:
name: "{{ backup_host }} backup"
name: "cloud backup"
user: "{{ backup_cron_owner }}"
job: "/usr/local/bin/backup.sh"
job: "/usr/local/bin/cloud_backup backup"
special_time: "{{ backup_cron_specialtime }}"

View File

@ -0,0 +1,147 @@
#!/bin/bash
BORGUSER="{{ backup_owner }}";
RUNFOLDER="{{ backup_run_folder }}";
TARGETFOLDER="{{ backup_storage }}";
REPOLOCATION="{{ backup_location }}";
ARCHIVENAME="{{ backup_app }}-$(date '+%s')";
help (){
echo "cloud_backup - backup and restore script on cloud with borg target v1.0 by L.Hahn.
Usage: $0 COMMAND [ARCHIVENAME]
COMMAND:
- list List available archives in remote borg repository for your host.
- backup Perform backup of your host and create a new archive in borg repository.
- restore [ARCHIVENAME] Download backup from borg repository to your host and restore files.
May turn off your application if still running.
If no ARCHIVENAME is provided, the latest one based on timestamp is taken.
IF ARCHIVENAME is provided, will try to download it; throws error if not found.
";
}
### INDIVIDUAL TEMPLATE PART ###
prework_backup () {
{{ backup_script.prework_backup | indent( width=4) }}
}
postwork_restore () {
{{ backup_script.postwork_restore | indent( width=4) }}
}
### SHARED TEMPLATE PART ###
get_archives () {
ARCHIVEIDS=$(
sudo -H -u $BORGUSER bash -c '
REPOLOCATION='$REPOLOCATION';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg list $REPOLOCATION' | grep {{ backup_app }} | sort -r);
echo "$ARCHIVEIDS";
}
cloud_backup () {
mkdir -p $TARGETFOLDER/$ARCHIVENAME;
prework_backup;
cd /;
sudo -H -u $BORGUSER bash -c '
TARGETFOLDER='$TARGETFOLDER';
REPOLOCATION='$REPOLOCATION';
ARCHIVENAME='$ARCHIVENAME';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg create -C lzma $REPOLOCATION::$ARCHIVENAME $TARGETFOLDER/$ARCHIVENAME';
rm -rf $TARGETFOLDER/$ARCHIVENAME;
}
cloud_restore () {
ARCHIVENAME=$1;
ARCHIVEIDS=$(get_archives | cut -f 1 -d ' ');
if [[ "${ARCHIVENAME,,}" == "latest" ]];
then
ARCHIVENAME=$(echo "$ARCHIVEIDS" | head -n 1);
else
if [[ "$ARCHIVEIDS" != *$ARCHIVENAME* ]];
then
echo "ERROR! Provided archivename '$ARCHIVENAME' is not part of the available archives! Aborting.";
exit 1;
fi
fi
mkdir -p $TARGETFOLDER/$ARCHIVENAME;
chown -R $BORGUSER: $TARGETFOLDER
cd /;
sudo -H -u $BORGUSER bash -c '
REPOLOCATION='$REPOLOCATION';
ARCHIVENAME='$ARCHIVENAME';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg extract $REPOLOCATION::$ARCHIVENAME --list';
postwork_restore;
rm -rf $TARGETFOLDER/$ARCHIVENAME;
}
cloud_backup_delete () {
ARCHIVENAME=$1;
ARCHIVEIDS=$(get_archives | cut -f 1 -d ' ');
if [[ "$ARCHIVEIDS" != *$ARCHIVENAME* ]];
then
echo "ERROR! Provided archivename '$ARCHIVENAME' is not part of the available archives! Aborting.";
exit 1;
fi
sudo -H -u $BORGUSER bash -c '
REPOLOCATION='$REPOLOCATION';
ARCHIVENAME='$ARCHIVENAME';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg delete $REPOLOCATION::$ARCHIVENAME';
}
has_argument () {
if [[ -z "$1" ]];
then
echo "Missing required paramter!";
exit 1;
fi
}
(
flock -n 9 || {
echo "BACKUP ALREADY RUNNING! ABORTING.";
exit 1;
}
if [ $# = 0 ]; then
help;
exit 0;
fi
action=$1
case $action in
"list")
get_archives;
;;
"backup")
cloud_backup;
;;
"restore")
has_argument $2;
cloud_restore $2;
;;
"delete")
has_argument $2;
cloud_backup_delete $2;
;;
*)
help;
exit 0;
;;
esac
echo ""
) 9>/var/run/lock/cloud-backup.lock;

View File

@ -1,26 +0,0 @@
#!/bin/bash
BORGUSER="{{ backup_owner }}";
RUNFOLDER="{{ backup_run_folder }}";
TARGETFOLDER="{{ backup_storage }}";
REPOLOCATION="{{ backup_location }}";
ARCHIVENAME="mailcow-$(date '+%s')";
cd $RUNFOLDER;
MAILCOW_BACKUP_LOCATION="$TARGETFOLDER/" ./helper-scripts/backup_and_restore.sh backup all;
LATESTBACKUP="$(ls -t $TARGETFOLDER | head -n 1)";
if [[ "$LATESTBACKUP" != *"mailcow"* ]];
then
echo "NOT MAILCOW! ABORT!";
exit 1;
fi
chown -R $BORGUSER: $TARGETFOLDER/$LATESTBACKUP;
sudo -H -u $BORGUSER bash -c '
TARGETFOLDER='$TARGETFOLDER';
REPOLOCATION='$REPOLOCATION';
ARCHIVENAME='$ARCHIVENAME';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg create -C lzma $REPOLOCATION::$ARCHIVENAME $TARGETFOLDER/$LATESTBACKUP';
rm -rf $TARGETFOLDER/$LATESTBACKUP;

View File

@ -1 +0,0 @@
#!/bin/bash

View File

@ -1,20 +0,0 @@
#!/bin/bash
BORGUSER="{{ backup_owner }}";
RUNFOLDER="{{ backup_run_folder }}";
TARGETFOLDER="{{ backup_storage }}";
REPOLOCATION="{{ backup_location }}";
ARCHIVENAME="vault-$(date '+%s')";
cd $RUNFOLDER;
cp -r $RUNFOLDER/home $TARGETFOLDER/$ARCHIVENAME;
chown -R $BORGUSER: $TARGETFOLDER/$ARCHIVENAME;
sudo -H -u $BORGUSER bash -c '
TARGETFOLDER='$TARGETFOLDER';
REPOLOCATION='$REPOLOCATION';
ARCHIVENAME='$ARCHIVENAME';
export BORG_PASSPHRASE=$(cat {{ backup_home }}/.borg.key);
borg create -C lzma $REPOLOCATION::$ARCHIVENAME $TARGETFOLDER/$ARCHIVENAME';
rm -rf $TARGETFOLDER/$ARCHIVENAME;