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
+47 -17
View File
@@ -32,10 +32,11 @@ done
[ -z "$ACTION" ] && usage && exit 1 [ -z "$ACTION" ] && usage && exit 1
if [ $ACTION != "size" ]; then if [ $ACTION != "size" ]; then
[ -z "$ARCHIVE" ] && usage && exit 1 [ -z "$ARCHIVE" ] && usage && exit 1
[ "$ARCHIVE" == '-' ] && QUIET=1 [ "$ARCHIVE" == '-' ] && [ $ACTION == "backup" ] && QUIET=1
fi fi
MEG=1048576 MEG=1048576
UNPACK="/home/$BIRDNET_USER/BirdSongs/tmp"
log() { log() {
[ -z "$QUIET" ] && echo "$1" [ -z "$QUIET" ] && echo "$1"
@@ -92,29 +93,53 @@ estimated_restore_size() {
} }
restore_check() { restore_check() {
[ ! -f "$ARCHIVE" ] && echo "$ARCHIVE" not found && exit 1 if [ "$ARCHIVE" != '-' ]; then
available_space_for_restore [ ! -f "$ARCHIVE" ] && echo "$ARCHIVE" not found && exit 1
estimated_restore_size available_space_for_restore
AVL_MB=$(printf "%1.f" $(bc <<< "$AVAILABLE / $MEG")) estimated_restore_size
EST_MB=$(printf "%1.f" $(bc <<< "$ESTIMATED / $MEG")) AVL_MB=$(printf "%1.f" $(bc <<< "$AVAILABLE / $MEG"))
log "Estimated space needed: ${EST_MB}M ($ESTIMATED), space available: ${AVL_MB}M ($AVAILABLE)" EST_MB=$(printf "%1.f" $(bc <<< "$ESTIMATED / $MEG"))
[ $ESTIMATED -gt $AVAILABLE ] && echo "Not enough space available on /home/$BIRDNET_USER/" && exit 1 log "Estimated space needed: ${EST_MB}M ($ESTIMATED), space available: ${AVL_MB}M ($AVAILABLE)"
log "Checking backup file" [ $ESTIMATED -gt $AVAILABLE ] && echo "Not enough space available on /home/$BIRDNET_USER/" && exit 1
arch_list=$(tar --list --exclude="*/*" -f "$ARCHIVE" | sed 's/\///') log "Checking backup file"
for obj in "${required[@]}";do arch_list=$(tar --list --exclude="*/*" -f "$ARCHIVE" | sed 's/\///')
part2=$(basename "$obj") for obj in "${required[@]}";do
! (echo $arch_list | grep -F -q "$part2") && echo corrupted backup file && exit 1 part2=$(basename "$obj")
done ! (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() { restore() {
log "Starting restore, this might take a while" log "Starting restore"
for obj in "${required[@]}";do 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 done
log "Trying to restore optional files" log "Trying to restore optional files"
for obj in "${optional[@]}";do 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 done
log "Fixing up configuration file" log "Fixing up configuration file"
CURRENT_BIRDNET_USER="$BIRDNET_USER" CURRENT_BIRDNET_USER="$BIRDNET_USER"
@@ -126,11 +151,13 @@ restore() {
else else
/home/$CURRENT_BIRDNET_USER/BirdNET-Pi/scripts/install_language_label.sh -l $DATABASE_LANG /home/$CURRENT_BIRDNET_USER/BirdNET-Pi/scripts/install_language_label.sh -l $DATABASE_LANG
fi fi
rm -fr ${UNPACK}
log "Restore done" log "Restore done"
} }
function cleanup() function cleanup()
{ {
rm -fr ${UNPACK}
"$my_dir/restart_services.sh" &>/dev/null "$my_dir/restart_services.sh" &>/dev/null
exit exit
} }
@@ -157,6 +184,9 @@ if [ $ACTION == "size" ]; then
fi fi
trap cleanup SIGINT SIGTERM SIGABRT trap cleanup SIGINT SIGTERM SIGABRT
[ $ACTION == "restore" ] && unpack
[ $ACTION == "restore" ] && late_restore_check
log "Stopping services" log "Stopping services"
"$my_dir/stop_core_services.sh" "$my_dir/stop_core_services.sh"