更新README文件
This commit is contained in:
205
README.md
205
README.md
@@ -345,8 +345,6 @@ print(vm.snapshot)
|
|||||||
|
|
||||||
## PY 文件作用
|
## PY 文件作用
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
``` powershell
|
``` powershell
|
||||||
PS D:\PycharmProjects\RemoveWeeklySnapshot> tree /F
|
PS D:\PycharmProjects\RemoveWeeklySnapshot> tree /F
|
||||||
卷 Date 的文件夹 PATH 列表
|
卷 Date 的文件夹 PATH 列表
|
||||||
@@ -412,22 +410,45 @@ sudo systemctl enable --now docker && systemctl status docker
|
|||||||
wget https://gitcode.junlan.site/junlan/RemoveWeeklyShapshot/archive/Dev.zip && unzip Dev.zip && cd removeweeklyshapshot/
|
wget https://gitcode.junlan.site/junlan/RemoveWeeklyShapshot/archive/Dev.zip && unzip Dev.zip && cd removeweeklyshapshot/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``` shell
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ tree
|
||||||
|
.
|
||||||
|
├── compose.yaml
|
||||||
|
├── config
|
||||||
|
│ ├── config.yaml
|
||||||
|
│ └── settings.py
|
||||||
|
├── core
|
||||||
|
│ ├── data_exporter.py
|
||||||
|
│ ├── get_vm_snapshots.py
|
||||||
|
│ ├── remove_snapshots.py
|
||||||
|
│ └── vm_connector.py
|
||||||
|
├── Dockerfile
|
||||||
|
├── main.py
|
||||||
|
├── README.md
|
||||||
|
├── requirements.txt
|
||||||
|
├── utils
|
||||||
|
│ └── logger.py
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 构建 Dockerfile 文件
|
### 构建 Dockerfile 文件
|
||||||
|
|
||||||
``` dockerfile
|
``` dockerfile
|
||||||
cat << 'EOF' > Dockerfile
|
cat << 'EOF' > Dockerfile
|
||||||
FROM python:3.14.3-slim
|
FROM python:3.14.3-slim
|
||||||
|
# 配置时区
|
||||||
|
ENV TZ=Asia/Shanghai
|
||||||
|
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
|
||||||
# 切换目录
|
# 切换目录
|
||||||
WORKDIR /removeweeklysnapshot/
|
WORKDIR /removeweeklysnapshot/
|
||||||
# 复制代码
|
# 复制代码
|
||||||
COPY ./ /removeweeklysnapshot/
|
COPY ./ /removeweeklysnapshot/
|
||||||
# 安装必要的软件和 python 库
|
# 安装必要的软件和 python 库
|
||||||
RUN apt-get update && apt-get install procps tzdata -y && pip install -r requirements.txt
|
RUN apt-get update && apt-get install procps tzdata -y && pip install -r requirements.txt && chmod +x /removeweeklysnapshot/main.py && mkdir output
|
||||||
# 配置时区
|
|
||||||
ENV TZ=Asia/Shanghai
|
|
||||||
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
|
|
||||||
# 容器内执行启动程序
|
# 容器内执行启动程序
|
||||||
CMD ["/removeweeklysnapshot/main.py"]
|
CMD ["python3", "/removeweeklysnapshot/main.py"]
|
||||||
EOF
|
EOF
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -440,18 +461,184 @@ sudo docker build --no-cache -t removeweeklysnapshot .
|
|||||||
### 构建 Compose 文件
|
### 构建 Compose 文件
|
||||||
|
|
||||||
``` yaml
|
``` yaml
|
||||||
|
cat << 'EOF' > compose.yaml
|
||||||
services:
|
services:
|
||||||
move_ix_file:
|
removeweeklysnapshot:
|
||||||
container_name: removeweeklysnapshot
|
container_name: removeweeklysnapshot
|
||||||
image: removeweeklysnapshot
|
image: removeweeklysnapshot
|
||||||
|
volumes:
|
||||||
|
- '/var/removeweeklysnapshot/logs:/removeweeklysnapshot/logs'
|
||||||
|
- '/var/removeweeklysnapshot/output:/removeweeklysnapshot/output'
|
||||||
|
# - '/var/removeweeklysnapshot/config:/removeweeklysnapshot/config'
|
||||||
restart: always
|
restart: always
|
||||||
stdin_open: true
|
stdin_open: true
|
||||||
tty: true
|
tty: true
|
||||||
|
EOF
|
||||||
```
|
```
|
||||||
|
|
||||||
### 运行容器
|
### 运行容器
|
||||||
|
|
||||||
``` shell
|
``` shell
|
||||||
docker compose up -d
|
sudo docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查看状态
|
||||||
|
|
||||||
|
``` shell
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ sudo docker images
|
||||||
|
|
||||||
|
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
|
||||||
|
python:3.14.3-slim 486b8092bfb1 176MB 45.5MB
|
||||||
|
removeweeklysnapshot:latest 6f17fcaaef99 512MB 140MB
|
||||||
|
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ sudo docker compose ps
|
||||||
|
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||||
|
removeweeklysnapshot removeweeklysnapshot "python3 /removeweek…" removeweeklysnapshot 34 seconds ago Up 15 seconds
|
||||||
|
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ sudo docker compose logs
|
||||||
|
removeweeklysnapshot | 2026-02-21 15:12:51,150 - INFO - ✅ 成功加载配置,共 2 个管理节点
|
||||||
|
removeweeklysnapshot | 2026-02-21 15:12:51,922 - INFO - 定时任务已设置:每周六凌晨4点导出文件,晚上7点删除快照
|
||||||
|
|
||||||
|
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ sudo docker exec -it removeweeklysnapshot bash
|
||||||
|
root@07c30da6408a:/removeweeklysnapshot# ps -ef
|
||||||
|
UID PID PPID C STIME TTY TIME CMD
|
||||||
|
root 1 0 0 15:12 pts/0 00:00:01 python3 /removeweeklysnapshot/main.py
|
||||||
|
root 11 0 0 15:15 pts/1 00:00:00 bash
|
||||||
|
root 17 11 0 15:15 pts/1 00:00:00 ps -ef
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 修改代码进行临时测试
|
||||||
|
|
||||||
|
> excel_output_path: /removeweeklysnapshot/output/vm_snapshots_report.xlsx # 可选,使用默认值,如:/logs/2026-02-20_14-00-21-VMsSnapShots_report.xlsx
|
||||||
|
> yaml_output_path: /removeweeklysnapshot/output/old_snapshots.yaml # 可选
|
||||||
|
>
|
||||||
|
> ```python
|
||||||
|
> # 每周六凌晨4点导出Excel和Yaml文件
|
||||||
|
> scheduler.add_job(export_files, 'cron', day_of_week='sat', hour=15, minute=43) # 修改时间进行测试
|
||||||
|
>
|
||||||
|
> # 每周六晚上7点执行删除快照任务
|
||||||
|
> scheduler.add_job(delete_old_snapshots, 'cron', day_of_week='sat', hour=15, minute=44) # 修改时间进行测试
|
||||||
|
> ```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
``` shell
|
||||||
|
root@07c30da6408a:/removeweeklysnapshot# cat << 'EOF' > config/config.yaml
|
||||||
|
management_nodes:
|
||||||
|
- type: esxi # 标记类型为esxi
|
||||||
|
name: esxi1
|
||||||
|
host: 192.168.40.133
|
||||||
|
user: root # ESXi默认用root
|
||||||
|
password: Root@2025
|
||||||
|
max_delete_concurrent: 2 # ESXi性能较弱,并发数可设小些
|
||||||
|
|
||||||
|
- type: esxi
|
||||||
|
name: esxi2
|
||||||
|
host: 192.168.40.135
|
||||||
|
user: root
|
||||||
|
password: Root@2025
|
||||||
|
max_delete_concurrent: 2
|
||||||
|
|
||||||
|
global:
|
||||||
|
disable_ssl_verify: true # ESXi连接特殊配置(禁用SSL验证,ESXi默认自签证书)
|
||||||
|
snapshot_retention_days: 0 # 可选,默认值 15 天
|
||||||
|
excel_output_path: /removeweeklysnapshot/output/vm_snapshots_report.xlsx # 可选,使用默认值,如:/logs/2026-02-20_14-00-21-VMsSnapShots_report.xlsx
|
||||||
|
yaml_output_path: /removeweeklysnapshot/output/old_snapshots.yaml # 可选
|
||||||
|
EOF
|
||||||
|
|
||||||
|
|
||||||
|
root@75ce49380cc0:/removeweeklysnapshot# cat << 'EOF' > main.py
|
||||||
|
import time
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
from utils.logger import logger
|
||||||
|
from config.settings import YAML_OUTPUT_PATH
|
||||||
|
from core.get_vm_snapshots import get_all_vms
|
||||||
|
from core.data_exporter import create_excel_report, export_yaml
|
||||||
|
from core.remove_snapshots import load_old_snapshots, remove_snapshot
|
||||||
|
|
||||||
|
|
||||||
|
def export_files():
|
||||||
|
"""导出Excel和Yaml文件的函数"""
|
||||||
|
logger.info("🔍 开始收集VM和快照信息...")
|
||||||
|
vms = get_all_vms() # 主函数入口,获取虚拟机信息
|
||||||
|
|
||||||
|
# 导出Excel报表
|
||||||
|
logger.info("📝 开始导出Excel报表...")
|
||||||
|
old_snapshots = create_excel_report(vms) # 生成Excel报告并获取旧快照
|
||||||
|
|
||||||
|
# 导出Yaml文件
|
||||||
|
logger.info("📝 开始导出 Yaml 文件...")
|
||||||
|
export_yaml(old_snapshots)
|
||||||
|
logger.info("========== Excel和Yaml文件导出完成 ==========")
|
||||||
|
|
||||||
|
|
||||||
|
def delete_old_snapshots():
|
||||||
|
"""删除旧快照的函数"""
|
||||||
|
logger.info("🗑️ 开始删除过旧快照...")
|
||||||
|
old_snapshots = load_old_snapshots(YAML_OUTPUT_PATH)
|
||||||
|
remove_snapshot(old_snapshots)
|
||||||
|
logger.info("========== VM快照清理任务执行完成 ==========")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""主执行函数"""
|
||||||
|
|
||||||
|
# 设置定时任务
|
||||||
|
scheduler = BackgroundScheduler()
|
||||||
|
|
||||||
|
# 每周六凌晨4点导出Excel和Yaml文件
|
||||||
|
scheduler.add_job(export_files, 'cron', day_of_week='sat', hour=15, minute=43) # 修改时间进行测试
|
||||||
|
|
||||||
|
# 每周六晚上7点执行删除快照任务
|
||||||
|
scheduler.add_job(delete_old_snapshots, 'cron', day_of_week='sat', hour=15, minute=44) # 修改时间进行测试
|
||||||
|
|
||||||
|
scheduler.start()
|
||||||
|
|
||||||
|
logger.info("定时任务已设置:每周六凌晨4点导出文件,晚上7点删除快照")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 保持主程序运行,以便调度器能正常工作
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
scheduler.shutdown()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 进入容器进行测试
|
||||||
|
[junlan@localhost removeweeklyshapshot]$ sudo docker exec -it removeweeklysnapshot bash
|
||||||
|
root@75ce49380cc0:/removeweeklysnapshot# python main.py
|
||||||
|
2026-02-21 15:42:56,070 - INFO - ✅ 成功加载配置,共 2 个管理节点
|
||||||
|
2026-02-21 15:42:56,556 - INFO - 定时任务已设置:每周六凌晨4点导出文件,晚上7点删除快照
|
||||||
|
2026-02-21 15:43:00,000 - INFO - 🔍 开始收集VM和快照信息...
|
||||||
|
2026-02-21 15:43:00,054 - INFO - 成功连接到节点: 192.168.40.133
|
||||||
|
2026-02-21 15:43:03,319 - ERROR - 处理节点 192.168.40.135 失败:[Errno 113] No route to host
|
||||||
|
2026-02-21 15:43:03,319 - INFO - 获取到 2 台虚拟机
|
||||||
|
2026-02-21 15:43:03,319 - INFO - 📝 开始导出Excel报表...
|
||||||
|
2026-02-21 15:43:03,322 - INFO - 总共有 3 个快照
|
||||||
|
2026-02-21 15:43:03,340 - DEBUG - Excel 文件已生成: /removeweeklysnapshot/output/vm_snapshots_report.xlsx
|
||||||
|
2026-02-21 15:43:03,340 - INFO - 📝 开始导出 Yaml 文件...
|
||||||
|
2026-02-21 15:43:03,340 - INFO - 可删除的快照有 3 个
|
||||||
|
2026-02-21 15:43:03,342 - DEBUG - YAML 文件已生成: /removeweeklysnapshot/output/old_snapshots.yaml
|
||||||
|
2026-02-21 15:43:03,342 - INFO - ========== Excel和Yaml文件导出完成 ==========
|
||||||
|
2026-02-21 15:44:00,001 - INFO - 🗑️ 开始删除过旧快照...
|
||||||
|
2026-02-21 15:44:00,004 - INFO - 连接 192.168.40.133 进行删除快照...
|
||||||
|
2026-02-21 15:44:00,050 - INFO - 成功连接到节点: 192.168.40.133
|
||||||
|
2026-02-21 15:44:00,081 - INFO - 正在删除 Snapshot: VMware vCenter Server Appliance-快照测试-(1-snapshot-7)
|
||||||
|
2026-02-21 15:44:01,100 - INFO - ✅ 删除成功: VMware vCenter Server Appliance-快照测试-(1-snapshot-7)
|
||||||
|
2026-02-21 15:44:01,125 - INFO - 正在删除 Snapshot: VMware vCenter Server Appliance-快照测试-第二层快照-(1-snapshot-8)
|
||||||
|
2026-02-21 15:44:02,139 - INFO - ✅ 删除成功: VMware vCenter Server Appliance-快照测试-第二层快照-(1-snapshot-8)
|
||||||
|
2026-02-21 15:44:02,164 - INFO - 正在删除 Snapshot: test-vm-01-snap-01-(4-snapshot-9)
|
||||||
|
2026-02-21 15:44:03,178 - INFO - ✅ 删除成功: test-vm-01-snap-01-(4-snapshot-9)
|
||||||
|
2026-02-21 15:44:03,184 - INFO - ========== VM快照清理任务执行完成 ==========
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user