前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker 备份 推送

Docker 备份 推送

作者头像
XRSec
发布2022-02-11 12:50:10
3620
发布2022-02-11 12:50:10
举报
文章被收录于专栏:XRSec.Blog

Oxo1 固定备份

把这个文件保存为你喜欢的名字,不要带后缀,移动到环境中 /usr/bin python解释器位置也要正确 如何查看变量? which python 添加cron 可设置定时备份,添加增量备份部分代码可导出容器

代码语言:javascript
复制
#!/usr/bin/python3
# _*_ coding: utf-8 _*_

import os, time, datetime, sys

today_time = time.strftime("%w", time.localtime())
username = "username"
hub_url = "registry.cn-hangzhou.aliyuncs.com/" + username + "/"

# images
mfs_app_image = os.popen("docker ps -aq --filter name=mfs_app").read().strip(
    "\n")
mfs_nginx_image = os.popen(
    "docker ps -aq --filter name=mfs_nginx").read().strip("\n")
mfs_redis_image = os.popen(
    "docker ps -aq --filter name=mfs_redis").read().strip("\n")
mfs_db_image = os.popen("docker ps -aq --filter name=mfs_db").read().strip(
    "\n")

# Container
mfs_app_container = os.popen(
    "docker images -q --filter reference=mfs_app").read().strip("\n")
mfs_nginx_container = os.popen(
    "docker images -q --filter reference=mfs_nginx").read().strip("\n")
mfs_redis_container = os.popen(
    "docker images -q --filter reference=mfs_redis").read().strip("\n")
mfs_db_container = os.popen(
    "docker images -q --filter reference=mfs_db").read().strip("\n")


def date_file(today_time):
    # if today_time == "3":
    commit_images()

# else:
#     print(today_time)


def commit_images():
    try:
        if "sha256" in os.popen("docker commit " + mfs_app_image +
                                " mfs_app:latest").read().strip("\n"):
            pass
        else:
            print("mfs_app_image - 程序异常")
        if "sha256" in os.popen("docker commit " + mfs_nginx_image +
                                " mfs_nginx:latest").read().strip("\n"):
            pass
        else:
            print("mfs_nginx_image - 程序异常")
        if "sha256" in os.popen("docker commit " + mfs_redis_image +
                                " mfs_redis:latest").read().strip("\n"):
            pass
        else:
            print("mfs_redis_image - 程序异常")
        if "sha256" in os.popen("docker commit " + mfs_db_image +
                                " mfs_db:latest").read().strip("\n"):
            pass
        else:
            print("mfs_db_image - 程序异常")
    except:
        print("commit_images - 程序异常")
        sys.exit()
    tag_container()


def tag_container():
    try:
        os.system("docker tag " + mfs_db_container +
                  " " + hub_url + "mfs_db:latest")
        os.system("docker tag " + mfs_app_container +
                  " " + hub_url + "mfs_app:latest")
        os.system("docker tag " + mfs_redis_container +
                  " " + hub_url + "mfs_redis:latest")
        os.system("docker tag " + mfs_nginx_container +
                  " " + hub_url + "mfs_nginx:latest")
    except:
        print("tag_container - 程序异常")
        sys.exit()
    container_push()


def container_push():
    try:
        os.system(
            "docker push " + hub_url + "mfs_db:latest"
        )
        os.system(
            "docker push " + hub_url + "mfs_app:latest"
        )
        os.system(
            "docker push " + hub_url + "mfs_redis:latest"
        )
        os.system(
            "docker push " + hub_url + "mfs_nginx:latest"
        )
    except:
        print("container_push - 程序异常")
        sys.exit()
    mfs_pull()


def mfs_pull():
    try:
        os.system(
        "docker pull " + hub_url + "mfs_db:latest"
        )
        os.system(
        "docker pull " + hub_url + "mfs_app:latest"
        )
        os.system(
        "docker pull " + hub_url + "mfs_redis:latest"
        )
        os.system(
        "docker pull " + hub_url + "mfs_nginx:latest"
        )
    except:
        print("mfs_pull - 程序异常")
        sys.exit()
    # os.system("mfs_run")


if __name__ == '__main__':
    date_file(today_time)

Oxo2 手动备份

代码语言:javascript
复制
#!/usr/bin/python3
# _*_ coding: utf-8 _*_

import os, time, datetime, sys

image_name = sys.argv[1:]
username = "username"
hub_url = "registry.cn-hangzhou.aliyuncs.com/" + username + "/"


def img_commit(i):
    container_id = os.popen("docker ps -aq --filter name=" +
                            image_name[i]).read().strip("\n")
    img_commit_status = os.popen("docker commit " + container_id + " " +
                                 image_name[i] + ":latest").read().strip("\n")
    return img_commit_status


def img_tag(i):
    image_id = os.popen("docker images -q --filter reference=" +
                        image_name[i]).read().strip("\n")
    img_tag_status = os.popen("docker tag " + image_id + " " + hub_url +
                              image_name[0] + ":latest").read().strip("\n")
    return img_tag_status


def img_push(i):
    img_push_status = os.popen("docker pull " + hub_url + image_name[i] +
                               ":latest")
    return img_push_status


def main():
    for i in range(0, len(image_name)):
        # 测试提交
        try:
            if "sha256" in img_commit(i):
                print(image_name[i] + "\tcommit\tok! \t💕")
            else:
                print(image_name[i] + "\tcommit\terror\t😫")
        except:
            print("mod\timg_commit\t" + image_name[i] + "\terror\t😫")
            sys.exit()

        # 测试打包
        try:
            if img_tag(i) != None:
                print(image_name[i] + "\ttag\tok! \t💕")
            else:
                print(image_name[i] + "\ttag\terror\t😫")
        except:

            print("mod\timg_tag\t" + image_name[i] + "\terror\t😫")
            sys.exit()

        # 测试推送
        try:
            if "error" not in img_push(i):
                print(image_name[i] + "\tpush\tok! \t💕")
            else:
                print(image_name[i] + "\tpush\terror\t😫")
        except:
            print("mod\timg_push\t" + image_name[i] + "\terror\t😫")
            sys.exit()


if __name__ == '__main__':
    main()

Oxo3 增量备份

基于手动备份 测试导出这里你们看需要开启,指定路径

代码语言:javascript
复制
#!/usr/bin/python3
# _*_ coding: utf-8 _*_

import os, time, datetime, sys

image_name = sys.argv[1:]
username = "username"
hub_url = "registry.cn-hangzhou.aliyuncs.com/" + username + "/"


def img_commit(i):
    container_id = os.popen("docker ps -aq --filter name=" +
                            image_name[i]).read().strip("\n")
    img_commit_status = os.popen("docker commit " + container_id + " " +
                                 image_name[i] + ":latest").read().strip("\n")
    return img_commit_status


def img_tag(i):
    image_id = os.popen("docker images -q --filter reference=" +
                        image_name[i]).read().strip("\n")
    img_tag_status = os.popen("docker tag " + image_id + " " + hub_url +
                              image_name[0] + ":latest").read().strip("\n")
    return img_tag_status


def img_push(i):
    img_push_status = os.popen("docker pull " + hub_url + image_name[i] +
                               ":latest")
    return img_push_status


def img_export(i):
    img_tag_path = os.popen("pwd").read().strip("\n") + "/"
    os.system("docker export " + image_name[i] + " -o " + img_tag_path +
              image_name[i] + ".tar")

    print(image_name[i] + "\texport\tok! \t💕")

    img_file_path = img_tag_path + image_name[i] + ".tar"
    img_file_size = os.path.getsize(img_file_path)

    if img_file_size >= 524288000:
        img_file_size = "{0:.2f}".format(
            (os.path.getsize(img_file_path) / 1024 / 1024 / 1024))
        print(image_name[i] + "\texport\tto\t" + img_file_path + "\t" +
              img_file_size + "G")
    else:
        img_file_size = "{0:.2f}".format(
            (os.path.getsize(img_file_path) / 1024 / 1024))
        print(image_name[i] + "\texport\tto\t" + img_file_path + "\t" +
              img_file_size + "M")


def main():
    for i in range(0, len(image_name)):
        # 测试提交
        try:
            if "sha256" in img_commit(i):
                print(image_name[i] + "\tcommit\tok! \t💕")
            else:
                print(image_name[i] + "\tcommit\terror\t😫")
                sys.exit()
        except:
            print("mod\timg_commit\t" + image_name[i] + "\terror\t😫")
            sys.exit()

        # 测试打包
        try:
            if img_tag(i) != None:
                print(image_name[i] + "\ttag\tok! \t💕")
            else:
                print(image_name[i] + "\ttag\terror\t😫")
                sys.exit()
        except:

            print("mod\timg_tag\t" + image_name[i] + "\terror\t😫")
            sys.exit()

        # 测试推送
        try:
            if "error" not in img_push(i):
                print(image_name[i] + "\tpush\tok! \t💕")
            else:
                print(image_name[i] + "\tpush\terror\t😫")
                sys.exit()
        except:
            print("mod\timg_push\t" + image_name[i] + "\terror\t😫")
            sys.exit()

        # 测试导出
        # img_export(i)
# ______________________________________________________

        # try:
        #     if "error" not in img_export(i):
        #         print(image_name[i] + "\texport\tok! \t💕")
        #     else:
        #         print(image_name[i] + "\texport\terror\t😫")
        # except:
        #     print("mod\timg_export\t" + image_name[i] + "\terror\t😫")
        #     sys.exit()

if __name__ == '__main__':
    main()

新增

最好还是指定一下路径,方便crontab

代码语言:javascript
复制
def img_export(i):
    # img_tag_path = os.popen("pwd").read().strip("\n") + "/"
    # 时间戳命名
    img_tag_path = "/nas/bak/" + time.strftime('%Y-%m-%d',time.localtime(time.time()))
    if os.path.exists(img_tag_path) == True:
        img_tag_path = img_tag_path + "/"
    else:
        os.system("mkdir " + img_tag_path)
        img_tag_path = img_tag_path + "/"
        
    os.system("docker export " + image_name[i] + " -o " + img_tag_path +
              image_name[i] + ".tar")
	  # 确认创建完成
    print(image_name[i] + "\texport\tok! \t💕")
		#测试文件大小
    img_file_path = img_tag_path + image_name[i] + ".tar"
    img_file_size = os.path.getsize(img_file_path)
		# 文件大小判断
    if img_file_size >= 524288000:
        img_file_size = "{0:.2f}".format(
            (os.path.getsize(img_file_path) / 1024 / 1024 / 1024))
        print(image_name[i] + "\texport\tto\t" + img_file_path + "\t" +
              img_file_size + "G")
    else:
        img_file_size = "{0:.2f}".format(
            (os.path.getsize(img_file_path) / 1024 / 1024))
        print(image_name[i] + "\texport\tto\t" + img_file_path + "\t" +
              img_file_size + "M")

对比

Oxo1 & Oxo2

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Oxo1 固定备份
  • Oxo2 手动备份
  • Oxo3 增量备份
  • 对比
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档