Docker 备份 推送 发表于 2021-01-16 | 更新于 2024-10-18
| 总字数: 1.6k | 阅读时长: 9分钟 | 浏览量:
Oxo1 固定备份 把这个文件保存为你喜欢的名字,不要带后缀,移动到环境中 /usr/bin python解释器位置也要正确
如何查看变量? which python
添加cron 可设置定时备份,添加增量备份部分代码可导出容器
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 import os, time, datetime, systoday_time = time.strftime("%w" , time.localtime()) username = "username" hub_url = "registry.cn-hangzhou.aliyuncs.com/" + username + "/" 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" ) 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 ): commit_images() 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() if __name__ == '__main__' : date_file(today_time) ```ini ```python import os, time, datetime, sysimage_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 增量备份 基于手动备份
测试导出这里你们看需要开启,指定路径
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 import os, time, datetime, sysimage_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() if __name__ == '__main__' : main()
新增 最好还是指定一下路径,方便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")
```ini
## 对比
Oxo1 & Oxo2
![image-20210115230626885](https://xrsec.s3.bitiful.net/IMG/20210115230629039000.png?fmt=webp&q=48)
> XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way