Linux Java日志备份常用方法如下:
logrotate工具
/etc/logrotate.d/java):/path/to/java/logs/*.log {
daily
rotate 7
compress
missingok
notifempty
create 0644 root root
}
logrotate -d /etc/logrotate.d/java,重新加载:sudo systemctl reload logrotate。Shell脚本+定时任务
#!/bin/bash
BACKUP_DIR="/backup/java_logs"
DATE=$(date +%Y%m%d)
cp /path/to/java/logs/*.log "$BACKUP_DIR/java_$DATE.log"
> /path/to/java/logs/*.log
find "$BACKUP_DIR" -mtime +30 -name "*.log" -exec rm -f {} \;
crontab -e设置定时任务(如每天凌晨执行)。远程备份(rsync)
#!/bin/bash
LOCAL_DIR="/path/to/java/logs"
REMOTE_USER="user"
REMOTE_HOST="remote.server.com"
REMOTE_DIR="/backup/java_logs"
rsync -avz "$LOCAL_DIR" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"
日志框架内置轮转
TimeBasedRollingPolicy:<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/app-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
专业日志管理工具
注意:根据实际需求选择方案,优先使用系统工具(如logrotate)或框架内置功能,确保备份策略与业务场景匹配。