在 config/app.php 的 providers 数组中添加:
'providers' => [
// ... 其他服务提供者
App\Module\Cleanup\CleanupServiceProvider::class,
],
执行以下SQL文件创建必要的数据库表:
# 执行数据库表创建脚本
mysql -u your_username -p your_database < app/Module/Cleanup/Databases/GenerateSql/cleanup_tables.sql
或者手动执行SQL文件中的语句。
# 发布配置文件
php artisan vendor:publish --tag=cleanup-config
# 发布数据库迁移文件
php artisan vendor:publish --tag=cleanup-migrations
在 .env 文件中添加以下配置:
# Cleanup 模块配置
CLEANUP_ENABLED=true
CLEANUP_DEBUG=false
CLEANUP_TIMEZONE=Asia/Shanghai
# 数据库配置
CLEANUP_TABLE_PREFIX=kku_
CLEANUP_DB_CONNECTION=mysql
CLEANUP_BATCH_SIZE=1000
CLEANUP_QUERY_TIMEOUT=300
# 备份配置
CLEANUP_BACKUP_TYPE=1
CLEANUP_BACKUP_COMPRESSION=2
CLEANUP_BACKUP_PATH=/path/to/backup/storage
CLEANUP_BACKUP_RETENTION=30
CLEANUP_BACKUP_STRUCTURE=true
# 执行配置
CLEANUP_MAX_CONCURRENT=3
CLEANUP_TASK_TIMEOUT=3600
CLEANUP_ENABLE_PREVIEW=true
CLEANUP_REQUIRE_CONFIRMATION=true
CLEANUP_AUTO_BACKUP=true
# 安全配置
CLEANUP_IP_WHITELIST=false
CLEANUP_ALLOWED_IPS=127.0.0.1
CLEANUP_REQUIRE_ADMIN=true
# 通知配置
CLEANUP_NOTIFICATIONS=true
CLEANUP_NOTIFY_MAIL=true
CLEANUP_NOTIFY_DB=true
CLEANUP_MAIL_TO=admin@example.com
php artisan config:clear
php artisan cache:clear
首次安装后,需要扫描系统中的数据表:
# 扫描所有数据表并生成默认配置
php artisan cleanup:scan-tables
# 查看扫描结果详情
php artisan cleanup:scan-tables --show-details
# 强制重新扫描(会覆盖现有配置)
php artisan cleanup:scan-tables --force
扫描完成后,可以通过以下方式查看结果:
# 查看系统状态
php artisan cleanup:data status
# 查看推荐的清理计划
php artisan cleanup:data status --recommendations
# 创建一个简单的日志清理计划
php artisan cleanup:data create-plan \
--name="日志数据清理" \
--type=3 \
--categories=2
# 创建农场模块清理计划
php artisan cleanup:data create-plan \
--name="农场模块清理" \
--type=2 \
--modules=Farm \
--exclude-tables=kku_farm_configs
在实际执行清理之前,建议先预览:
# 预览计划清理结果
php artisan cleanup:data preview 1
# 创建任务并预览
php artisan cleanup:data create-task 1
php artisan cleanup:data preview 2
'security' => [
// 受保护的表(永远不会被清理)
'protected_tables' => [
'kku_users',
'kku_user_profiles',
'kku_configs',
// ... 添加更多受保护的表
],
// 受保护的模块
'protected_modules' => [
'User',
'Permission',
'Config',
// ... 添加更多受保护的模块
],
],
'database' => [
'batch_size' => [
'default' => 1000, // 默认批处理大小
'min' => 100, // 最小批处理大小
'max' => 10000, // 最大批处理大小
],
],
'backup' => [
'storage_path' => storage_path('app/cleanup/backups'),
'retention_days' => 30,
'max_file_size_mb' => 500,
],
确保只有授权用户可以访问清理功能:
// 在路由中间件中添加权限检查
Route::middleware(['auth', 'admin'])->group(function () {
// Cleanup 路由
});
所有清理操作都会记录详细日志:
# 查看操作日志
tail -f storage/logs/cleanup.log
# 查看特定任务的日志
php artisan cleanup:data status 1
# 检查数据库连接
php artisan cleanup:scan-tables --force
# 查看详细错误信息
tail -f storage/logs/laravel.log
# 检查数据库用户权限
SHOW GRANTS FOR 'your_user'@'localhost';
# 确保用户有以下权限:
# SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER
# 增加内存限制
CLEANUP_MEMORY_LIMIT=1024
# 增加超时时间
CLEANUP_TASK_TIMEOUT=7200
CLEANUP_QUERY_TIMEOUT=600
启用调试模式获取更多信息:
CLEANUP_DEBUG=true
CLEANUP_LOG_LEVEL=debug
CLEANUP_LOG_SQL=true
系统会自动注册以下定时任务:
// 每天凌晨2点清理过期备份
$schedule->call(function () {
CleanupService::cleanExpiredBackups(30);
})->dailyAt('02:00');
// 每天凌晨3点清理历史日志
$schedule->call(function () {
CleanupService::cleanHistoryLogs(30);
})->dailyAt('03:00');
// 每小时更新表统计信息
$schedule->call(function () {
CleanupService::scanTables(false);
})->hourly();
# 检查系统健康状态
php artisan cleanup:data status
# 查看详细的健康报告
php artisan cleanup:health-check
# 查看性能统计
php artisan cleanup:stats
# 查看最近的任务执行情况
php artisan cleanup:data status --recent=7
清除缓存
# 升级步骤
php artisan down
# 更新代码...
php artisan migrate
php artisan config:clear
php artisan cache:clear
php artisan up
如果遇到问题,请:
storage/logs/cleanup.log安装完成后,建议先在测试环境中验证所有功能正常工作,然后再部署到生产环境。