-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crontab
142 lines (111 loc) · 4.65 KB
/
Crontab
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
#!/usr/bin/env python3
import subprocess
# Define cloud credentials
cloud_username = "lndbackup"
cloud_password = "cloudpassword"
# Create ~/.curl-config file with credentials
curl_config_content = f"-u {cloud_username}:{cloud_password}"
try:
with open("/home/humble/.curl-config", "w") as file:
file.write(curl_config_content)
print("Created ~/.curl-config file.")
except Exception as e:
print(f"Failed to create ~/.curl-config file: {str(e)}")
# Set the file permissions to 600
try:
subprocess.run("chmod 600 /home/humble/.curl-config", shell=True, check=True)
print("Set ~/.curl-config file permissions to 600.")
except subprocess.CalledProcessError as e:
print(f"Failed to set file permissions with error code {e.returncode}.")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
# Set up crontab for tar and curl commands
cron_commands = [
'*/1 * * * * tar -czf /tmp/lndbackup.tar.gz -C /home/humble/.lnd/ .',
'*/1 * * * * curl --config /home/humble/.curl-config -T /tmp/lndbackup.tar.gz http://{ipaddress}/remote.php/dav/files/lndbackup/lndbackup/'
]
# Join the commands and set up crontab
cron_content = '\n'.join(cron_commands)
try:
subprocess.run(f'(crontab -l ; echo "{cron_content}") | crontab -', shell=True, check=True)
print("Crontab successfully updated.")
except subprocess.CalledProcessError as e:
print(f"Failed to update crontab with error code {e.returncode}.")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
# Input variables
channel_backup_path = "/home/humble/.lnd/data/chain/bitcoin/mainnet/channel.backup"
ipaddress = "cloud_ipaddress"
nextcloud_username = "lndbackup"
nextcloud_password = "cloudpassword"
# Script content
script_content = f"""#!/bin/bash
# Set the path to the channel.backup file
channel_backup_path="{channel_backup_path}"
# Set the URL of the Nextcloud WebDAV endpoint
nextcloud_webdav_url="http://{ipaddress}/remote.php/dav/files/{nextcloud_username}/lndbackup/"
# Nextcloud username and password
nextcloud_username="{nextcloud_username}"
nextcloud_password="{nextcloud_password}"
# Watch for changes to the channel.backup file
while true; do
inotifywait "$channel_backup_path"
# If the file has changed and exists, copy it to the Nextcloud backup directory
if [[ -f "$channel_backup_path" ]]; then
curl -u "$nextcloud_username:$nextcloud_password" -T "$channel_backup_path" "$nextcloud_webdav_url"
fi
done
"""
# Save the script content to the hidden file
hidden_script_path = "/home/humble/.copy-channel-backup-on-change.sh"
with open(hidden_script_path, "w") as script_file:
script_file.write(script_content)
def make_script_executable(script_path):
# Use the chmod command to make the script executable
subprocess.run(f"chmod +x {script_path}", shell=True, check=True)
if __name__ == "__main__":
script_path = "/home/humble/.copy-channel-backup-on-change.sh" # Replace with the actual path to your script
make_script_executable(script_path)
import os
import subprocess
def create_lndbackup_service(system_username):
content = f'''[Unit]
Description=Copy Channel Backup on Change
[Service]
ExecStart=/home/humble/.copy-channel-backup-on-change.sh
Restart=always
RestartSec=1
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=backup-channels
User={system_username}
Group={system_username}
[Install]
WantedBy=multi-user.target
'''
# Define the file_path variable
file_path = "/etc/systemd/system/lndbackup.service"
# Write the content to a temporary file
temp_file_path = "/tmp/lndbackup.service"
with open(temp_file_path, 'w') as file:
file.write(content)
# Use sudo to move the temporary file to the target directory
move_command = ["sudo", "mv", temp_file_path, file_path]
subprocess.run(move_command, check=True)
# Set the correct ownership and permissions for the service file
chown_command = ["sudo", "chown", "root:root", file_path]
subprocess.run(chown_command, check=True)
chmod_command = ["sudo", "chmod", "644", file_path]
subprocess.run(chmod_command, check=True)
# Reload systemd configuration
reload_command = ["sudo", "systemctl", "daemon-reload"]
subprocess.run(reload_command, check=True)
# Enable the lndbackup service
enable_command = ["sudo", "systemctl", "enable", "lndbackup.service"]
subprocess.run(enable_command, check=True)
# Start the lndbackup service
start_command = ["sudo", "systemctl", "start", "lndbackup.service"]
subprocess.run(start_command, check=True)
if __name__ == "__main__":
system_username = "humble" # Replace with the actual system username
create_lndbackup_service(system_username)