backup-helper/backup.sh

52 lines
1.3 KiB
Bash
Raw Normal View History

2025-01-27 21:34:13 +00:00
#!/usr/bin/env bash
# If neither of BACKUP_HELPER_DIRS and BACKUP_HELPER_COMMANDS not set. Exit
if [ -z "$BACKUP_HELPER_DIRS" ] && [ -z "$BACKUP_HELPER_COMMANDS" ]; then
echo "BACKUP_HELPER_DIRS is not set"
exit 1;
fi
exit_code=0;
# Backup directories
if [ -n "$BACKUP_HELPER_DIRS" ]; then
for i in ${BACKUP_HELPER_DIRS//,/ }; do
echo "Starting backup of $i";
if ! output=$(restic backup "$i"); then
exit_code=1;
echo "Backup of $i failed with exit code $?:"
echo "$output"
else
echo "Backup of $i successful:"
echo "$output"
fi
done
fi
# Backup command outputs
if [ -n "$BACKUP_HELPER_COMMANDS" ]; then
IFS=";" read -r -a pairs <<< "$BACKUP_HELPER_COMMANDS"
for pair in "${pairs[@]}"; do
IFS=":" read -r key value <<< "$pair"
2025-01-27 21:55:20 +00:00
# Split value into array of command args
IFS=' ' read -r -a cmd_parts <<< "$value"
if ! output=$(restic backup --stdin-filename "$key" --stdin-from-command -- "${cmd_parts[@]}"); then
2025-01-27 21:34:13 +00:00
exit_code=1;
2025-01-27 21:59:26 +00:00
echo "Backup of $key failed with exit code $?:"
2025-01-27 21:34:13 +00:00
echo "$output"
else
2025-01-27 21:59:26 +00:00
echo "Backup of $key successful:"
2025-01-27 21:34:13 +00:00
echo "$output"
fi
done
fi
if [ "$BACKUP_FORGET" -eq 1 ]; then
echo "Removing old backups"
output=$(restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune)
echo "$output"
fi
exit "$exit_code"