add STDIN support to backup script

This commit is contained in:
frederik
2024-11-19 17:22:38 +01:00
committed by Nachtzuster
parent 59e00e6dd6
commit 1dd6fe7f14
+35 -5
View File
@@ -32,10 +32,11 @@ done
[ -z "$ACTION" ] && usage && exit 1
if [ $ACTION != "size" ]; then
[ -z "$ARCHIVE" ] && usage && exit 1
[ "$ARCHIVE" == '-' ] && QUIET=1
[ "$ARCHIVE" == '-' ] && [ $ACTION == "backup" ] && QUIET=1
fi
MEG=1048576
UNPACK="/home/$BIRDNET_USER/BirdSongs/tmp"
log() {
[ -z "$QUIET" ] && echo "$1"
@@ -92,6 +93,7 @@ estimated_restore_size() {
}
restore_check() {
if [ "$ARCHIVE" != '-' ]; then
[ ! -f "$ARCHIVE" ] && echo "$ARCHIVE" not found && exit 1
available_space_for_restore
estimated_restore_size
@@ -103,18 +105,41 @@ restore_check() {
arch_list=$(tar --list --exclude="*/*" -f "$ARCHIVE" | sed 's/\///')
for obj in "${required[@]}";do
part2=$(basename "$obj")
! (echo $arch_list | grep -F -q "$part2") && echo corrupted backup file && exit 1
! (echo $arch_list | grep -F -q "$part2") && echo Missing \'"$part2"\': corrupted backup file? && exit 1
done
fi
}
late_restore_check() {
if [ "$ARCHIVE" == '-' ]; then
log "Checking backup file"
for obj in "${required[@]}";do
part2=$(basename "$obj")
! [ -e "${UNPACK}/${part2}" ] && echo Missing \'"$part2"\': corrupted backup file? && exit 1
done
fi
}
unpack() {
log "Starting unpacking, this might take a while"
rm -fr ${UNPACK}
mkdir ${UNPACK}
tar --extract -p -f "$ARCHIVE" -C "${UNPACK}"
}
restore() {
log "Starting restore, this might take a while"
log "Starting restore"
for obj in "${required[@]}";do
tar --extract -p -f "$ARCHIVE" -C "$(dirname "$obj")" "$(basename "$obj")"
[ -d "$obj" ] && rm -rf "$obj"
mv "${UNPACK}/$(basename "$obj")" "$(dirname "$obj")/"
done
log "Trying to restore optional files"
for obj in "${optional[@]}";do
tar --extract --ignore-failed-read -p -f "$ARCHIVE" -C "$(dirname "$obj")" "$(basename "$obj")" || log "$(basename "$obj") not found in backup file, moving on"
if [ -f "${UNPACK}/$(basename "$obj")" ] ; then
mv "${UNPACK}/$(basename "$obj")" "$(dirname "$obj")/"
else
echo No $(basename "$obj") found, moving on
fi
done
log "Fixing up configuration file"
CURRENT_BIRDNET_USER="$BIRDNET_USER"
@@ -126,11 +151,13 @@ restore() {
else
/home/$CURRENT_BIRDNET_USER/BirdNET-Pi/scripts/install_language_label.sh -l $DATABASE_LANG
fi
rm -fr ${UNPACK}
log "Restore done"
}
function cleanup()
{
rm -fr ${UNPACK}
"$my_dir/restart_services.sh" &>/dev/null
exit
}
@@ -157,6 +184,9 @@ if [ $ACTION == "size" ]; then
fi
trap cleanup SIGINT SIGTERM SIGABRT
[ $ACTION == "restore" ] && unpack
[ $ACTION == "restore" ] && late_restore_check
log "Stopping services"
"$my_dir/stop_core_services.sh"