-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_restore.py
More file actions
executable file
·379 lines (293 loc) · 11.5 KB
/
backup_restore.py
File metadata and controls
executable file
·379 lines (293 loc) · 11.5 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
"""
EFIO Edge Controller - Backup/Restore Utility
Backs up and restores configuration, users, and device data
"""
import os
import sys
import json
import shutil
import tarfile
import argparse
from datetime import datetime
from pathlib import Path
# Configuration
CONFIG_DIR = Path.home() / "efio"
BACKUP_DIR = Path.home() / "efio_backups"
DEFAULT_BACKUP_NAME = f"efio_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.tar.gz"
# Files to backup
BACKUP_FILES = [
"users.json",
"network_config.json",
"io_config.json",
"alarm_config.json",
"modbus_devices.json",
"modbus_log.json",
"pairing.json"
]
class Colors:
"""ANSI color codes"""
GREEN = '\033[0;32m'
BLUE = '\033[0;34m'
YELLOW = '\033[1;33m'
RED = '\033[0;31m'
NC = '\033[0m' # No Color
def print_success(msg):
print(f"{Colors.GREEN}✓ {msg}{Colors.NC}")
def print_info(msg):
print(f"{Colors.BLUE}ℹ {msg}{Colors.NC}")
def print_warning(msg):
print(f"{Colors.YELLOW}⚠ {msg}{Colors.NC}")
def print_error(msg):
print(f"{Colors.RED}✗ {msg}{Colors.NC}")
def print_header(msg):
print(f"\n{Colors.BLUE}{'='*50}{Colors.NC}")
print(f"{Colors.BLUE}{msg}{Colors.NC}")
print(f"{Colors.BLUE}{'='*50}{Colors.NC}\n")
def ensure_dirs():
"""Ensure config and backup directories exist"""
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
BACKUP_DIR.mkdir(parents=True, exist_ok=True)
def list_config_files():
"""List all configuration files"""
files = []
for filename in BACKUP_FILES:
filepath = CONFIG_DIR / filename
if filepath.exists():
size = filepath.stat().st_size
mtime = datetime.fromtimestamp(filepath.stat().st_mtime)
files.append({
'name': filename,
'path': filepath,
'size': size,
'modified': mtime
})
return files
def create_backup(output_path=None, include_logs=False):
"""Create backup archive of configuration files"""
print_header("Creating Backup")
ensure_dirs()
# Determine output path
if output_path is None:
output_path = BACKUP_DIR / DEFAULT_BACKUP_NAME
else:
output_path = Path(output_path)
# Get files to backup
files = list_config_files()
if not files:
print_warning("No configuration files found to backup")
return False
print_info(f"Found {len(files)} configuration files")
# Create backup archive
try:
with tarfile.open(output_path, "w:gz") as tar:
for file_info in files:
# Skip logs unless requested
if 'log' in file_info['name'].lower() and not include_logs:
print_info(f"Skipping log file: {file_info['name']}")
continue
print_info(f"Adding: {file_info['name']} ({file_info['size']} bytes)")
tar.add(file_info['path'], arcname=file_info['name'])
# Add metadata
metadata = {
'created': datetime.now().isoformat(),
'hostname': os.uname().nodename,
'version': '1.0.0',
'files': [f['name'] for f in files]
}
# Create metadata file
metadata_path = CONFIG_DIR / "backup_metadata.json"
with open(metadata_path, 'w') as f:
json.dump(metadata, f, indent=2)
tar.add(metadata_path, arcname="backup_metadata.json")
# Clean up metadata file
metadata_path.unlink()
# Get backup size
backup_size = output_path.stat().st_size
print_success(f"Backup created: {output_path}")
print_info(f"Backup size: {backup_size / 1024:.1f} KB")
return True
except Exception as e:
print_error(f"Backup failed: {e}")
return False
def restore_backup(backup_path, force=False):
"""Restore configuration from backup archive"""
print_header("Restoring Backup")
ensure_dirs()
backup_path = Path(backup_path)
if not backup_path.exists():
print_error(f"Backup file not found: {backup_path}")
return False
print_info(f"Backup file: {backup_path}")
# Extract and validate backup
try:
with tarfile.open(backup_path, "r:gz") as tar:
members = tar.getmembers()
print_info(f"Backup contains {len(members)} files")
# Check for metadata
metadata = None
for member in members:
if member.name == "backup_metadata.json":
f = tar.extractfile(member)
metadata = json.load(f)
break
if metadata:
print_info(f"Backup created: {metadata['created']}")
print_info(f"From host: {metadata['hostname']}")
# Confirm restoration
if not force:
print_warning("This will overwrite existing configuration files!")
response = input("Continue with restore? (yes/no): ").strip().lower()
if response not in ['yes', 'y']:
print_info("Restore cancelled")
return False
# Create backup of current config before restore
current_backup = BACKUP_DIR / f"pre_restore_{datetime.now().strftime('%Y%m%d_%H%M%S')}.tar.gz"
print_info(f"Creating safety backup: {current_backup}")
create_backup(current_backup, include_logs=False)
# Extract files
print_info("Extracting files...")
for member in members:
if member.name == "backup_metadata.json":
continue
print_info(f"Restoring: {member.name}")
tar.extract(member, path=CONFIG_DIR)
print_success("Backup restored successfully")
print_warning("You may need to restart the EFIO service for changes to take effect")
print_info("Restart command: sudo systemctl restart efio-api")
return True
except Exception as e:
print_error(f"Restore failed: {e}")
return False
def list_backups():
"""List all available backups"""
print_header("Available Backups")
ensure_dirs()
backups = sorted(BACKUP_DIR.glob("*.tar.gz"), key=lambda p: p.stat().st_mtime, reverse=True)
if not backups:
print_info("No backups found")
return []
print(f"{'Name':<40} {'Size':>10} {'Created':<20}")
print("-" * 72)
for backup in backups:
size = backup.stat().st_size / 1024 # KB
mtime = datetime.fromtimestamp(backup.stat().st_mtime)
print(f"{backup.name:<40} {size:>8.1f} KB {mtime.strftime('%Y-%m-%d %H:%M:%S')}")
return backups
def show_config_status():
"""Show current configuration status"""
print_header("Configuration Status")
ensure_dirs()
files = list_config_files()
if not files:
print_warning("No configuration files found")
return
print(f"{'File':<30} {'Size':>10} {'Last Modified':<20}")
print("-" * 62)
for file_info in files:
size = file_info['size']
mtime = file_info['modified'].strftime('%Y-%m-%d %H:%M:%S')
print(f"{file_info['name']:<30} {size:>8} B {mtime}")
print(f"\nTotal files: {len(files)}")
print(f"Config directory: {CONFIG_DIR}")
def export_config_json(output_path=None):
"""Export all configuration as a single JSON file"""
print_header("Exporting Configuration")
ensure_dirs()
if output_path is None:
output_path = f"efio_config_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
config = {
'metadata': {
'exported': datetime.now().isoformat(),
'hostname': os.uname().nodename,
'version': '1.0.0'
},
'configuration': {}
}
# Load all config files
for filename in BACKUP_FILES:
filepath = CONFIG_DIR / filename
if filepath.exists():
try:
with open(filepath, 'r') as f:
config['configuration'][filename] = json.load(f)
print_info(f"Loaded: {filename}")
except Exception as e:
print_warning(f"Could not load {filename}: {e}")
# Save combined config
try:
with open(output_path, 'w') as f:
json.dump(config, f, indent=2)
file_size = Path(output_path).stat().st_size
print_success(f"Configuration exported: {output_path}")
print_info(f"File size: {file_size / 1024:.1f} KB")
return True
except Exception as e:
print_error(f"Export failed: {e}")
return False
def main():
parser = argparse.ArgumentParser(
description='EFIO Configuration Backup/Restore Utility',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Create backup
./backup_restore.py backup
# Create backup with logs
./backup_restore.py backup --include-logs
# Restore from backup
./backup_restore.py restore efio_backup_20241223_120000.tar.gz
# List available backups
./backup_restore.py list
# Show current configuration status
./backup_restore.py status
# Export configuration as JSON
./backup_restore.py export
"""
)
subparsers = parser.add_subparsers(dest='command', help='Command to execute')
# Backup command
backup_parser = subparsers.add_parser('backup', help='Create configuration backup')
backup_parser.add_argument('-o', '--output', help='Output path for backup file')
backup_parser.add_argument('--include-logs', action='store_true', help='Include log files in backup')
# Restore command
restore_parser = subparsers.add_parser('restore', help='Restore configuration from backup')
restore_parser.add_argument('backup_file', help='Path to backup file')
restore_parser.add_argument('-f', '--force', action='store_true', help='Skip confirmation prompt')
# List command
list_parser = subparsers.add_parser('list', help='List available backups')
# Status command
status_parser = subparsers.add_parser('status', help='Show configuration status')
# Export command
export_parser = subparsers.add_parser('export', help='Export configuration as JSON')
export_parser.add_argument('-o', '--output', help='Output path for JSON file')
args = parser.parse_args()
if args.command is None:
parser.print_help()
return 1
# Execute command
if args.command == 'backup':
success = create_backup(args.output, args.include_logs)
return 0 if success else 1
elif args.command == 'restore':
success = restore_backup(args.backup_file, args.force)
return 0 if success else 1
elif args.command == 'list':
list_backups()
return 0
elif args.command == 'status':
show_config_status()
return 0
elif args.command == 'export':
success = export_config_json(args.output)
return 0 if success else 1
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
print("\n\nInterrupted by user")
sys.exit(1)
except Exception as e:
print_error(f"Unexpected error: {e}")
sys.exit(1)