fix: Directly copy SQLite DB, WAL, and SHM files from the host volume when sqlite3 is unavailable in the container, and update cleanup to include these related files.

This commit is contained in:
2026-01-04 14:21:29 +01:00
parent 1151b16e52
commit ba553036fc

View File

@@ -36,35 +36,36 @@ if [ ! -d "$BACKUP_DEST_DIR" ]; then
fi
# 2. Backup Execution
# We assume the container has 'sqlite3' installed. If not, this will fail safely.
# Format: db_backup_YYYYMMDD_HHMMSS.sqlite3
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
FILENAME="db_backup_${TIMESTAMP}.sqlite3"
log_info "Creating backup: $FILENAME"
# We execute sqlite3 inside the container to safely backup the DB to the /data volume (mapped to host)
if ! docker exec "$CONTAINER_NAME" command -v sqlite3 &>/dev/null; then
log_error "ERROR: 'sqlite3' command not found inside '$CONTAINER_NAME'. Please install it or check image."
exit 1
fi
# FALLBACK METHOD: Since 'sqlite3' is missing in the container, we simply copy the DB file.
# Note: This is *technically* not transaction-safe if writes happen exactly during copy,
# but for a personal vault, it's usually acceptable (WAL mode helps).
# Ideally, install sqlite3 in container or use vaultwarden/server:alpine which might have it.
docker exec "$CONTAINER_NAME" sqlite3 /data/db.sqlite3 ".backup '/data/$FILENAME'"
# 3. Verify and Move
HOST_FILE_PATH="$BACKUP_SOURCE_DIR/$FILENAME"
if [ -f "$HOST_FILE_PATH" ]; then
mv "$HOST_FILE_PATH" "$BACKUP_DEST_DIR/"
log_info "SUCCESS: Backup moved to $BACKUP_DEST_DIR/$FILENAME"
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3" ]; then
cp "$BACKUP_SOURCE_DIR/db.sqlite3" "$BACKUP_DEST_DIR/$FILENAME"
# Also copy the WAL and SHM files if they exist (crucial for WAL mode integrity)
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3-wal" ]; then
cp "$BACKUP_SOURCE_DIR/db.sqlite3-wal" "$BACKUP_DEST_DIR/db_backup_${TIMESTAMP}.sqlite3-wal"
fi
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3-shm" ]; then
cp "$BACKUP_SOURCE_DIR/db.sqlite3-shm" "$BACKUP_DEST_DIR/db_backup_${TIMESTAMP}.sqlite3-shm"
fi
log_info "SUCCESS: Backup created at $BACKUP_DEST_DIR/$FILENAME"
else
log_error "ERROR: Backup file '$HOST_FILE_PATH' not found on host."
log_error "ERROR: Source DB file '$BACKUP_SOURCE_DIR/db.sqlite3' not found."
exit 1
fi
# 4. Cleanup / Rotation
# 3. Cleanup / Rotation
log_info "Cleaning up backups older than $RETENTION_DAYS days..."
find "$BACKUP_DEST_DIR" -name "db_backup_*.sqlite3" -type f -mtime +$RETENTION_DAYS -delete
find "$BACKUP_DEST_DIR" -name "db_backup_*.sqlite3*" -type f -mtime +$RETENTION_DAYS -delete
log_info "Backup process completed."
exit 0