-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePersephoneBackup.py
138 lines (118 loc) · 4.67 KB
/
createPersephoneBackup.py
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
#!/usr/bin/env python3
"""
createPersephoneBackup.py
This script performs a Restic backup by prompting for required configuration
values—including AWS credentials—storing those values in a config file
(.persephone_backup.conf) for future runs, and then executing the Restic commands.
"""
import os
import subprocess
import getpass
CONFIG_FILE = ".persephone_backup.conf"
def load_config(config_file):
"""
Loads configuration from the config file if it exists.
Expected file format (one key="value" per line):
REPO_FILE="s3:https://s3api.cybermonkey.dev/restic"
PASS_FILE="/root/.restic-password"
BACKUP_PATHS_STR="/root /home /var /etc /srv /usr /opt"
AWS_ACCESS_KEY_ID="..."
AWS_SECRET_ACCESS_KEY="..."
Returns a dictionary with the configuration values.
"""
config = {}
if os.path.exists(config_file):
with open(config_file, "r") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
key, value = line.split("=", 1)
key = key.strip()
# Remove surrounding quotes if present.
value = value.strip().strip('"').strip("'")
config[key] = value
return config
def prompt_input(prompt_message, default_val=None, hidden=False):
"""
Prompts the user for input with an optional default.
If hidden=True, the input will be hidden (useful for sensitive data).
"""
if default_val:
prompt = f"{prompt_message} [{default_val}]: "
else:
prompt = f"{prompt_message}: "
while True:
if hidden:
response = getpass.getpass(prompt).strip()
else:
response = input(prompt).strip()
if response == "" and default_val is not None:
return default_val
elif response != "":
return response
else:
print("Error: Input cannot be empty. Please enter a valid value.")
def save_config(config_file, config):
"""
Saves the configuration dictionary to the config file.
"""
with open(config_file, "w") as f:
for key, value in config.items():
f.write(f'{key}="{value}"\n')\
def main():
# Load configuration if available.
config = load_config(CONFIG_FILE)
# Get default values from the config or use sensible defaults.
default_repo = config.get("REPO_FILE", "s3:https://s3api.cybermonkey.dev/restic")
default_pass_file = config.get("PASS_FILE", "/root/.restic-password")
default_backup_paths = config.get("BACKUP_PATHS_STR", "/root /home /var /etc /srv /usr /opt")
# AWS credentials defaults.
default_aws_access_key = config.get("AWS_ACCESS_KEY_ID", "")
default_aws_secret_key = config.get("AWS_SECRET_ACCESS_KEY", "")
print("=== Restic Backup Configuration ===")
repo_file = prompt_input("Enter the restic repository file path", default_repo)
pass_file = prompt_input("Enter the restic password file path", default_pass_file)
backup_paths_str = prompt_input("Enter backup paths (space-separated)", default_backup_paths)
print("\n=== AWS Credentials ===")
aws_access_key = prompt_input("Enter AWS_ACCESS_KEY_ID", default_aws_access_key)
aws_secret_key = prompt_input("Enter AWS_SECRET_ACCESS_KEY", default_aws_secret_key, hidden=True)
# Update configuration dictionary.
config["REPO_FILE"] = repo_file
config["PASS_FILE"] = pass_file
config["BACKUP_PATHS_STR"] = backup_paths_str
config["AWS_ACCESS_KEY_ID"] = aws_access_key
config["AWS_SECRET_ACCESS_KEY"] = aws_secret_key
# Save the configuration for future runs.
save_config(CONFIG_FILE, config)
# Convert the backup paths string to a list.
backup_paths = backup_paths_str.split()
# Prepare environment variables for subprocess.
env = os.environ.copy()
env["AWS_ACCESS_KEY_ID"] = aws_access_key
env["AWS_SECRET_ACCESS_KEY"] = aws_secret_key
# Run the backup command.
print("\nRunning Restic backup...")
backup_cmd = [
"sudo",
"restic",
"--repository-file", repo_file,
"--password-file", pass_file,
"--verbose",
"backup",
] + backup_paths
subprocess.run(backup_cmd, check=True, env=env)
# Check snapshots.
print("Backup completed. Checking snapshots...")
snapshots_cmd = [
"sudo",
"restic",
"--repository-file", repo_file,
"--password-file", pass_file,
"snapshots",
]
subprocess.run(snapshots_cmd, check=True, env=env)
print("Restic backup and snapshot check complete.")
if __name__ == "__main__":
main()