#!/bin/bash # ChefBible Database Manager # Easy backup and restore for your recipes BACKUP_DIR="/home/chefbible/backups" DB_PATH="/home/chefbible/data/database.db" CONTAINER_NAME="chefbible" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color show_help() { echo -e "${BLUE}ChefBible Database Manager${NC}" echo "" echo "Usage: $0 [command]" echo "" echo "Commands:" echo " backup - Create a backup of the database" echo " restore - Restore from the most recent backup" echo " list - List all available backups" echo " status - Show current database status" echo " help - Show this help message" echo "" echo "Examples:" echo " $0 backup # Create backup" echo " $0 restore # Restore from latest backup" echo " $0 restore backup_20240101 # Restore from specific backup" } backup_database() { echo -e "${BLUE}๐Ÿ”„ Creating backup of ChefBible database...${NC}" # Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # Check if database exists if [ -f "$DB_PATH" ]; then TIMESTAMP=$(date +"%Y%m%d_%H%M%S") BACKUP_FILE="$BACKUP_DIR/chefbible_backup_$TIMESTAMP.db" # Create backup cp "$DB_PATH" "$BACKUP_FILE" echo -e "${GREEN}โœ… Backup created: $(basename "$BACKUP_FILE")${NC}" # Show backup size BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1) echo -e "${GREEN}๐Ÿ“Š Backup size: $BACKUP_SIZE${NC}" # Keep only last 5 backups ls -t "$BACKUP_DIR"/chefbible_backup_*.db 2>/dev/null | tail -n +6 | xargs -r rm echo -e "${GREEN}๐Ÿงน Cleaned up old backups (keeping last 5)${NC}" else echo -e "${YELLOW}โš ๏ธ No database found at $DB_PATH${NC}" echo -e "${YELLOW} This might be a fresh installation${NC}" fi echo -e "${GREEN}๐ŸŽ‰ Backup process complete!${NC}" } restore_database() { echo -e "${BLUE}๐Ÿ”„ Restoring ChefBible database...${NC}" # Create data directory if it doesn't exist mkdir -p "$(dirname "$DB_PATH")" # If no backup file specified, use the most recent one if [ -z "$1" ]; then BACKUP_FILE=$(ls -t "$BACKUP_DIR"/chefbible_backup_*.db 2>/dev/null | head -n1) if [ -z "$BACKUP_FILE" ]; then echo -e "${RED}โŒ No backup files found in $BACKUP_DIR${NC}" echo -e "${YELLOW} Run '$0 backup' first to create a backup${NC}" exit 1 fi echo -e "${BLUE}๐Ÿ“ Using most recent backup: $(basename "$BACKUP_FILE")${NC}" else BACKUP_FILE="$BACKUP_DIR/chefbible_backup_$1.db" if [ ! -f "$BACKUP_FILE" ]; then echo -e "${RED}โŒ Backup file not found: $BACKUP_FILE${NC}" echo -e "${YELLOW} Available backups:${NC}" list_backups exit 1 fi fi # Stop the container if it's running echo -e "${YELLOW}โน๏ธ Stopping ChefBible container...${NC}" docker stop "$CONTAINER_NAME" 2>/dev/null || echo -e "${YELLOW} Container not running${NC}" # Restore the database echo -e "${BLUE}๐Ÿ“ฅ Restoring database from: $(basename "$BACKUP_FILE")${NC}" cp "$BACKUP_FILE" "$DB_PATH" # Set proper permissions chown 1001:1001 "$DB_PATH" 2>/dev/null || echo -e "${YELLOW} Note: Run with sudo to set proper permissions${NC}" echo -e "${GREEN}โœ… Database restored successfully!${NC}" echo -e "${GREEN}๐Ÿš€ You can now start the ChefBible container${NC}" # Show restored database size if [ -f "$DB_PATH" ]; then DB_SIZE=$(du -h "$DB_PATH" | cut -f1) echo -e "${GREEN}๐Ÿ“Š Restored database size: $DB_SIZE${NC}" fi } list_backups() { echo -e "${BLUE}๐Ÿ“‹ Available backups:${NC}" if [ -d "$BACKUP_DIR" ] && [ "$(ls -A "$BACKUP_DIR"/chefbible_backup_*.db 2>/dev/null)" ]; then ls -lh "$BACKUP_DIR"/chefbible_backup_*.db | awk '{print " " $9 " (" $5 ", " $6 " " $7 " " $8 ")"}' else echo -e "${YELLOW} No backups found${NC}" echo -e "${YELLOW} Run '$0 backup' to create your first backup${NC}" fi } show_status() { echo -e "${BLUE}๐Ÿ“Š ChefBible Database Status${NC}" echo "" # Check if database exists if [ -f "$DB_PATH" ]; then DB_SIZE=$(du -h "$DB_PATH" | cut -f1) echo -e "${GREEN}โœ… Database exists: $DB_PATH${NC}" echo -e "${GREEN}๐Ÿ“Š Size: $DB_SIZE${NC}" else echo -e "${RED}โŒ No database found at $DB_PATH${NC}" fi # Check container status if docker ps | grep -q "$CONTAINER_NAME"; then echo -e "${GREEN}โœ… Container is running${NC}" else echo -e "${YELLOW}โš ๏ธ Container is not running${NC}" fi # Show backup count BACKUP_COUNT=$(ls "$BACKUP_DIR"/chefbible_backup_*.db 2>/dev/null | wc -l) echo -e "${BLUE}๐Ÿ“‹ Backups available: $BACKUP_COUNT${NC}" } # Main script logic case "$1" in "backup") backup_database ;; "restore") restore_database "$2" ;; "list") list_backups ;; "status") show_status ;; "help"|"--help"|"-h"|"") show_help ;; *) echo -e "${RED}โŒ Unknown command: $1${NC}" echo "" show_help exit 1 ;; esac