Skip to content

Feature/image backup script #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7c7669e
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
283ec16
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
efcfaa5
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
7e77099
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
e2db3fc
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
45fd7f2
Feat(创建镜像):添加腾讯云镜像服务
Jun 11, 2024
aec71db
Feat(创建镜像):添加腾讯云镜像服务2
Jun 11, 2024
dae243f
Feat(创建镜像):添加腾讯云镜像服务3
Jun 11, 2024
8868dfc
Feat(创建镜像):添加腾讯云镜像服务3
Jun 11, 2024
f77b358
Feat(创建镜像):添加腾讯云镜像服务4
Jun 11, 2024
4425e96
添加镜像
Jun 11, 2024
39de6f3
添加镜像1
Jun 11, 2024
5b7d5dc
添加镜像1
Jun 11, 2024
05ee26e
Feat(添加镜像):
Jun 12, 2024
28bfead
add 1
Jun 12, 2024
7427435
add2
Jun 14, 2024
1d78634
add certbot/certbot
Jul 30, 2024
d6f5841
add certbot/certbot
Jul 30, 2024
b8878be
fix certbot/certbot
Jul 30, 2024
8935cbf
fix certbot/certbot
Jul 30, 2024
fbafab0
fix certbot/certbot
Jul 30, 2024
5161f8b
fix certbot/certbot
Jul 30, 2024
1b4fdf6
fix certbot/certbot
Jul 30, 2024
67687de
Add(certd)
Jul 31, 2024
1074c75
Add(certd)
Jul 31, 2024
ca27631
add2
Aug 19, 2024
3cdb8d4
add2
Aug 21, 2024
ce9e15b
Update images.txt
subahua Aug 23, 2024
0302838
add2
Aug 27, 2024
afd6c45
Update images.txt
subahua Aug 27, 2024
1fea74a
Update images.txt
subahua Aug 27, 2024
cdeac27
save
Sep 23, 2024
f50cb02
save
Sep 23, 2024
715b8e9
jupyter
Dec 19, 2024
b77279c
add test
Mar 12, 2025
cab0c84
Merge branch 'main' of https://github.com/subahua/docker_image_pusher
Mar 12, 2025
2d3b813
feat: Enhanced main.py to implement image backup and recording functi…
google-labs-jules[bot] Jun 17, 2025
435dcc9
feat: Add GitHub Actions Workflow for daily image backup
google-labs-jules[bot] Jun 18, 2025
d2cf920
docs: Update the script and Workflow instructions in README.md to Chi…
google-labs-jules[bot] Jun 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/daily_image_backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Daily Docker Image Backup

on:
schedule:
- cron: '0 2 * * *' # Daily at 02:00 UTC
workflow_dispatch: # Allows manual triggering

jobs:
backup_images:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install dependencies
run: pip install aiohttp

- name: Run image backup script
run: python main.py
env:
TARGET_REGISTRY_URL: ${{ secrets.TARGET_REGISTRY_URL_SECRET }}
TARGET_NAMESPACE: ${{ secrets.TARGET_NAMESPACE_SECRET }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME_SECRET }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD_SECRET }}
2 changes: 1 addition & 1 deletion .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Docker
on:
workflow_dispatch:
push:
branches: [ main ]
branches: [ master ]


env:
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/tencent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: tencent_docker
on:
workflow_dispatch:
push:
branches: [main]

permissions:
contents: write

env:
TENCENT_NAME_SPACE: "${{ secrets.TENCENT_NAME_SPACE }}"
TENCENT_REGISTRY_USER: "${{ secrets.TENCENT_REGISTRY_USER }}"
TENCENT_REGISTRY_PASSWORD: "${{ secrets.TENCENT_REGISTRY_PASSWORD }}"

jobs:
build:
name: Pull
runs-on: ubuntu-latest
steps:
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf

- name: Check out code
uses: actions/checkout@v2

- name: Build and push image TENCENT
run: |
docker login https://ccr.ccs.tencentyun.com --username=$TENCENT_REGISTRY_USER --password $TENCENT_REGISTRY_PASSWORD
while IFS= read -r line; do
[[ -z "$line" ]] && continue
echo "docker pull $line"
docker pull $line
# 获取镜像的完整名称,例如kasmweb/nginx:1.25.3(命名空间/镜像名:版本号)
image=$(echo "$line" | awk '{print $NF}')
# 获取 镜像名:版本号 例如nginx:1.25.3
image_name_tag=$(echo "$image" | awk -F'/' '{print $NF}')
new_image="ccr.ccs.tencentyun.com/$TENCENT_NAME_SPACE/$image_name_tag"
echo "docker tag $image $new_image"
docker tag $image $new_image
echo "docker push $new_image"
docker push $new_image
done < images.txt
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.lh
.vscode
*.pyc
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,134 @@ docker pull registry.cn-hangzhou.aliyuncs.com/shrimp-images/alpine
registry.cn-hangzhou.aliyuncs.com 即 ALIYUN_REGISTRY<br>
shrimp-images 即 ALIYUN_NAME_SPACE<br>
alpine 即images.txt里面填的镜像<br>

## 命令行脚本: `main.py`

该脚本提供了一个命令行界面,用于将 Docker Hub 上的 Docker 镜像备份到指定的私有仓库。它会获取镜像标签,拉取镜像,为目标仓库重新标记镜像,推送它们,并记录已备份的镜像以避免重复操作。

### 先决条件

* Python 3.7+ (由于使用了 `asyncio` 和 `aiohttp`)。
* 在脚本执行的机器上已安装并正在运行 Docker。
* 可以访问 Docker Hub 的网络 (用于获取公共镜像)。
* 拥有目标私有 Docker 仓库的凭据和访问权限 (例如,腾讯云 CCR、阿里云 ACR、Docker Hub 私有仓库等)。

### 依赖项

该脚本需要以下 Python 包:
* `aiohttp`

您可以使用 pip 安装它:
```bash
pip install aiohttp
```

### 配置

配置通过环境变量管理凭据和目标仓库详细信息,通过命令行参数管理操作参数。

#### 环境变量 (必需)

在运行脚本之前 **必须** 设置这些环境变量。如果缺少任何一个,脚本将退出。

* `TARGET_REGISTRY_URL`: 您的目标私有仓库的 URL。
* 示例: `ccr.ccs.tencentyun.com` 或 `registry.aliyuncs.com` 或 `docker.io` (用于 Docker Hub)。
* `TARGET_NAMESPACE`: 您的目标私有仓库中用于存储镜像的命名空间。
* 示例: `my-docker-backups` 或 `myusername` (如果使用 Docker Hub 作为目标)。
* `DOCKER_USERNAME`: 用于向 `TARGET_REGISTRY_URL` 进行身份验证的用户名。
* `DOCKER_PASSWORD`: 用于向 `TARGET_REGISTRY_URL` 进行身份验证的密码。

#### 命令行参数

这些参数是可选的,用于控制脚本的执行:

* `--num-tags` (`-n`): 为每个镜像获取和处理的最新标签数量。
* 默认值: `5`
* `--record-file` (`-r`): 用于记录已备份镜像以防止重复处理的文件路径。
* 默认值: `backed_up_images.txt`
* `--image-urls` (`-u`): 以逗号分隔的 Docker Hub 镜像 URL 字符串,用于指定要处理的镜像。
* 示例: `"https://hub.docker.com/_/nginx/tags,https://hub.docker.com/r/prom/prometheus/tags"`
* 如果未提供,脚本将使用 `main.py` 中硬编码的默认镜像 URL 列表。

### 运行脚本

1. **设置环境变量:**
```bash
export TARGET_REGISTRY_URL="your-registry.example.com"
export TARGET_NAMESPACE="your-namespace"
export DOCKER_USERNAME="your-registry-username"
export DOCKER_PASSWORD="your-registry-password"
```

2. **执行 `main.py`:**
```bash
python main.py [选项]
```
例如,备份 `alpine` 和 `redis` 的最新3个标签:
```bash
python main.py -n 3 -u "https://hub.docker.com/_/alpine/tags,https://hub.docker.com/_/redis/tags"
```
使用默认镜像列表并为每个镜像获取5个标签:
```bash
python main.py
```

脚本会将其进度记录到标准输出,包括获取标签、拉取、标记、推送镜像以及遇到的任何错误的信息。

### 工作原理

脚本执行以下步骤:

1. **加载配置:** 读取环境变量并解析命令行参数。
2. **Docker 登录:** 使用提供的凭据登录到目标 Docker 仓库。如果登录失败则退出。
3. **镜像处理循环:** 对于指定的每个源镜像 URL:
a. **获取标签:** 使用其 API 从 Docker Hub 检索指定数量的最新标签。
b. **标签处理循环:** 对于获取的每个标签:
i. **检查备份记录:** 查询记录文件 (例如 `backed_up_images.txt`),看是否已备份特定的镜像和标签组合。如果是,则跳到下一个标签。
ii. **拉取 (Pull):** 如果未备份,则从 Docker Hub 拉取镜像 (例如 `nginx:latest`)。
iii. **标记 (Tag):** 为目标私有仓库重新标记拉取的镜像 (例如 `your-registry.example.com/your-namespace/nginx:latest`)。
iv. **推送 (Push):** 将新标记的镜像推送到目标私有仓库。
v. **记录备份:** 如果所有先前的步骤 (拉取、标记、推送) 都成功,则将镜像和标签的条目添加到记录文件。

### 记录文件

* 记录文件 (默认: `backed_up_images.txt`) 存储已成功备份的镜像和标签组合的列表。
* 文件中的每一行格式为: `镜像在Hub上的名称:标签`
* 示例: `nginx:1.25` 或 `prom/prometheus:v2.40.0`
* 该文件确保脚本不会重复处理已备份的镜像和标签,从而节省时间和资源。

### 错误处理

* 脚本使用 Python 的 `logging` 模块将所有操作 (包括错误) 记录到控制台。
* 如果缺少关键配置 (环境变量) 或初始登录到目标 Docker 仓库失败,脚本将立即退出。
* 对于在处理特定镜像或标签期间遇到的错误 (例如,拉取失败、推送失败),脚本将记录错误并尝试继续处理下一个标签或镜像 URL。

## 自动化每日备份工作流 (`.github/workflows/daily_image_backup.yml`)

该仓库包含一个 GitHub Actions 工作流,可自动每日执行 `main.py` 脚本以备份 Docker 镜像。

### 特性

- **定时执行:** 每天 UTC 时间 02:00 自动运行。
- **手动触发:** 也可以从 GitHub Actions 选项卡手动触发。
- **安全配置:** 使用 GitHub Actions Secrets 存储敏感信息,如仓库凭据。

### 工作流配置

要使用此工作流,您需要在您的 GitHub 仓库设置中配置以下 Secrets (`Settings > Secrets and variables > Actions > New repository secret`):

- **`TARGET_REGISTRY_URL_SECRET`**: 您的目标私有仓库的 URL (例如, `ccr.ccs.tencentyun.com` 或 `registry.aliyuncs.com`)。
- **`TARGET_NAMESPACE_SECRET`**: 您的目标私有仓库中用于存储镜像的命名空间 (例如, `my-docker-images` 或 `my-project`)。
- **`DOCKER_USERNAME_SECRET`**: 用于向您的目标私有仓库进行身份验证的用户名。
- **`DOCKER_PASSWORD_SECRET`**: 用于向您的目标私有仓库进行身份验证的密码。

### 工作原理

该工作流执行以下步骤:

1. **检出 (Checks out)** 仓库代码。
2. **设置 (Sets up)** Python 3.9 环境。
3. **安装 (Installs)** 所需的 Python 依赖 (`aiohttp`)。
4. **执行 (Executes)** `main.py` 脚本,并将配置的 Secrets作为环境变量传递给脚本。然后,脚本按照 "命令行脚本: `main.py`" 部分所述处理镜像备份逻辑。

脚本执行的日志可以在此工作流的 GitHub Actions 运行历史中查看。
74 changes: 74 additions & 0 deletions back_images.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
bitnami/kafka
jnovack/autossh
registry.k8s.io/pause:3.9
registry.k8s.io/coredns/coredns:v1.11.1
registry.k8s.io/etcd:3.5.10-0
registry.k8s.io/kube-proxy:v1.29.2
registry.k8s.io/kube-scheduler:v1.29.2
registry.k8s.io/kube-controller-manager:v1.29.2
registry.k8s.io/kube-apiserver:v1.29.2
docker/desktop-kubernetes:kubernetes-v1.29.2-cni-v1.4.0-critools-v1.29.0-cri-dockerd-v0.3.11-1-debian
nginx
gcr.io/k8s-minikube/kicbase:v0.0.44
alpine
busybox
httpd
mongo
ubuntu
node
postgres
memcached
golang
centos
php
mariadb
rabbitmq
elasticsearch
ruby
debian
tomcat:8
tomcat
jenkins
kibana
neo4j
fedora
sentry
solr
rethinkdb
zookeeper
redmine
tomee
ubuntu-debootstra
ros
rust
scratch
python:alpine3.19
mysql:5.7
mysql:8
redis
kibana:8.2.0
neo4j
fedora
sentry
solr
rethinkdb
zookeeper
redmine
tomee
certbot/certbot

eipwork/kuboard
gitlab/gitlab-ce:latest
gitlab/gitlab-runner:latest
certbot/certbot
ros
rust
eipwork/kuboard
gitlab/gitlab-ce:latest
gitlab/gitlab-runner:latest
certbot/certbot
grafana/grafana:11.2.0
prom/prometheus:v2.54.1
prom/node-exporter:v1.8.2
nginx/nginx-ingress:3.6.2
nginx/nginx-prometheus-exporter:1.3
8 changes: 3 additions & 5 deletions images.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
alpine
python:alpine3.19
kasmweb/nginx:1.25.3
--platform linux/arm64 cooderl/wewe-rss-sqlite:latest

rapidfort/etcd:3.5
rapidfort/etcd:3.5.12
rapidfort/etcd:3.5.9
Loading