149 lines
3.9 KiB
Django/Jinja
149 lines
3.9 KiB
Django/Jinja
#!/bin/bash
|
|
|
|
BORGUSER="{{ backup_owner }}";
|
|
RUNFOLDER="{{ backup_storage }}";
|
|
APPLIST="{{ backup_clients | json_query('*.app') | join(' ') }}"
|
|
|
|
declare -A BORG_CLIENT_MAP
|
|
{% for client in backup_clients %}
|
|
BORG_CLIENT_MAP[{{ backup_clients[client].app }}]="{{ client }}"
|
|
{% endfor %}
|
|
|
|
declare -A BORG_USER_MAP
|
|
{% for client in backup_clients %}
|
|
BORG_USER_MAP[{{ backup_clients[client].app }}]="{{ backup_clients[client].owner }}"
|
|
{% endfor %}
|
|
|
|
|
|
help (){
|
|
echo "cloud-server-backup - backup and restore script on cloud backup with borg target v1.0 by L.Hahn.
|
|
|
|
Usage: $0 COMMAND [APPS]
|
|
|
|
COMMAND:
|
|
- list-apps List available apps in remote borg repositories.
|
|
- list-remote [APPS] List available archives for apps in remote borg repositories.
|
|
- list List available archives of remote repositories on local repository.
|
|
- prune [APPS] Prune app (or all) borg repositories.
|
|
- backup Perform backup of remote borg repositories and create a new archive in local borg repository.
|
|
- restore ARCHIVENAME [APPS] Upload backup to borg repository to restore older file states.
|
|
May turn off your application if still running.
|
|
If latest is provided as ARCHIVENAME, the latest one based on timestamp is taken.
|
|
IF ARCHIVENAME is provided, will try to download it; throws error if not found.
|
|
If APP is provided, only the mentioned app repositories are restored.
|
|
|
|
|
|
APPS: A single app or (sub-)set of below listed apps to be considered.
|
|
{% for client in backup_clients %}
|
|
- {{ backup_clients[client].app }}
|
|
{% endfor %}
|
|
";
|
|
}
|
|
|
|
|
|
get_app () {
|
|
for APP in $APPLIST;
|
|
do
|
|
echo $APP;
|
|
done
|
|
}
|
|
|
|
get_remote_archives () {
|
|
CHECKLIST=$1;
|
|
for APP in $CHECKLIST;
|
|
do
|
|
REPOSITORYCLIENT=${BORG_CLIENT_MAP[$APP]}
|
|
BORG_REPO_USER=${BORG_USER_MAP[$APP]}
|
|
REPOLOCATION="ssh://$BORG_REPO_USER@{{ backup_host }}:{{ backup_ssh_port }}/./$APP"
|
|
BORG_PASSPHRASE=$(cat {{ backup_key_folder }}/$APP)
|
|
KEYFILE="{{ backup_home }}/.ssh/$REPOSITORYCLIENT"
|
|
sudo -H -u $BORGUSER bash -c '
|
|
export BORG_PASSPHRASE='$BORG_PASSPHRASE';
|
|
KEYFILE='$KEYFILE';
|
|
REPOLOCATION='$REPOLOCATION';
|
|
borg list $REPOLOCATION --rsh "/usr/bin/ssh -i $KEYFILE"'
|
|
done
|
|
}
|
|
|
|
|
|
has_argument () {
|
|
if [[ -z "$1" ]];
|
|
then
|
|
echo "Missing required paramter!";
|
|
exit 1;
|
|
fi
|
|
}
|
|
|
|
validate_apps_or_default () {
|
|
VALID=1;
|
|
if [[ -z "$1" ]];
|
|
then
|
|
echo $APPLIST;
|
|
else
|
|
for APP in $1;
|
|
do
|
|
if [[ ! ${APPLIST[@]} =~ $APP ]]
|
|
then
|
|
VALID=0;
|
|
echo "$APP is not a valid app.";
|
|
fi
|
|
done
|
|
if [[ ! $VALID == 1 ]]
|
|
then
|
|
echo "Aborting.";
|
|
exit 1;
|
|
else
|
|
echo $1;
|
|
fi
|
|
fi
|
|
}
|
|
|
|
(
|
|
flock -n 9 || {
|
|
echo "BACKUP ALREADY RUNNING! ABORTING.";
|
|
exit 1;
|
|
}
|
|
|
|
if [ $# = 0 ]; then
|
|
help;
|
|
exit 0;
|
|
fi
|
|
|
|
action=$1
|
|
case $action in
|
|
"list-apps")
|
|
get_app;
|
|
;;
|
|
|
|
"list-remote")
|
|
CHECKLIST=$(validate_apps_or_default $2);
|
|
get_remote_archives "$CHECKLIST";
|
|
;;
|
|
|
|
"list")
|
|
#get_archives;
|
|
;;
|
|
|
|
"prune")
|
|
#prune_remote_archives;
|
|
;;
|
|
|
|
"backup")
|
|
#local_backup;
|
|
;;
|
|
|
|
"restore")
|
|
has_argument $2;
|
|
#local_restore $2;
|
|
;;
|
|
"delete")
|
|
has_argument $2;
|
|
CHECKLIST=$(validate_apps_or_default $3);
|
|
#local_delete $2;
|
|
;;
|
|
*)
|
|
help;
|
|
exit 0;
|
|
;;
|
|
esac
|
|
) 9>/var/run/lock/cloud-server-backup.lock; |