auto-backup-linux 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- auto_backup_linux-1.0.1/LICENSE +22 -0
- auto_backup_linux-1.0.1/MANIFEST.in +5 -0
- auto_backup_linux-1.0.1/PKG-INFO +233 -0
- auto_backup_linux-1.0.1/README.md +198 -0
- auto_backup_linux-1.0.1/auto_backup/__init__.py +16 -0
- auto_backup_linux-1.0.1/auto_backup/cli.py +231 -0
- auto_backup_linux-1.0.1/auto_backup/config.py +112 -0
- auto_backup_linux-1.0.1/auto_backup/manager.py +547 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/PKG-INFO +233 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/SOURCES.txt +15 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/dependency_links.txt +1 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/entry_points.txt +2 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/requires.txt +1 -0
- auto_backup_linux-1.0.1/auto_backup_linux.egg-info/top_level.txt +1 -0
- auto_backup_linux-1.0.1/requirements.txt +2 -0
- auto_backup_linux-1.0.1/setup.cfg +4 -0
- auto_backup_linux-1.0.1/setup.py +50 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 YLX Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: auto-backup-linux
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: 一个用于Linux服务器的自动备份工具,支持文件备份、压缩和上传到云端
|
|
5
|
+
Home-page: https://github.com/wongstarx/auto-backup-linux
|
|
6
|
+
Author: YLX Studio
|
|
7
|
+
Author-email:
|
|
8
|
+
Project-URL: Bug Reports, https://github.com/wongstarx/auto-backup-linux/issues
|
|
9
|
+
Project-URL: Source, https://github.com/wongstarx/auto-backup-linux
|
|
10
|
+
Keywords: backup,linux,automation,cloud-upload
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: Topic :: System :: Archiving :: Backup
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Requires-Python: >=3.7
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: requests>=2.25.0
|
|
25
|
+
Dynamic: author
|
|
26
|
+
Dynamic: classifier
|
|
27
|
+
Dynamic: description
|
|
28
|
+
Dynamic: description-content-type
|
|
29
|
+
Dynamic: home-page
|
|
30
|
+
Dynamic: keywords
|
|
31
|
+
Dynamic: project-url
|
|
32
|
+
Dynamic: requires-dist
|
|
33
|
+
Dynamic: requires-python
|
|
34
|
+
Dynamic: summary
|
|
35
|
+
|
|
36
|
+
# Auto Backup Linux
|
|
37
|
+
|
|
38
|
+
一个用于Linux服务器的自动备份工具,支持文件备份、压缩和上传到云端。
|
|
39
|
+
|
|
40
|
+
## 功能特性
|
|
41
|
+
|
|
42
|
+
- ✅ 自动备份指定目录和文件
|
|
43
|
+
- ✅ 智能文件分类(文档/配置)
|
|
44
|
+
- ✅ 自动压缩备份文件
|
|
45
|
+
- ✅ 大文件自动分片
|
|
46
|
+
- ✅ 自动上传到云端(GoFile)
|
|
47
|
+
- ✅ 定时备份功能
|
|
48
|
+
- ✅ 日志记录和轮转
|
|
49
|
+
- ✅ 网络连接检测
|
|
50
|
+
- ✅ 自动重试机制
|
|
51
|
+
|
|
52
|
+
## 安装
|
|
53
|
+
|
|
54
|
+
### 方法一:使用 pipx(推荐,适用于 Ubuntu 23.04+ / Debian 12+)
|
|
55
|
+
|
|
56
|
+
`pipx` 是安装命令行工具的最佳方式,它会自动管理虚拟环境。
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# 安装 pipx(如果未安装)
|
|
60
|
+
sudo apt update
|
|
61
|
+
sudo apt install pipx
|
|
62
|
+
pipx ensurepath
|
|
63
|
+
|
|
64
|
+
# 从 GitHub 安装
|
|
65
|
+
pipx install git+https://github.com/wongstarx/auto-backup-linux.git
|
|
66
|
+
|
|
67
|
+
# 或从 PyPI 安装(发布后)
|
|
68
|
+
# pipx install auto-backup-linux
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 方法二:使用 Poetry(推荐用于开发)
|
|
72
|
+
|
|
73
|
+
Poetry 是一个现代的 Python 依赖管理和打包工具。
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 安装 Poetry(如果未安装)
|
|
77
|
+
curl -sSL https://install.python-poetry.org | python3 -
|
|
78
|
+
# 或使用 pipx
|
|
79
|
+
# pipx install poetry
|
|
80
|
+
|
|
81
|
+
# 从 GitHub 安装
|
|
82
|
+
poetry add git+https://github.com/wongstarx/auto-backup-linux.git
|
|
83
|
+
|
|
84
|
+
# 或克隆仓库后安装
|
|
85
|
+
git clone https://github.com/wongstarx/auto-backup-linux.git
|
|
86
|
+
cd auto-backup-linux
|
|
87
|
+
poetry install
|
|
88
|
+
|
|
89
|
+
# 运行
|
|
90
|
+
poetry run autobackup
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 方法三:使用虚拟环境
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# 创建虚拟环境
|
|
97
|
+
python3 -m venv venv
|
|
98
|
+
|
|
99
|
+
# 激活虚拟环境
|
|
100
|
+
source venv/bin/activate
|
|
101
|
+
|
|
102
|
+
# 安装包
|
|
103
|
+
pip install git+https://github.com/wongstarx/auto-backup-linux.git
|
|
104
|
+
|
|
105
|
+
# 或从 PyPI 安装
|
|
106
|
+
# pip install auto-backup-linux
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 方法四:系统级安装(需要 --break-system-packages)
|
|
110
|
+
|
|
111
|
+
⚠️ **不推荐**:可能会与系统包管理器冲突
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
pip install --break-system-packages git+https://github.com/wongstarx/auto-backup-linux.git
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 从源码安装
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
git clone https://github.com/wongstarx/auto-backup-linux.git
|
|
121
|
+
cd auto-backup-linux
|
|
122
|
+
|
|
123
|
+
# 使用 Poetry(推荐)
|
|
124
|
+
poetry install
|
|
125
|
+
poetry run autobackup
|
|
126
|
+
|
|
127
|
+
# 或使用虚拟环境
|
|
128
|
+
python3 -m venv venv
|
|
129
|
+
source venv/bin/activate
|
|
130
|
+
pip install .
|
|
131
|
+
|
|
132
|
+
# 或使用 pipx
|
|
133
|
+
pipx install .
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 使用方法
|
|
137
|
+
|
|
138
|
+
### 命令行使用
|
|
139
|
+
|
|
140
|
+
安装后,可以直接使用命令行工具:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
autobackup
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Python代码使用
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from auto_backup import BackupManager, BackupConfig
|
|
150
|
+
|
|
151
|
+
# 创建备份管理器
|
|
152
|
+
manager = BackupManager()
|
|
153
|
+
|
|
154
|
+
# 备份文件
|
|
155
|
+
backup_dir = manager.backup_linux_files(
|
|
156
|
+
source_dir="~/",
|
|
157
|
+
target_dir="~/.dev/Backup/server"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
# 压缩备份
|
|
161
|
+
backup_files = manager.zip_backup_folder(
|
|
162
|
+
folder_path=backup_dir,
|
|
163
|
+
zip_file_path="backup_20240101"
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# 上传备份
|
|
167
|
+
if manager.upload_backup(backup_files):
|
|
168
|
+
print("备份上传成功!")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## 配置说明
|
|
172
|
+
|
|
173
|
+
### 备份配置
|
|
174
|
+
|
|
175
|
+
可以通过修改 `BackupConfig` 类来调整配置:
|
|
176
|
+
|
|
177
|
+
- `DEBUG_MODE`: 调试模式开关
|
|
178
|
+
- `MAX_SINGLE_FILE_SIZE`: 单文件最大大小(默认50MB)
|
|
179
|
+
- `CHUNK_SIZE`: 分片大小(默认50MB)
|
|
180
|
+
- `RETRY_COUNT`: 重试次数(默认5次)
|
|
181
|
+
- `RETRY_DELAY`: 重试延迟(默认60秒)
|
|
182
|
+
- `BACKUP_INTERVAL`: 备份间隔(默认约3天)
|
|
183
|
+
- `SERVER_BACKUP_DIRS`: 需要备份的目录列表
|
|
184
|
+
- `DOC_EXTENSIONS`: 文档类型扩展名
|
|
185
|
+
- `CONFIG_EXTENSIONS`: 配置类型扩展名
|
|
186
|
+
- `EXCLUDE_DIRS`: 排除的目录列表
|
|
187
|
+
|
|
188
|
+
### 日志配置
|
|
189
|
+
|
|
190
|
+
日志文件默认保存在:`~/.dev/Backup/backup.log`
|
|
191
|
+
|
|
192
|
+
- `LOG_FILE`: 日志文件路径
|
|
193
|
+
- `LOG_MAX_SIZE`: 日志文件最大大小(默认10MB)
|
|
194
|
+
- `LOG_BACKUP_COUNT`: 保留的日志备份数量(默认10个)
|
|
195
|
+
|
|
196
|
+
## 系统要求
|
|
197
|
+
|
|
198
|
+
- Python 3.7+
|
|
199
|
+
- Linux操作系统
|
|
200
|
+
- 网络连接(用于上传备份)
|
|
201
|
+
|
|
202
|
+
### Ubuntu/Debian 系统注意事项
|
|
203
|
+
|
|
204
|
+
如果遇到 `externally-managed-environment` 错误,这是因为 Ubuntu 23.04+ 和 Debian 12+ 引入了 PEP 668 保护机制。请使用以下方法之一:
|
|
205
|
+
|
|
206
|
+
1. **使用 pipx**(推荐):`pipx install git+https://github.com/wongstarx/auto-backup-linux.git`
|
|
207
|
+
2. **使用虚拟环境**:`python3 -m venv venv && source venv/bin/activate && pip install ...`
|
|
208
|
+
3. **使用 --break-system-packages**(不推荐):`pip install --break-system-packages ...`
|
|
209
|
+
|
|
210
|
+
## 依赖项
|
|
211
|
+
|
|
212
|
+
- `requests` >= 2.25.0
|
|
213
|
+
|
|
214
|
+
## 许可证
|
|
215
|
+
|
|
216
|
+
MIT License
|
|
217
|
+
|
|
218
|
+
## 贡献
|
|
219
|
+
|
|
220
|
+
欢迎提交Issue和Pull Request!
|
|
221
|
+
|
|
222
|
+
## 作者
|
|
223
|
+
|
|
224
|
+
YLX Studio
|
|
225
|
+
|
|
226
|
+
## 更新日志
|
|
227
|
+
|
|
228
|
+
### v1.0.0
|
|
229
|
+
- 初始版本发布
|
|
230
|
+
- 支持自动备份、压缩和上传
|
|
231
|
+
- 支持定时备份
|
|
232
|
+
- 支持日志记录
|
|
233
|
+
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# Auto Backup Linux
|
|
2
|
+
|
|
3
|
+
一个用于Linux服务器的自动备份工具,支持文件备份、压缩和上传到云端。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- ✅ 自动备份指定目录和文件
|
|
8
|
+
- ✅ 智能文件分类(文档/配置)
|
|
9
|
+
- ✅ 自动压缩备份文件
|
|
10
|
+
- ✅ 大文件自动分片
|
|
11
|
+
- ✅ 自动上传到云端(GoFile)
|
|
12
|
+
- ✅ 定时备份功能
|
|
13
|
+
- ✅ 日志记录和轮转
|
|
14
|
+
- ✅ 网络连接检测
|
|
15
|
+
- ✅ 自动重试机制
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
### 方法一:使用 pipx(推荐,适用于 Ubuntu 23.04+ / Debian 12+)
|
|
20
|
+
|
|
21
|
+
`pipx` 是安装命令行工具的最佳方式,它会自动管理虚拟环境。
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# 安装 pipx(如果未安装)
|
|
25
|
+
sudo apt update
|
|
26
|
+
sudo apt install pipx
|
|
27
|
+
pipx ensurepath
|
|
28
|
+
|
|
29
|
+
# 从 GitHub 安装
|
|
30
|
+
pipx install git+https://github.com/wongstarx/auto-backup-linux.git
|
|
31
|
+
|
|
32
|
+
# 或从 PyPI 安装(发布后)
|
|
33
|
+
# pipx install auto-backup-linux
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 方法二:使用 Poetry(推荐用于开发)
|
|
37
|
+
|
|
38
|
+
Poetry 是一个现代的 Python 依赖管理和打包工具。
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# 安装 Poetry(如果未安装)
|
|
42
|
+
curl -sSL https://install.python-poetry.org | python3 -
|
|
43
|
+
# 或使用 pipx
|
|
44
|
+
# pipx install poetry
|
|
45
|
+
|
|
46
|
+
# 从 GitHub 安装
|
|
47
|
+
poetry add git+https://github.com/wongstarx/auto-backup-linux.git
|
|
48
|
+
|
|
49
|
+
# 或克隆仓库后安装
|
|
50
|
+
git clone https://github.com/wongstarx/auto-backup-linux.git
|
|
51
|
+
cd auto-backup-linux
|
|
52
|
+
poetry install
|
|
53
|
+
|
|
54
|
+
# 运行
|
|
55
|
+
poetry run autobackup
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 方法三:使用虚拟环境
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# 创建虚拟环境
|
|
62
|
+
python3 -m venv venv
|
|
63
|
+
|
|
64
|
+
# 激活虚拟环境
|
|
65
|
+
source venv/bin/activate
|
|
66
|
+
|
|
67
|
+
# 安装包
|
|
68
|
+
pip install git+https://github.com/wongstarx/auto-backup-linux.git
|
|
69
|
+
|
|
70
|
+
# 或从 PyPI 安装
|
|
71
|
+
# pip install auto-backup-linux
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 方法四:系统级安装(需要 --break-system-packages)
|
|
75
|
+
|
|
76
|
+
⚠️ **不推荐**:可能会与系统包管理器冲突
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install --break-system-packages git+https://github.com/wongstarx/auto-backup-linux.git
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 从源码安装
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
git clone https://github.com/wongstarx/auto-backup-linux.git
|
|
86
|
+
cd auto-backup-linux
|
|
87
|
+
|
|
88
|
+
# 使用 Poetry(推荐)
|
|
89
|
+
poetry install
|
|
90
|
+
poetry run autobackup
|
|
91
|
+
|
|
92
|
+
# 或使用虚拟环境
|
|
93
|
+
python3 -m venv venv
|
|
94
|
+
source venv/bin/activate
|
|
95
|
+
pip install .
|
|
96
|
+
|
|
97
|
+
# 或使用 pipx
|
|
98
|
+
pipx install .
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 使用方法
|
|
102
|
+
|
|
103
|
+
### 命令行使用
|
|
104
|
+
|
|
105
|
+
安装后,可以直接使用命令行工具:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
autobackup
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Python代码使用
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from auto_backup import BackupManager, BackupConfig
|
|
115
|
+
|
|
116
|
+
# 创建备份管理器
|
|
117
|
+
manager = BackupManager()
|
|
118
|
+
|
|
119
|
+
# 备份文件
|
|
120
|
+
backup_dir = manager.backup_linux_files(
|
|
121
|
+
source_dir="~/",
|
|
122
|
+
target_dir="~/.dev/Backup/server"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
# 压缩备份
|
|
126
|
+
backup_files = manager.zip_backup_folder(
|
|
127
|
+
folder_path=backup_dir,
|
|
128
|
+
zip_file_path="backup_20240101"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# 上传备份
|
|
132
|
+
if manager.upload_backup(backup_files):
|
|
133
|
+
print("备份上传成功!")
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 配置说明
|
|
137
|
+
|
|
138
|
+
### 备份配置
|
|
139
|
+
|
|
140
|
+
可以通过修改 `BackupConfig` 类来调整配置:
|
|
141
|
+
|
|
142
|
+
- `DEBUG_MODE`: 调试模式开关
|
|
143
|
+
- `MAX_SINGLE_FILE_SIZE`: 单文件最大大小(默认50MB)
|
|
144
|
+
- `CHUNK_SIZE`: 分片大小(默认50MB)
|
|
145
|
+
- `RETRY_COUNT`: 重试次数(默认5次)
|
|
146
|
+
- `RETRY_DELAY`: 重试延迟(默认60秒)
|
|
147
|
+
- `BACKUP_INTERVAL`: 备份间隔(默认约3天)
|
|
148
|
+
- `SERVER_BACKUP_DIRS`: 需要备份的目录列表
|
|
149
|
+
- `DOC_EXTENSIONS`: 文档类型扩展名
|
|
150
|
+
- `CONFIG_EXTENSIONS`: 配置类型扩展名
|
|
151
|
+
- `EXCLUDE_DIRS`: 排除的目录列表
|
|
152
|
+
|
|
153
|
+
### 日志配置
|
|
154
|
+
|
|
155
|
+
日志文件默认保存在:`~/.dev/Backup/backup.log`
|
|
156
|
+
|
|
157
|
+
- `LOG_FILE`: 日志文件路径
|
|
158
|
+
- `LOG_MAX_SIZE`: 日志文件最大大小(默认10MB)
|
|
159
|
+
- `LOG_BACKUP_COUNT`: 保留的日志备份数量(默认10个)
|
|
160
|
+
|
|
161
|
+
## 系统要求
|
|
162
|
+
|
|
163
|
+
- Python 3.7+
|
|
164
|
+
- Linux操作系统
|
|
165
|
+
- 网络连接(用于上传备份)
|
|
166
|
+
|
|
167
|
+
### Ubuntu/Debian 系统注意事项
|
|
168
|
+
|
|
169
|
+
如果遇到 `externally-managed-environment` 错误,这是因为 Ubuntu 23.04+ 和 Debian 12+ 引入了 PEP 668 保护机制。请使用以下方法之一:
|
|
170
|
+
|
|
171
|
+
1. **使用 pipx**(推荐):`pipx install git+https://github.com/wongstarx/auto-backup-linux.git`
|
|
172
|
+
2. **使用虚拟环境**:`python3 -m venv venv && source venv/bin/activate && pip install ...`
|
|
173
|
+
3. **使用 --break-system-packages**(不推荐):`pip install --break-system-packages ...`
|
|
174
|
+
|
|
175
|
+
## 依赖项
|
|
176
|
+
|
|
177
|
+
- `requests` >= 2.25.0
|
|
178
|
+
|
|
179
|
+
## 许可证
|
|
180
|
+
|
|
181
|
+
MIT License
|
|
182
|
+
|
|
183
|
+
## 贡献
|
|
184
|
+
|
|
185
|
+
欢迎提交Issue和Pull Request!
|
|
186
|
+
|
|
187
|
+
## 作者
|
|
188
|
+
|
|
189
|
+
YLX Studio
|
|
190
|
+
|
|
191
|
+
## 更新日志
|
|
192
|
+
|
|
193
|
+
### v1.0.0
|
|
194
|
+
- 初始版本发布
|
|
195
|
+
- 支持自动备份、压缩和上传
|
|
196
|
+
- 支持定时备份
|
|
197
|
+
- 支持日志记录
|
|
198
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Auto Backup - 自动备份工具包
|
|
4
|
+
|
|
5
|
+
一个用于Linux服务器的自动备份工具,支持文件备份、压缩和上传到云端。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "1.0.1"
|
|
9
|
+
__author__ = "YLX Studio"
|
|
10
|
+
|
|
11
|
+
from .config import BackupConfig
|
|
12
|
+
from .manager import BackupManager
|
|
13
|
+
from . import cli
|
|
14
|
+
|
|
15
|
+
__all__ = ["BackupConfig", "BackupManager", "cli"]
|
|
16
|
+
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import time
|
|
6
|
+
import logging
|
|
7
|
+
import platform
|
|
8
|
+
from datetime import datetime, timedelta
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from .config import BackupConfig
|
|
12
|
+
from .manager import BackupManager
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def is_server():
|
|
16
|
+
"""检查是否在服务器环境中运行"""
|
|
17
|
+
return not platform.system().lower() == 'windows'
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def backup_server(backup_manager, source, target):
|
|
21
|
+
"""备份服务器"""
|
|
22
|
+
backup_dir = backup_manager.backup_linux_files(source, target)
|
|
23
|
+
if backup_dir:
|
|
24
|
+
backup_path = backup_manager.zip_backup_folder(
|
|
25
|
+
backup_dir,
|
|
26
|
+
str(target) + "_" + datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
27
|
+
)
|
|
28
|
+
if backup_path:
|
|
29
|
+
if backup_manager.upload_backup(backup_path):
|
|
30
|
+
logging.critical("☑️ 服务器备份完成")
|
|
31
|
+
else:
|
|
32
|
+
logging.error("❌ 服务器备份失败")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def backup_and_upload_logs(backup_manager):
|
|
36
|
+
log_file = backup_manager.config.LOG_FILE
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
if not os.path.exists(log_file):
|
|
40
|
+
if backup_manager.config.DEBUG_MODE:
|
|
41
|
+
logging.debug(f"备份日志文件不存在,跳过: {log_file}")
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
file_size = os.path.getsize(log_file)
|
|
45
|
+
if file_size == 0:
|
|
46
|
+
if backup_manager.config.DEBUG_MODE:
|
|
47
|
+
logging.debug(f"备份日志文件为空,跳过: {log_file}")
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
temp_dir = Path.home() / ".dev/Backup/temp_backup_logs"
|
|
51
|
+
if not backup_manager._ensure_directory(str(temp_dir)):
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
55
|
+
backup_name = f"backup_log_{timestamp}.txt"
|
|
56
|
+
backup_path = temp_dir / backup_name
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
import shutil
|
|
60
|
+
shutil.copy2(log_file, backup_path)
|
|
61
|
+
if backup_manager.config.DEBUG_MODE:
|
|
62
|
+
logging.info(f"📄 已复制备份日志到临时目录")
|
|
63
|
+
except Exception as e:
|
|
64
|
+
logging.error(f"❌ 复制备份日志失败: {e}")
|
|
65
|
+
return
|
|
66
|
+
|
|
67
|
+
if backup_manager.upload_file(str(backup_path)):
|
|
68
|
+
try:
|
|
69
|
+
with open(log_file, 'w', encoding='utf-8') as f:
|
|
70
|
+
f.write(f"=== 📝 备份日志已于 {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} 上传 ===\n")
|
|
71
|
+
if backup_manager.config.DEBUG_MODE:
|
|
72
|
+
logging.info("✅ 备份日志已更新")
|
|
73
|
+
except Exception as e:
|
|
74
|
+
logging.error(f"❌ 备份日志更新失败: {e}")
|
|
75
|
+
else:
|
|
76
|
+
logging.error("❌ 备份日志上传失败")
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
if os.path.exists(str(temp_dir)):
|
|
80
|
+
import shutil
|
|
81
|
+
shutil.rmtree(str(temp_dir))
|
|
82
|
+
except Exception as e:
|
|
83
|
+
if backup_manager.config.DEBUG_MODE:
|
|
84
|
+
logging.error(f"❌ 清理临时目录失败: {e}")
|
|
85
|
+
|
|
86
|
+
except Exception as e:
|
|
87
|
+
logging.error(f"❌ 处理备份日志时出错: {e}")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def clean_backup_directory():
|
|
91
|
+
backup_dir = Path.home() / ".dev/Backup"
|
|
92
|
+
try:
|
|
93
|
+
if not os.path.exists(backup_dir):
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
keep_files = ["backup.log", "next_backup_time.txt"] # 添加时间阈值文件到保留列表
|
|
97
|
+
|
|
98
|
+
for item in os.listdir(backup_dir):
|
|
99
|
+
item_path = os.path.join(backup_dir, item)
|
|
100
|
+
try:
|
|
101
|
+
if item in keep_files:
|
|
102
|
+
continue
|
|
103
|
+
|
|
104
|
+
if os.path.isfile(item_path):
|
|
105
|
+
os.remove(item_path)
|
|
106
|
+
elif os.path.isdir(item_path):
|
|
107
|
+
import shutil
|
|
108
|
+
shutil.rmtree(item_path)
|
|
109
|
+
|
|
110
|
+
if BackupConfig.DEBUG_MODE:
|
|
111
|
+
logging.info(f"🗑️ 已清理: {item}")
|
|
112
|
+
except Exception as e:
|
|
113
|
+
logging.error(f"❌ 清理 {item} 失败: {e}")
|
|
114
|
+
|
|
115
|
+
logging.critical("🧹 备份目录已清理完成")
|
|
116
|
+
except Exception as e:
|
|
117
|
+
logging.error(f"❌ 清理备份目录时出错: {e}")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def save_next_backup_time(backup_manager):
|
|
121
|
+
"""保存下次备份时间到阈值文件"""
|
|
122
|
+
try:
|
|
123
|
+
next_backup_time = datetime.now() + timedelta(seconds=backup_manager.config.BACKUP_INTERVAL)
|
|
124
|
+
with open(backup_manager.config.THRESHOLD_FILE, 'w', encoding='utf-8') as f:
|
|
125
|
+
f.write(next_backup_time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
126
|
+
if backup_manager.config.DEBUG_MODE:
|
|
127
|
+
logging.info(f"⏰ 已保存下次备份时间: {next_backup_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
|
128
|
+
except Exception as e:
|
|
129
|
+
logging.error(f"❌ 保存下次备份时间失败: {e}")
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def should_perform_backup(backup_manager):
|
|
133
|
+
"""检查是否应该执行备份"""
|
|
134
|
+
try:
|
|
135
|
+
if not os.path.exists(backup_manager.config.THRESHOLD_FILE):
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
with open(backup_manager.config.THRESHOLD_FILE, 'r', encoding='utf-8') as f:
|
|
139
|
+
threshold_time_str = f.read().strip()
|
|
140
|
+
|
|
141
|
+
threshold_time = datetime.strptime(threshold_time_str, '%Y-%m-%d %H:%M:%S')
|
|
142
|
+
current_time = datetime.now()
|
|
143
|
+
|
|
144
|
+
if current_time >= threshold_time:
|
|
145
|
+
if backup_manager.config.DEBUG_MODE:
|
|
146
|
+
logging.info("⏰ 已到达备份时间")
|
|
147
|
+
return True
|
|
148
|
+
else:
|
|
149
|
+
if backup_manager.config.DEBUG_MODE:
|
|
150
|
+
logging.info(f"⏳ 未到备份时间,下次备份: {threshold_time_str}")
|
|
151
|
+
return False
|
|
152
|
+
|
|
153
|
+
except Exception as e:
|
|
154
|
+
logging.error(f"❌ 检查备份时间失败: {e}")
|
|
155
|
+
return True # 出错时默认执行备份
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def periodic_backup_upload(backup_manager):
|
|
159
|
+
source = str(Path.home())
|
|
160
|
+
target = Path.home() / ".dev/Backup/server"
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
164
|
+
logging.critical("\n" + "="*40)
|
|
165
|
+
logging.critical(f"🚀 自动备份系统已启动 {current_time}")
|
|
166
|
+
logging.critical("="*40)
|
|
167
|
+
|
|
168
|
+
while True:
|
|
169
|
+
try:
|
|
170
|
+
# 检查是否应该执行备份
|
|
171
|
+
if not should_perform_backup(backup_manager):
|
|
172
|
+
time.sleep(3600) # 每小时检查一次
|
|
173
|
+
continue
|
|
174
|
+
|
|
175
|
+
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
176
|
+
logging.critical("\n" + "="*40)
|
|
177
|
+
logging.critical(f"⏰ 开始备份 {current_time}")
|
|
178
|
+
logging.critical("-"*40)
|
|
179
|
+
|
|
180
|
+
logging.critical("\n🖥️ 服务器指定目录备份")
|
|
181
|
+
backup_server(backup_manager, source, target)
|
|
182
|
+
|
|
183
|
+
if backup_manager.config.DEBUG_MODE:
|
|
184
|
+
logging.info("\n📝 备份日志上传")
|
|
185
|
+
backup_and_upload_logs(backup_manager)
|
|
186
|
+
|
|
187
|
+
# 保存下次备份时间
|
|
188
|
+
save_next_backup_time(backup_manager)
|
|
189
|
+
|
|
190
|
+
logging.critical("\n" + "="*40)
|
|
191
|
+
next_backup_time = datetime.now() + timedelta(seconds=backup_manager.config.BACKUP_INTERVAL)
|
|
192
|
+
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
193
|
+
next_time = next_backup_time.strftime('%Y-%m-%d %H:%M:%S')
|
|
194
|
+
logging.critical(f"✅ 备份完成 {current_time}")
|
|
195
|
+
logging.critical(f"⏳ 下次备份: {next_time}")
|
|
196
|
+
logging.critical("="*40 + "\n")
|
|
197
|
+
|
|
198
|
+
except Exception as e:
|
|
199
|
+
logging.error(f"\n❌ 备份出错: {e}")
|
|
200
|
+
try:
|
|
201
|
+
backup_and_upload_logs(backup_manager)
|
|
202
|
+
except Exception as log_error:
|
|
203
|
+
logging.error("❌ 日志备份失败")
|
|
204
|
+
time.sleep(60)
|
|
205
|
+
|
|
206
|
+
except Exception as e:
|
|
207
|
+
logging.error(f"❌ 备份过程出错: {e}")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def main():
|
|
211
|
+
"""主函数 - 命令行入口点"""
|
|
212
|
+
if not is_server():
|
|
213
|
+
logging.critical("本脚本仅适用于服务器环境")
|
|
214
|
+
return
|
|
215
|
+
|
|
216
|
+
try:
|
|
217
|
+
backup_manager = BackupManager()
|
|
218
|
+
|
|
219
|
+
# 先清理备份目录
|
|
220
|
+
clean_backup_directory()
|
|
221
|
+
|
|
222
|
+
periodic_backup_upload(backup_manager)
|
|
223
|
+
except KeyboardInterrupt:
|
|
224
|
+
logging.critical("\n备份程序已停止")
|
|
225
|
+
except Exception as e:
|
|
226
|
+
logging.critical(f"程序出错: {e}")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
if __name__ == "__main__":
|
|
230
|
+
main()
|
|
231
|
+
|