章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • python

    • python基础

      • python基础知识
      • python基础较难的15个知识点
    • FastAPI

    • python每日练习脚本

    • python3给防火墙添加放行
    • python生成部署脚本
    • python将多个文件内容输出到一个文件中
    • 使用 Aligo 定时备份服务器文件
      • python监控日志文件并发送钉钉告警
      • python监控数据库脚本并发送钉钉告警
      • 使用python编写自动化发布脚本
      • 查询redis列表某个元素
      • centos7安装python3
      • python环境管理工具介绍
      • conda安装和镜像源配置
      • pip更换国内源
      • python爬虫
      • python环境启动服务报错缺少glibc库版本
      • 监控目录或文件变化
      • 批量更改文件
      • python引用数据库
    • shell

    • go

    • 编程
    • python
    章工运维
    2023-04-25
    目录

    使用 Aligo 定时备份服务器文件

    # 简介

    在现在的互联网时代,数据显得尤为重要,我们的服务器中也有一些重要数据,如果服务器被攻击或者过期忘记取回数据,那造成的后果难以想象,所以要做好定时备份的习惯,但是人又懒怎么办呢,今天给出一个 Aligo 定时备份文件的解决方案。

    # 源码

    使用 python3.8 压缩备份文件,使用第三方库 aligo 来上传到阿里云盘

    from aligo import Aligo
    import time 
    import datetime
    import gzip
    import os
    import zipfile
    import shutil
    
    # 备份目录路径数组
    backup_dir_list = [
        '/www/lskypro/storage/app/uploads',
        '/www/shinie.top',
        '/www/bot',
        '/www/bimg.cc'
    ]
    # 压缩包路径
    compress_dir = '/www/aligo/backup'
    # 压缩包文件名数组,可添加文件夹
    compress_name_list = [
        'lskypro.zip',
        'shinie.top.zip',
        'bot.zip',
        'bimg.cc.zip'
    ]
    # 阿里云盘上传路径upload_dir = '服务器数据备份'
    # 压缩包保存多少天
    day=7
    # 接收登录二维码邮件
    email='1982989137@qq.com'
    # 防伪字符串
    security_str='backup-server-file'
    
    t=time.strftime("%Y-%m-%d",time.localtime())
    
    if __name__ == '__main__':
        ali = Aligo(email=('1982989137@qq.com', 'haivhisaofwu1920u90du90w'))
        # 遍历备份目录
        for i in range(0,len(backup_dir_list)):
            remote_folder = ali.get_folder_by_path(upload_dir)
    
            # 遍历压缩目录
            for zip_file in os.listdir(compress_dir):
                t_be=zip_file[0:10]
                start = datetime.datetime.strptime(t_be, "%Y-%m-%d")
                end = datetime.datetime.strptime(t, "%Y-%m-%d")
                days = (end-start).days #计算天数差
                old_zip_file=compress_dir+'/'+t_be
                # 删除过期文件
                if days>day:
                    print("旧文件:"+old_zip_file+"已过期")
                    print("即将删除本地压缩文件:"+old_zip_file)
                    if os.path.exists(old_zip_file):
                        shutil.rmtree(old_zip_file)
            
            # 创建压缩包完整目录 /www/backup/2022-11-11/
            compress_pull_dir=compress_dir+'/'+t+"/" 
            if os.path.exists(compress_pull_dir)==False:
                print("创建压缩包完整目录:"+compress_pull_dir)
                os.makedirs(compress_pull_dir)
    
            # 压缩
            zip_file=compress_pull_dir+compress_name_list[i]
            zip = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED)
            for root,dirs,files in os.walk(backup_dir_list[i]):    #遍历统计
                for each in files:
                    if os.path.exists(root+"/"+each):
                        print("压缩:"+root+"/"+each)
                        zip.write(root+"/"+each)
            zip.close()
            # 同步文件夹(以本地文件为主)
            print("同步文件夹……")
            ali.sync_folder(local_folder=compress_dir, remote_folder=remote_folder.file_id,flag=True,follow_delete=True)
    
    
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75

    参数解释:

    参数 解释 参考值
    backup_dir_list 备份目录路径数组,可选择备份服务器的文件路径(必要) [‘/www/lskypro/storage/app/uploads’,’/www/shinie.top’]
    compress_dir 压缩包存放的目录,可与备份路径不一样(必要) ‘/www/aligo/backup’
    compress_name_list 压缩包文件名数组,需和 backup_dir_list 长度一致,且一一对应(必要) [ ‘lskypro.zip’, ‘shinie.top.zip’]
    upload_dir 阿里云盘上传路径(必要) ‘AList / 服务器数据备份’
    day 压缩包保存天数(必要) 7
    email 接收登录二维码邮件(必要) ‘1982989137@qq.com‘
    security_str 防伪字符串 ‘backup-server-file’

    先运行一遍(记得改邮箱)登录阿里云盘,获取 token

    编辑 /etc/crontab:

    2 0 * * * root /www/python3.8.6/bin/python3.8 /www/aligo/upload.py
    
    1

    [原文链接](使用Aligo定时备份服务器文件 | 枫叶 (opens new window))

    微信 支付宝
    上次更新: 2023/04/26, 16:58:05

    ← python将多个文件内容输出到一个文件中 python监控日志文件并发送钉钉告警→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式