Docker 备份 推送

Oxo1 固定备份

把这个文件保存为你喜欢的名字,不要带后缀,移动到环境中 /usr/bin python解释器位置也要正确

如何查看变量? which python

添加cron 可设置定时备份,添加增量备份部分代码可导出容器

#!/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 手动备份

#!/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 增量备份

基于手动备份

测试导出这里你们看需要开启,指定路径

#!/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()

image-20210116000016133

新增

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

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

image-20210115230626885