Skip to main content

Maintenance

Regular Maintenance Tasks

Ubuntu Maintenance Scripts:

bash



# Create maintenance script
cat > ~/flowise-maintenance.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/backup/flowise"
FLOWISE_DIR="/var/lib/flowise"
LOG_FILE="/var/log/flowise/maintenance.log"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# Database backup
backup_database() {
log_message "Starting database backup..."
if [ -f "$FLOWISE_DIR/database.sqlite" ]; then
mkdir -p "$BACKUP_DIR/$(date +%Y%m%d)"
cp "$FLOWISE_DIR/database.sqlite" "$BACKUP_DIR/$(date +%Y%m%d)/database-$(date +%H%M).sqlite"
log_message "Database backup completed"
else
log_message "Database file not found"
fi
}
# Clean old logs
cleanup_logs() {
log_message "Starting log cleanup..."
find /var/log/flowise -name "*.log" -mtime +30 -delete
find "$BACKUP_DIR" -type d -mtime +90 -exec rm -rf {} +
log_message "Log cleanup completed"
}
# Update Node.js packages
update_packages() {
log_message "Checking for package updates..."
cd /home/flowise
npm outdated --global
# Update if needed (uncomment for auto-update)
# npm update -g flowise
log_message "Package check completed"
}
# System cleanup
cleanup_system() {
log_message "Starting system cleanup..."
# Clear temporary files
sudo rm -rf /tmp/flowise-*
# Clear npm cache
npm cache clean --force
log_message "System cleanup completed"
}
# Main maintenance routine
main() {
log_message "=== Starting maintenance routine ==="
backup_database
cleanup_logs
update_packages
cleanup_system
log_message "=== Maintenance routine completed ==="
}
main
EOF
chmod +x ~/flowise-maintenance.sh
# Schedule maintenance
crontab -l > /tmp/crontab.bak
echo "0 2 * * 0 /home/$(whoami)/flowise-maintenance.sh" >> /tmp/crontab.bak
crontab /tmp/crontab.bak

RHEL8 Maintenance Scripts:

bash

# Create maintenance script (similar to Ubuntu but with SELinux considerations)
cat > ~/flowise-maintenance.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/backup/flowise"
FLOWISE_DIR="/var/lib/flowise"
LOG_FILE="/var/log/flowise/maintenance.log"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# SELinux context restoration
restore_selinux_context() {
log_message "Restoring SELinux contexts..."
sudo restorecon -R /var/lib/flowise
sudo restorecon -R /var/log/flowise
log_message "SELinux context

Database Maintenance

Database Optimization (Ubuntu & RHEL8):

bash



# Create database optimization script
cat > ~/optimize-database.sh << 'EOF'
#!/bin/bash
DB_PATH="/var/lib/flowise/database.sqlite"
LOG_FILE="/var/log/flowise/db-maintenance.log"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# SQLite optimization
optimize_sqlite() {
if [ -f "$DB_PATH" ]; then
log_message "Starting SQLite optimization..."
# Create backup before optimization
cp "$DB_PATH" "${DB_PATH}.backup-$(date +%Y%m%d%H%M)"
# Run VACUUM to optimize database
sqlite3 "$DB_PATH" "VACUUM;"
# Update statistics
sqlite3 "$DB_PATH" "ANALYZE;"
# Check integrity
local integrity=$(sqlite3 "$DB_PATH" "PRAGMA integrity_check;")
if [ "$integrity" = "ok" ]; then
log_message "Database optimization completed successfully"
else
log_message "Database integrity check failed: $integrity"
fi
else
log_message "Database file not found at $DB_PATH"
fi
}
optimize_sqlite
EOF
chmod +x ~/optimize-database.sh
# Add to weekly cron
crontab -l > /tmp/crontab.bak
echo "0 3 * * 1 /home/$(whoami)/optimize-database.sh" >> /tmp/crontab.bak
crontab /tmp/crontab.bak

Backup and Recovery

Automated Backup System:

bash


# Create comprehensive backup script
cat > ~/flowise-backup.sh << 'EOF'
#!/bin/bash
# Configuration
BACKUP_ROOT="/backup/flowise"
FLOWISE_HOME="/var/lib/flowise"
CONFIG_DIR="/etc/flowise"
RETENTION_DAYS=30
LOG_FILE="/var/log/flowise/backup.log"
# Remote backup configuration (optional)
REMOTE_BACKUP_ENABLED=false
REMOTE_HOST="backup-server.example.com"
REMOTE_USER="backup"
REMOTE_PATH="/backups/flowise"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Create backup directory structure
setup_backup_dirs() {
local backup_date=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="$BACKUP_ROOT/$backup_date"
mkdir -p "$BACKUP_DIR"/{database,files,config,logs}
log_message "Created backup directory: $BACKUP_DIR"
}
# Backup database
backup_database() {
log_message "Backing up database..."
if [ -f "$FLOWISE_HOME/database.sqlite" ]; then
cp "$FLOWISE_HOME/database.sqlite" "$BACKUP_DIR/database/"
log_message "Database backup completed"
else
log_message "Warning: Database file not found"
fi
}
# Backup configuration files
backup_config() {
log_message "Backing up configuration..."
if [ -d "$CONFIG_DIR" ]; then
cp -r "$CONFIG_DIR"/* "$BACKUP_DIR/config/"
log_message "Configuration backup completed"
else
log_message "Warning: Configuration directory not found"
fi
}
# Backup uploaded files and storage
backup_files() {
log_message "Backing up files and storage..."
if [ -d "$FLOWISE_HOME/storage" ]; then
cp -r "$FLOWISE_HOME/storage" "$BACKUP_DIR/files/"
log_message "Files backup completed"
fi
if [ -d "$FLOWISE_HOME/api" ]; then
cp -r "$FLOWISE_HOME/api" "$BACKUP_DIR/files/"
log_message "API keys backup completed"
fi
}
# Backup logs
backup_logs() {
log_message "Backing up recent logs..."
find /var/log/flowise -name "*.log" -mtime -7 -exec cp {} "$BACKUP_DIR/logs/" \;
log_message "Logs backup completed"
}
# Create backup metadata
create_metadata() {
cat > "$BACKUP_DIR/backup_info.txt" << EOF
Backup Information
==================
Backup Date: $(date)
Backup Type: Full
Flowise Version: $(npm list -g flowise --depth=0 2>/dev/null | grep flowise || echo "Unknown")
System: $(uname -a)
Database Size: $(du -h "$FLOWISE_HOME/database.sqlite" 2>/dev/null | cut -f1 || echo "Unknown")
Total Backup Size: $(du -sh "$BACKUP_DIR" | cut -f1)
EOF
log_message "Backup metadata created"
}
# Compress backup
compress_backup() {
log_message "Compressing backup..."
cd "$BACKUP_ROOT"
tar -czf "${BACKUP_DIR}.tar.gz" "$(basename "$BACKUP_DIR")"
if [ $? -eq 0 ]; then
rm -rf "$BACKUP_DIR"
log_message "Backup compressed successfully: ${BACKUP_DIR}.tar.gz"
else
log_message "Error: Backup compression failed"
return 1
fi
}
# Remote backup sync
sync_remote_backup() {
if [ "$REMOTE_BACKUP_ENABLED" = true ]; then
log_message "Syncing to remote backup location..."
rsync -avz --delete \
"$BACKUP_ROOT/" \
"$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/"
if [ $? -eq 0 ]; then
log_message "Remote backup sync completed"
else
log_message "Error: Remote backup sync failed"
fi
fi
}
# Cleanup old backups
cleanup_old_backups() {
log_message "Cleaning up old backups..."
find "$BACKUP_ROOT" -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
log_message "Old backup cleanup completed"
}
# Main backup process
main() {
log_message "=== Starting backup process ==="
setup_backup_dirs
backup_database
backup_config
backup_files
backup_logs
create_metadata
compress_backup
sync_remote_backup
cleanup_old_backups
log_message "=== Backup process completed ==="
}
main
EOF
chmod +x ~/flowise-backup.sh
# Schedule daily backups
crontab -l > /tmp/crontab.bak
echo "0 1 * * * /home/$(whoami)/flowise-backup.sh" >> /tmp/crontab.bak
crontab /tmp/crontab.bak

Recovery Procedures:

bash



# Create recovery script
cat > ~/flowise-restore.sh << 'EOF'
#!/bin/bash
# Configuration
BACKUP_ROOT="/backup/flowise"
FLOWISE_HOME="/var/lib/flowise"
CONFIG_DIR="/etc/flowise"
LOG_FILE="/var/log/flowise/restore.log"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# List available backups
list_backups() {
echo "Available backups:"
ls -la "$BACKUP_ROOT"/*.tar.gz 2>/dev/null | awk '{print $9, $5, $6, $7, $8}' | sort -k1
}
# Restore from backup
restore_backup() {
local backup_file="$1"
if [ ! -f "$backup_file" ]; then
log_message "Error: Backup file not found: $backup_file"
return 1
fi
log_message "Starting restore from: $backup_file"
# Stop Flowise service
sudo systemctl stop flowise
# Create restore point
local restore_point="/tmp/flowise-restore-point-$(date +%Y%m%d%H%M%S)"
mkdir -p "$restore_point"
# Backup current state
cp -r "$FLOWISE_HOME" "$restore_point/" 2>/dev/null
cp -r "$CONFIG_DIR" "$restore_point/" 2>/dev/null
# Extract backup
local temp_dir="/tmp/flowise-restore-temp"
mkdir -p "$temp_dir"
cd "$temp_dir"
tar -xzf "$backup_file"
# Restore files
local backup_dir=$(ls -1 | head -n1)
if [ -d "$backup_dir/database" ]; then
cp "$backup_dir/database"/* "$FLOWISE_HOME/" 2>/dev/null
log_message "Database restored"
fi
if [ -d "$backup_dir/config" ]; then
cp -r "$backup_dir/config"/* "$CONFIG_DIR/" 2>/dev/null
log_message "Configuration restored"
fi
if [ -d "$backup_dir/files/storage" ]; then
cp -r "$backup_dir/files/storage" "$FLOWISE_HOME/" 2>/dev/null
log_message "Storage files restored"
fi
if [ -d "$backup_dir/files/api" ]; then
cp -r "$backup_dir/files/api" "$FLOWISE_HOME/" 2>/dev/null
log_message "API keys restored"
fi
# Fix permissions
sudo chown -R flowise:flowise "$FLOWISE_HOME"
sudo chown -R root:root "$CONFIG_DIR"
# Cleanup temp files
rm -rf "$temp_dir"
# Start Flowise service
sudo systemctl start flowise
log_message "Restore completed. Previous state backed up to: $restore_point"
}
# Usage information
usage() {
echo "Usage: $0 [list|restore <backup_file>]"
echo ""
echo "Commands:"
echo " list - List available backups"
echo " restore <backup_file> - Restore from specified backup"
echo ""
echo "Examples:"
echo " $0 list"
echo " $0 restore /backup/flowise/20240101_120000.tar.gz"
}
# Main function
case "$1" in
list)
list_backups
;;
restore)
if [ -z "$2" ]; then
echo "Error: Please specify backup file"
usage
exit 1
fi
restore_backup "$2"
;;
*)
usage
exit 1
;;
esac
EOF
chmod +x ~/flowise-restore.sh