Browse Source

备份目录:比如目录D:\sbFile\DRFID02004,上传后D:\sbFile\DRFID02004里的文件全部转移到D:\sbFile\DRFID02004_bak目录下,然后D:\sbFile\DRFID02004下文件全部删除

master
han\hanst 1 week ago
parent
commit
d475224e97
  1. 3
      client-file-collector/README.md
  2. 34
      client-file-collector/client_file_collector.py
  3. BIN
      client-file-collector/dist/QMSFileCollector.exe
  4. 2
      client-file-collector/dist/collector_config.json

3
client-file-collector/README.md

@ -26,7 +26,8 @@
- 每轮轮询会扫描每行配置的本地目录。 - 每轮轮询会扫描每行配置的本地目录。
- 仅同步新增或修改过的文件(避免重复上传同一文件)。 - 仅同步新增或修改过的文件(避免重复上传同一文件)。
- 不会删除本地文件。
- 单个文件上传成功后,会将该文件从本地目录转移至备份目录(源目录名 + `_bak`,例如 `D:\sbFile\DRFID02004` -> `D:\sbFile\DRFID02004_bak`),源目录中对应文件即被移除。
- 若备份目录中已存在同名文件,会自动在文件名后追加时间戳避免覆盖。
- Windows 下首次启动 `QMSFileCollector.exe` 时,会自动写入当前用户自启动项(优先写入 `Startup` 启动文件夹,失败时回退注册表 `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`)。 - Windows 下首次启动 `QMSFileCollector.exe` 时,会自动写入当前用户自启动项(优先写入 `Startup` 启动文件夹,失败时回退注册表 `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`)。
## 打包步骤 ## 打包步骤

34
client-file-collector/client_file_collector.py

@ -3,6 +3,7 @@
import json import json
import os import os
import queue import queue
import shutil
import sys import sys
import threading import threading
import time import time
@ -420,7 +421,10 @@ class CollectorApp:
continue continue
if self._upload_file(row, file_path): if self._upload_file(row, file_path):
self.upload_state[state_key] = signature
if self._backup_and_remove_file(row, file_path):
self.upload_state[state_key] = signature
else:
self.upload_state.pop(state_key, None)
def _file_signature(self, file_path): def _file_signature(self, file_path):
try: try:
@ -430,6 +434,34 @@ class CollectorApp:
self.log("读取文件状态失败: %s, 原因: %s" % (file_path, e)) self.log("读取文件状态失败: %s, 原因: %s" % (file_path, e))
return None return None
def _get_backup_dir(self, source_path):
source = Path(source_path)
return source.parent / (source.name + "_bak")
def _backup_and_remove_file(self, row, file_path):
source_path = row["source_path"]
row_index = row["row_index"]
backup_dir = self._get_backup_dir(source_path)
try:
backup_dir.mkdir(parents=True, exist_ok=True)
except Exception as e:
self.log("%d行创建备份目录失败: %s, 原因: %s" % (row_index, backup_dir, e))
return False
target = backup_dir / file_path.name
if target.exists():
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
target = backup_dir / ("%s_%s%s" % (file_path.stem, timestamp, file_path.suffix))
try:
shutil.move(str(file_path), str(target))
self.log("%d行源文件已转移至备份: %s -> %s" % (row_index, file_path.name, target))
return True
except Exception as e:
self.log("%d行源文件备份转移失败: %s, 原因: %s" % (row_index, file_path.name, e))
return False
def _upload_file(self, row, file_path): def _upload_file(self, row, file_path):
url = self._base_url() + "/collector/client/upload" url = self._base_url() + "/collector/client/upload"
data = { data = {

BIN
client-file-collector/dist/QMSFileCollector.exe

2
client-file-collector/dist/collector_config.json

@ -1,5 +1,5 @@
{ {
"base_url": "http://192.168.1.162:9000",
"base_url": "http://192.168.1.162:9097",
"poll_seconds": "10", "poll_seconds": "10",
"rows": [ "rows": [
{ {

Loading…
Cancel
Save