欢迎来到代码驿站!

Tomcat

当前位置:首页 > 服务器 > Tomcat

tomcat应用文件备份脚本及日志备份脚本的示例代码

时间:2021-03-20 10:02:11|栏目:Tomcat|点击:

下面先给大家介绍tomcat应用文件备份脚本

#!/bin/bash
#Back up the entire tomcat8080-dist to the /backup directory
backuppath=/mnt/tomcat8080-dist/webapps/
backpath=/backup/backup-tomcat8080-dist/
action=/backup/backup-tomcat8080-dist/
APPNAME=dist
BACKUPNAME1=dist
BACKUPNAME2=spongod-module-admin
BACKUPNAME3=spongod-module-interface
for i in 1,2,3
do
    echo "" >>/backup/log/backup.log
done
echo "this backup start!" >>/backup/log/backup.log
#echo "----STRAT BACKUP TIME:`date '+%Y%m%d-%H%M'`" >>/backup/log//backup.log
if [[ -n $backpath ]] && [[ -n $backuppath ]];then
    echo "--------STARTUP BACKUP TIME:`date '+%Y-%m-%d-%H-%M'`-------" >>/backup/log/backup.log
    cd /mnt/tomcat8080-dist/ 2>> /backup/log/backup.log
    time=`date '+%Y-%m-%d-%H-%M'`
    tar czf /backup/backup-tomcat8080-dist/$APPNAME-$time\.tar.gz webapps/$BACKUPNAME1/ webapps/$BACKUPNAME2/ webapps/$BACKUPNAME3/ 2>>/backup/log/BackupFailed.log
    if [ $? -eq 0 ];then
        echo -e "----\033[32m BACKUP SUCCESSFUL\033[0m-----------------------" >>/backup/log/backup.log
        echo "--------BACKUP END TIME is:`date '+%Y-%m-%d-%H-%M'`--------" >>/backup/log/backup.log
        echo "This backup ends" >>/backup/log/backup.log
        for i in 1,2,3
        do
            echo "" >>/backup/log/backup.log
        done
    else
        echo -e "----\033[31m Backup failed, please try again \033[0m -------------" >>/backup/log/BackupFailed.log
        echo "--------Failed Time:`date '+%Y-%m-%d-%H-%M'`-----------" >>/backup/log/BackupFailed.log
    fi
fi
#Delete files from three days away
echo "" >>/backup/log/delete-backup.log
echo "start deleted file" >>/backup/log/delete-backup.log
find $action -mtime +3 -type f |xargs rm -f
if [ $? -eq 0 ];then
    echo "----------File deleted successfully--------------" >>/backup/log/delete-backup.log
else
    echo "----------File deletion failed-------------------" >>/backup/log/delete-backup.log
    echo "" >>/backup/log/delete-backup.log
fi

下面给大家介绍tomcat的日志备份脚本

这个是python脚本,默认对tomcat日志保存15天,我们自己可以手动修改的

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os,datetime,shutil,time
logRootPath = "/data/webservers"
backupRootPath = "/data/logs/backup/tomcat"
delNDayBefore = 15
def getYestoday():
  now = datetime.datetime.now()
  delta = datetime.timedelta(days=-1)
  yestoday = now + delta
  yestodayStr = yestoday.strftime('%Y%m%d')
  return yestodayStr
def createDir(path):
  if not os.path.exists(path):
    os.makedirs(path)
def backupLog():
  for path in os.listdir(logRootPath):
    logPath = os.path.join(os.path.join(logRootPath, path), "logs")
    backupPath = os.path.join(backupRootPath, path)
    logFilePath = os.path.join(logPath, "catalina.out")
    targetFileName = "catalina.out." + getYestoday()
    targetFilePath = os.path.join(backupPath, targetFileName)
    createDir(backupPath)
    shutil.copy(logFilePath,targetFilePath)
    os.system("echo '' > %s" % (logFilePath))
def getNDayTime(n):
  currTime = time.time()
  deltTime = 3600*24*n # n天前
  return currTime - deltTime
def delLog():
  fileInfos = os.walk(backupRootPath)
  beforeTime = getNDayTime(delNDayBefore)
  for path,d,filelist in fileInfos:
    for filename in filelist:
      filePath = os.path.join(path, filename)
      modifyTime = os.stat(filePath).st_mtime
      print(filePath,":",modifyTime,"-",beforeTime,"-",modifyTime < beforeTime)
      if modifyTime < beforeTime:
        print("remove file %s" % (filePath))
        os.remove(filePath)
def do():
  backupLog()
  delLog()
if __name__ == "__main__":
  do()

总结

上一篇:简单实现nginx+tomcat的反向代理与动静分离

栏    目:Tomcat

下一篇:Eclipse/tomcat 如何实现热部署和热启动

本文标题:tomcat应用文件备份脚本及日志备份脚本的示例代码

本文地址:http://www.codeinn.net/misctech/84517.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有