Backup einrichten
Mit folgendem Backup-Script (bash) können wir die Datenbank sichern. Diese können wir außerdem mit systemd
per Service und Timer automatisieren. Alternativ kann das Bash-Script auch als Cronjob eingebunden werden.
vim /opt/fabinfra/bffh-scripts/bffh-backup.service
[Unit]
Description=BFFH Backup Service
[Service]
Type=oneshot
ExecStart=/opt/fabinfra/bffh-scripts/bffh-backup.sh
vim /opt/fabinfra/bffh-scripts/bffh-backup.sh
#!/bin/bash
# Database dump command
DB_DUMP_CMD="/home/fabaccess/fabaccess-repos/bffh/target/release/bffhd -c /home/fabaccess/fabaccess-repos/bffh/examples/bffh.dhall --dump-users"
# Backup directory
BACKUP_DIR="/home/fabaccess/backups"
# Number of backups to keep
NUM_BACKUPS_TO_KEEP=5
# Dry run flag
DRY_RUN=false
# Parse command-line options
while getopts ":n:r" opt; do
case $opt in
n)
NUM_BACKUPS_TO_KEEP="$OPTARG"
;;
r)
DRY_RUN=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Current date and time
CURRENT_DATE=$(date +"%Y%m%d%H%M%S")
# Create a backup file name
BACKUP_FILE="$BACKUP_DIR/db_backup_$CURRENT_DATE.toml"
# Execute the database dump command
if [ "$DRY_RUN" = true ]; then
echo "Dry run mode: Database backup command will not be executed."
else
$DB_DUMP_CMD $BACKUP_FILE
fi
# Check if the database dump was successful
if [ $? -eq 0 ]; then
echo "Database backup completed successfully."
# Sort backup files by modification time in ascending order
sorted_backup_files=($(ls -t "$BACKUP_DIR"))
# Determine number of backups to delete
num_backups_to_delete=$((${#sorted_backup_files[@]} - NUM_BACKUPS_TO_KEEP))
# Delete oldest backups if necessary
if [ $num_backups_to_delete -gt 0 ]; then
for ((i = 0; i < $num_backups_to_delete; i++)); do
if [ "$DRY_RUN" = true ]; then
echo "Dry run mode: Would remove old backup: ${sorted_backup_files[$i]}"
else
rm "${sorted_backup_files[$i]}"
echo "Removed old backup: ${sorted_backup_files[$i]}"
fi
done
fi
else
echo "Error: Database backup failed."
fi
Außerdem als Timer. Dieser muss den gleichen Name haben wie der Service (siehe https://wiki.ubuntuusers.de/systemd/Timer_Units)
/opt/fabinfra/bffh-scripts/bffh-backup.timer
[Unit]
Description=BFFH Backup Timer
[Timer]
# Run every day at midnight
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target