mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

08_Other-GitHub Actions 定时执行脚本,备份又拍云云存储

前言#

使用 GitHub Acitons,定时跑 Python FTP 脚本,达到 又拍云云存储 与 GitHub 仓库间的备份

仓库示例:https://github.com/mycpen/image_bed/tree/main/.github

个人示例#

1. 新增 Workflow YML 文件#

复制以下我的 yml 示例;或是按照 这篇文章,选择想要的 workflow 模板,再自定义修改内容(Actions => New workflow = Choose a workflow)

我的文件路径为 .github/workflows/python-app.yml,内容如下:

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
  schedule:
    - cron: "0 17 * * 5" # 周六 1:00
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - uses: szenius/set-timezone@v1.0 # 设置执行环境的时区
      with:
        timezoneLinux: "Asia/Shanghai"
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"
    - name: Run sync script
      env:
          UPYUNUSERNAME: ${{ secrets.UPYUNUSERNAME }}
          UPYUNPASSWORD: ${{ secrets.UPYUNPASSWORD }}
      run: |
        git config --global user.name ${{ secrets.GITHUBUSERNAME }}
        git config --global user.email ${{ secrets.GITHUBEMAIL }}
        python .github/ftp.py
        bash git.sh

参数解释:

  • cron:定义工作流的时间表(有延迟),为格林尼治时间(UTC),换算为北京时间需在该基础上增加 8 小时,参考:Actions 中的 cron 格式和设置Actions 时区及缓存问题处理

  • UPYUNUSERNAME, UPYUNPASSWORD, GITHUBUSERNAME, GITHUBEMAIL,为自定义的 Secrets 常量

    常量名释义
    UPYUNUSERNAME又拍云 FTP 用户名,格式「操作员名 / 服务名」,参考 又拍云视频教程又拍云文档
    UPYUNPASSWORD又拍云 FTP 密码,参考同上
    GITHUBUSERNAMEGitHub 账号,如 mycpen
    GITHUBEMAILGitHub 账号绑定的主邮箱
  • ftp.py:Python 脚本,使用 FTP 同步云存储与仓库的文件内容

  • git.sh:将本地变更推送至远端的脚本,git add, git commit, git push 这类

2. 新增 Secrets 常量#

新增 4 个 Secrets 常量,UPYUNUSERNAME、UPYUNPASSWORD、GITHUBUSERNAME、GITHUBEMAIL,具体含义同上

image-20230417094207479

3. 授予 Workflow 读写权限#

参考:https://blog.csdn.net/jj89929665/article/details/129817011

image-20230417095659542

4. 新建 Python 同步脚本#

脚本内容来自:Python 实现的 FTP 上传和下载功能

脚本最后的 if 语句中声明了相关路径和参数

我的脚本路径为 .github/ftp.py,作用是同步仓库 image/ 目录与又拍云云存储 /image/ 目录,内容如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-


from cmath import log
from ftplib import FTP
import os
import sys
import time
import socket
import subprocess

class MyFTP:
    """
        ftp自动下载、自动上传脚本,可以递归目录操作
        作者:欧阳鹏
        博客地址:http://blog.csdn.net/ouyang_peng/article/details/79271113
    """

    def __init__(self, host, port=21):
        """ 初始化 FTP 客户端
        参数:
                 host:ip地址

                 port:端口号
        """
        # print("__init__()---> host = %s ,port = %s" % (host, port))

        self.host = host
        self.port = port
        self.ftp = FTP()
        # 重新设置下编码方式
        #self.ftp.encoding = 'gbk'
        self.ftp.encoding = 'utf8'
        # 获取脚本路径
        path = os.path.dirname(os.path.realpath(__file__))
        # self.log_file = open(path + "/log.txt", "a", encoding='utf-8')
        self.file_list = []

    def login(self, username, password):
        """ 初始化 FTP 客户端
            参数:
                  username: 用户名

                 password: 密码
            """
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            # 0主动模式 1 #被动模式
            self.ftp.set_pasv(True)
            # 打开调试级别2,显示详细信息
            # self.ftp.set_debuglevel(2)

            self.debug_print('开始尝试连接到 %s' % self.host)
            self.ftp.connect(self.host, self.port)
            self.debug_print('成功连接到 %s' % self.host)

            self.debug_print('开始尝试登录到 %s' % self.host)
            self.ftp.login(username, password)
            self.debug_print('成功登录到 %s' % self.host)

            self.debug_print(self.ftp.welcome)
        except Exception as err:
            self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
            pass

    def is_same_size(self, local_file, remote_file):
        """判断远程文件和本地文件大小是否一致

           参数:
             local_file: 本地文件

             remote_file: 远程文件
        """
        try:
            remote_file_size = self.ftp.size(remote_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            remote_file_size = -1

        try:
            local_file_size = os.path.getsize(local_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            local_file_size = -1

        self.debug_print('local_file_size:%d  , remote_file_size:%d' % (local_file_size, remote_file_size))
        if remote_file_size == local_file_size:
            return 1
        else:
            return 0

    def download_file(self, local_file, remote_file):
        """从ftp下载文件
            参数:
                local_file: 本地文件

                remote_file: 远程文件
        """
        self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))

        if self.is_same_size(local_file, remote_file):
            self.debug_print('%s 文件大小相同,无需下载' % local_file)
            return
        else:
            try:
                self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
                buf_size = 1024
                file_handler = open(local_file, 'wb')
                self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
                file_handler.close()
            except Exception as err:
                self.debug_print('下载文件出错,出现异常:%s ' % err)
                return

    def download_file_tree(self, local_path, remote_path):
        """从远程目录下载多个文件到本地目录
                       参数:
                         local_path: 本地路径

                         remote_path: 远程路径
                """
        print("download_file_tree()--->  local_path = %s ,remote_path = %s" % (local_path, remote_path))
        try:
            self.ftp.cwd(remote_path)
        except Exception as err:
            self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
            return

        if not os.path.isdir(local_path):
            self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
            os.makedirs(local_path)

        self.debug_print('切换至目录: %s' % self.ftp.pwd())

        self.file_list = []
        # 方法回调
        self.ftp.dir(self.get_file_list)

        remote_names = self.file_list
        self.debug_print('远程目录 列表: %s' % remote_names)
        for item in remote_names:
            file_type = item[0]
            file_name = item[1]
            local = os.path.join(local_path, file_name)
            if file_type == 'd':
                print("download_file_tree()---> 下载目录: %s" % file_name)
                self.download_file_tree(local, file_name)
            elif file_type == '-':
                print("download_file()---> 下载文件: %s" % file_name)
                self.download_file(local, file_name)
        self.ftp.cwd("..")
        self.debug_print('返回上层目录 %s' % self.ftp.pwd())
        return True

    def upload_file(self, local_file, remote_file):
        """从本地上传文件到ftp

           参数:
             local_path: 本地文件

             remote_path: 远程文件
        """
        if not os.path.isfile(local_file):
            self.debug_print('%s 不存在' % local_file)
            return

        if self.is_same_size(local_file, remote_file):
            self.debug_print('跳过相等的文件: %s' % local_file)
            return

        buf_size = 1024
        file_handler = open(local_file, 'rb')
        self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
        file_handler.close()
        self.debug_print('上传: %s' % local_file + "成功!")

    def upload_file_tree(self, local_path, remote_path):
        """从本地上传目录下多个文件到ftp
           参数:

             local_path: 本地路径

             remote_path: 远程路径
        """
        if not os.path.isdir(local_path):
            self.debug_print('本地目录 %s 不存在' % local_path)
            return

        self.ftp.cwd(remote_path)
        self.debug_print('切换至远程目录: %s' % self.ftp.pwd())

        local_name_list = os.listdir(local_path)
        for local_name in local_name_list:
            src = os.path.join(local_path, local_name)
            if os.path.isdir(src):
                try:
                    self.ftp.mkd(local_name)
                except Exception as err:
                    self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
                self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
                self.upload_file_tree(src, local_name)
            else:
                self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
                self.upload_file(src, local_name)
        self.ftp.cwd("..")

    def close(self):
        """ 退出ftp
        """
        self.debug_print("close()---> FTP退出")
        self.ftp.quit()
        # self.log_file.close()

    def debug_print(self, s):
        """ 打印日志
        """
        self.write_log(s)

    def deal_error(self, e):
        """ 处理错误异常
            参数:
                e:异常
        """
        log_str = '发生错误: %s' % e
        self.write_log(log_str)
        sys.exit()

    def write_log(self, log_str):
        """ 记录日志
            参数:
                log_str:日志
        """
        time_now = time.localtime()
        date_now = time.strftime('%Y-%m-%d', time_now)
        format_log_str = "%s ---> %s \n " % (date_now, log_str)
        print(format_log_str)
        # self.log_file.write(format_log_str)

    def get_file_list(self, line):
        """ 获取文件列表
            参数:
                line:
        """
        file_arr = self.get_file_name(line)
        # 去除  . 和  ..
        if file_arr[1] not in ['.', '..']:
            self.file_list.append(file_arr)

    def get_file_name(self, line):
        """ 获取文件名
            参数:
                line:
        """
        pos = line.rfind(':')
        while (line[pos] != ' '):
            pos += 1
        while (line[pos] == ' '):
            pos += 1
        file_arr = [line[0], line[pos:]]
        return file_arr


if __name__ == "__main__":
    # 清除日志
    path = os.path.dirname(os.path.realpath(__file__))      # 脚本路径
    # if os.path.exists(path + '/log.txt'):
    #     log_file = path + '/log.txt 'if os.sep == "/" else path + '\\' + 'log.txt'
    #     subprocess.Popen(f'rm -rf {log_file}', shell=True)
    #     time.sleep(1)

    # 获取 Actions Secrets 常量
    upyunUsername = os.environ["UPYUNUSERNAME"]
    upyunPassword = os.environ["UPYUNPASSWORD"]

    my_ftp = MyFTP("v0.ftp.upyun.com")
    my_ftp.login(upyunUsername, upyunPassword)

    # 下载目录
    # 又拍云云存储 → 本地 image/
    if os.sep == "\\":      # Windows
        pass
    elif os.sep == "/":     # Unix
        my_ftp.download_file_tree("image/", "/image/")  # image/ 仓库目录; /image/ 又拍云云存储目录

    # 上传目录
    # 本地 image/ → 又拍云云存储
    if os.sep == "\\":
        pass
    elif os.sep == "/":
        my_ftp.upload_file_tree("image/", "/image/")  # image/ 仓库目录; /image/ 又拍云云存储目录
        my_ftp.close()

5. git.sh 推送变更#

个人习惯将 push 命令写在一个文件里,放在仓库根目录下

#!/usr/bin/bash

# 远端同步至本地
git pull

# 推送变更
git add .
git commit -m "$(date +'%Y/%m/%d')"
git push

参考文章#

  1. * GitHub Actions 中 python 脚本获取仓库 secrets
  2. * 使用 github 仓库创建定时任务,定时签到等服务
  3. * Python 实现的 FTP 上传和下载功能
  4. * 使用 Github Action 实现全自动部署
  5. * 又拍云:如何使用 FTP、API 上传文件
  6. * 又拍云:创建存储服务和使用 FTP 上传
  7. * Github/Gitlab Actions 中的 cron 格式和设置方法
  8. * Github Actions 执行 Python 定时任务(时区及缓存问题处理)
  9. * 运行 Github Actions unable to access ‘https://github.com/x/‘: The requested URL returned error: 403
  10. * workflow_dispatch
  11. 白嫖 github 的 Action 做定时任务
  12. Github+Action 实现自动定时推送
  13. 基于 github 中 action 服务的自动化打包部署
  14. 基于 GITHUB ACTION 的定时任务,真香!
  15. GitHub Actions 定时运行代码:每天定时百度链接推送
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.