From d5dd71831a27ca8a7fca810aabfebaada5bc9a8b Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:54:31 +0100 Subject: [PATCH] Update 01-structure.sh --- .../rootfs/etc/cont-init.d/01-structure.sh | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/birdnet-go/rootfs/etc/cont-init.d/01-structure.sh b/birdnet-go/rootfs/etc/cont-init.d/01-structure.sh index 9f59fbfb5..d55d91fa3 100755 --- a/birdnet-go/rootfs/etc/cont-init.d/01-structure.sh +++ b/birdnet-go/rootfs/etc/cont-init.d/01-structure.sh @@ -64,19 +64,31 @@ if [[ "${CURRENT_BIRDSONGS_FOLDER%/}" != "${$BIRDSONGS_FOLDER%/}" ]]; then fi # Adapt the database if [ -f /config/birdnet.db ]; then - # SQL query to update the paths - bashio::log.warning "Modifying database paths from $CURRENT_BIRDSONGS_FOLDER to $BIRDSONGS_FOLDER. A backup will be created before" - cp /config/birdnet.db /config/birdnet.db_$(date +%Y%m%d_%H%M%S) - SQL_QUERY="UPDATE notes SET clip_name = REPLACE(clip_name, '^${CURRENT_BIRDSONGS_FOLDER}/', '${BIRDSONGS_FOLDER%/}/');" - + # Prepare + backup="$(date +%Y%m%d_%H%M%S)" + bashio::log.warning "Modifying database paths from $CURRENT_BIRDSONGS_FOLDER to $BIRDSONGS_FOLDER. A backup named birdnet.db_$backup will be created before" + + # Create backup + cp /config/birdnet.db "birdnet.db_$backup" + if [ $? -ne 0 ]; then + bashio::log.error "Failed to create a backup of the database. Aborting path modification." + exit 1 + fi + # Execute the query using sqlite3 + SQL_QUERY="UPDATE notes SET clip_name = '${BIRDSONGS_FOLDER%/}/' || substr(clip_name, length('${CURRENT_BIRDSONGS_FOLDER%/}/') + 1) WHERE clip_name LIKE '${CURRENT_BIRDSONGS_FOLDER%/}/%';" sqlite3 /config/birdnet.db "$SQL_QUERY" - - # Check if the operation was successful if [ $? -eq 0 ]; then echo "Paths have been successfully updated." else - bashio::log.warning "An error occurred while updating the paths." + bashio::log.warning "An error occurred while updating the paths. The database backup will be restored." + BACKUP_FILE="/config/birdnet.db_$(date +%Y%m%d_%H%M%S)" # Make sure this matches the earlier backup filename + if [ -f "$BACKUP_FILE" ]; then + mv "$BACKUP_FILE" /config/birdnet.db + bashio::log.info "The database backup has been restored." + else + bashio::log.error "Backup file not found! Manual intervention required." + fi fi fi fi