欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-03-19 08:19:26
下面先给大家介绍tomcat应用文件备份脚本 #!/bin/bash #back up the entire tomcat8080-dist to the /b...

下面先给大家介绍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()

总结

以上所述是小编给大家介绍的tomcat应用文件备份脚本及日志备份脚本的示例代码,希望对大家有所帮助