-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
31 lines (25 loc) · 862 Bytes
/
backup.py
File metadata and controls
31 lines (25 loc) · 862 Bytes
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
import os
import shutil
import json
from datetime import datetime
# Load config
with open("config.json", "r") as file:
config = json.load(file)
SOURCE = config["source_folder"]
BACKUP = config["backup_folder"]
LOG_FILE = "logs/backup_log.txt"
# Create backup folder with timestamp
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup_path = os.path.join(BACKUP, f"backup_{timestamp}")
try:
# Copy files
shutil.copytree(SOURCE, backup_path)
print(f"✅ Backup completed successfully! Files copied to: {backup_path}")
# Log success
with open(LOG_FILE, "a") as log:
log.write(f"[{datetime.now()}] SUCCESS: Backup created at {backup_path}\n")
except Exception as e:
print(f"❌ Backup failed: {e}")
# Log failure
with open(LOG_FILE, "a") as log:
log.write(f"[{datetime.now()}] ERROR: {str(e)}\n")