zoukankan      html  css  js  c++  java
  • 使用python脚本定时备份web网站

     1 #!/usr/bin/env python
     2 #-*- coding: utf-8 -*-
     3 
     4 import os
     5 import time
     6 
     7 # 备份的指定目录
     8 source = ['/data/www/Admin/','/data/www/tpl/']
     9 # 备份文件存放路径
    10 target_dir='/home/backup/web_back/'
    11 # 备份时长
    12 data = 5
    13 # 备份日志
    14 filebak_log = "/var/log/filebak.log"
    15 # 删除备份文件日志
    16 filerm_log = "/var/log/filerm.log"
    17 
    18 def file_bak():
    19     """备份指定目录下的文件"""
    20     target=target_dir+time.strftime('%Y%m%d%H%M%S')+'.tar.gz'
    21     cmd='tar -zcPf %s %s '%(target,' '.join(source))
    22     if os.system(cmd)==0 :
    23         with open(filebak_log,'a') as filebak:
    24             filebak.write('successfull backup to %s 
    ' % target)
    25  
    30 def file_rm():
    31     """删除备份目录下超过一定时长的文件"""
    32     f = list(os.listdir(target_dir))
    33     now_time = time.strftime('%Y%m%d%H%M%S')[0:8]
    34     for i in f:
    35         if i[15:] == 'tar.gz':
    36             exit_time = i[0:8]
    37             update_time = int(exit_time) + data
    38             if update_time < int(now_time):
    39                 os.remove(target_dir+i)
    40                 with open(filerm_log,'a') as file_log:
    41                     file_log.write("%s删除备份文件%s 
    " % (now_time,i))
    42 
    43 if __name__ == '__main__':
    44     file_bak()
    45     file_rm()

     # linux定时执行python文件

     # crontab -e 添加如下信息:

     # 0 4 * * * /usr/bin/python /root/bak.py >> /var/log/bak.py.log 2>&1

  • 相关阅读:
    OpenMP vs WinSxS
    JIT, dynarec and binary translation
    VC++2010 bug
    控制和释放共享内存块
    分配和释放信号量
    代码列表5.1 (shm.c) 尝试共享内存
    绑定和脱离
    信号量
    每个字段动态添加一个随机数
    最近做了一个红底鞋类电商网站
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/9849601.html
Copyright © 2011-2022 走看看