Compare commits
No commits in common. "7cd6eb88211f48e41861bd52e089296ff49fe945" and "3eca803f0810fd25f15355dc286986f69352f65c" have entirely different histories.
7cd6eb8821
...
3eca803f08
@ -1,34 +0,0 @@
|
|||||||
from datetime import datetime
|
|
||||||
import requests
|
|
||||||
import os
|
|
||||||
|
|
||||||
import config
|
|
||||||
|
|
||||||
|
|
||||||
def raiseWebhook(message, file=None):
|
|
||||||
data = {
|
|
||||||
"username": "Backup script",
|
|
||||||
"embeds":
|
|
||||||
[{
|
|
||||||
"title": "Erreur lors de la sauvegarde",
|
|
||||||
"description": message,
|
|
||||||
"color": color_red,
|
|
||||||
"fields": []
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
||||||
if file is not None:
|
|
||||||
data[files]={"file": open(file, "rb")}
|
|
||||||
|
|
||||||
requests.post(config.ERROR_WEBHOOK, json=data)
|
|
||||||
|
|
||||||
with open("latest-success", "r") as f:
|
|
||||||
latest_success = f.read().strip()
|
|
||||||
|
|
||||||
today = datetime.today().strftime('%Y-%m-%d')
|
|
||||||
if latest_success != today:
|
|
||||||
log_file = None
|
|
||||||
if os.path.exists(f"logs/{today}.txt"):
|
|
||||||
log_file = f"logs/{today}.txt"
|
|
||||||
|
|
||||||
raiseWebhook(f"No successful backup today", file=log_file)
|
|
@ -8,5 +8,3 @@ REMOTE_USER = "myuser"
|
|||||||
BACKUP_MAP = [
|
BACKUP_MAP = [
|
||||||
("/home/remote/MyData", "backup-it-here") # myuser@192.168.1.5:/home/remote/MyData will be backed up to /mnt/{backup-date and "latest"}/backup-it-here
|
("/home/remote/MyData", "backup-it-here") # myuser@192.168.1.5:/home/remote/MyData will be backed up to /mnt/{backup-date and "latest"}/backup-it-here
|
||||||
]
|
]
|
||||||
|
|
||||||
ERROR_WEBHOOK = "https://discord.com/api/webhooks/...."
|
|
79
src/main.py
79
src/main.py
@ -1,7 +1,6 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import subprocess
|
import subprocess
|
||||||
import requests
|
import requests
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import config
|
import config
|
||||||
@ -10,19 +9,9 @@ def log(*args, **kwargs):
|
|||||||
date = datetime.now().strftime('%d-%b-%Y %H:%M:%S')
|
date = datetime.now().strftime('%d-%b-%Y %H:%M:%S')
|
||||||
print(f"[{date}]", *args, **kwargs)
|
print(f"[{date}]", *args, **kwargs)
|
||||||
|
|
||||||
def log_error(*args, **kwargs):
|
|
||||||
date = datetime.now().strftime('%d-%b-%Y %H:%M:%S')
|
|
||||||
print(f"[{date}]", *args, **kwargs, file=sys.stderr)
|
|
||||||
|
|
||||||
def check_return_code(ret, *args, **kwargs):
|
|
||||||
if ret != 0:
|
|
||||||
log_error(*args, **kwargs)
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
if not os.path.exists(config.DISK):
|
if not os.path.exists(config.DISK):
|
||||||
log_error(f"Drive {config.DISK} not present on the system")
|
log_error(f"Drive {config.DISK} not present on the system")
|
||||||
exit(1)
|
|
||||||
|
|
||||||
passwd = requests.get(config.PWD_URL).text.strip()
|
passwd = requests.get(config.PWD_URL).text.strip()
|
||||||
log("Got password :", passwd)
|
log("Got password :", passwd)
|
||||||
@ -31,46 +20,26 @@ ssh_key_passwd = requests.get(config.SSH_KEY_PWD_URL).text.strip()
|
|||||||
log("Got password :", ssh_key_passwd)
|
log("Got password :", ssh_key_passwd)
|
||||||
|
|
||||||
|
|
||||||
class Drive():
|
subprocess.run([
|
||||||
def __init__(self, disk, passwd, mount_location):
|
|
||||||
self.disk = disk
|
|
||||||
self.passwd = passwd
|
|
||||||
self.mount_location = mount_location
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
subprocess.run([
|
|
||||||
"cryptsetup", "luksOpen",
|
"cryptsetup", "luksOpen",
|
||||||
self.disk, "backup"
|
config.DISK, "backup"
|
||||||
], input=self.passwd, text=True)
|
], input=passwd, text=True)
|
||||||
|
|
||||||
subprocess.call([
|
subprocess.call([
|
||||||
"mount", "/dev/mapper/backup",
|
"mount", "/dev/mapper/backup",
|
||||||
self.mount_location
|
config.MNT_LOCATION
|
||||||
])
|
])
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, exception_type, exception_value, exception_traceback):
|
log("Successfully mounted, doing the backup stuff")
|
||||||
subprocess.call([
|
|
||||||
"umount", self.mount_location
|
|
||||||
])
|
|
||||||
|
|
||||||
subprocess.call([
|
os.makedirs(
|
||||||
"cryptsetup", "luksClose",
|
|
||||||
"/dev/mapper/backup"
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
with Drive(config.DISK, passwd, config.MNT_LOCATION) as backup_drive:
|
|
||||||
log("Successfully mounted, doing the backup stuff")
|
|
||||||
|
|
||||||
os.makedirs(
|
|
||||||
os.path.join(config.MNT_LOCATION, "latest"),
|
os.path.join(config.MNT_LOCATION, "latest"),
|
||||||
exist_ok=True
|
exist_ok=True
|
||||||
)
|
)
|
||||||
|
|
||||||
for src, dest in config.BACKUP_MAP:
|
for src, dest in config.BACKUP_MAP:
|
||||||
# We could also use a rsync daemon on the remote machine for pwd(-less) auth
|
# We could also use a rsync daemon on the remote machine for pwd(-less) auth
|
||||||
ret = subprocess.call(
|
subprocess.call(
|
||||||
[
|
[
|
||||||
"sshpass", "-P", "passphrase", "-e",
|
"sshpass", "-P", "passphrase", "-e",
|
||||||
"rsync",
|
"rsync",
|
||||||
@ -83,25 +52,29 @@ with Drive(config.DISK, passwd, config.MNT_LOCATION) as backup_drive:
|
|||||||
],
|
],
|
||||||
env={**os.environ, "SSHPASS":ssh_key_passwd}
|
env={**os.environ, "SSHPASS":ssh_key_passwd}
|
||||||
)
|
)
|
||||||
check_return_code(ret, f"Error when syncing {config.REMOTE_LOCATION}")
|
|
||||||
|
|
||||||
log("Rsync ok. Creating snapshot.")
|
log("Rsync ok. Creating snapshot.")
|
||||||
|
|
||||||
ret = subprocess.call([
|
subprocess.call([
|
||||||
"cp", "-a",
|
"cp", "-a",
|
||||||
"--reflink=always", #! The partition must be Btrfs or XFS
|
"--reflink=always", #! The partition must be Btrfs or XFS
|
||||||
os.path.join(config.MNT_LOCATION, "latest"),
|
os.path.join(config.MNT_LOCATION, "latest"),
|
||||||
os.path.join(config.MNT_LOCATION, datetime.today().strftime('%Y-%m-%d'))
|
os.path.join(config.MNT_LOCATION, datetime.today().strftime('%Y-%m-%d'))
|
||||||
])
|
])
|
||||||
check_return_code(ret, f"Error when creating snapshot")
|
|
||||||
|
|
||||||
log("Snapshot ok. Deleting unneeded backups.")
|
log("Snapshot ok. Deleting unneeded backups.")
|
||||||
|
|
||||||
#TODO
|
#TODO
|
||||||
|
|
||||||
log("Unmounting.")
|
log("Unmounting.")
|
||||||
|
|
||||||
log("Everything okay, done !")
|
subprocess.call([
|
||||||
|
"umount", config.MNT_LOCATION
|
||||||
|
])
|
||||||
|
|
||||||
with open("latest-success", "w") as f:
|
subprocess.call([
|
||||||
f.write(datetime.today().strftime('%Y-%m-%d'))
|
"cryptsetup", "luksClose",
|
||||||
|
"/dev/mapper/backup"
|
||||||
|
])
|
||||||
|
|
||||||
|
log("Done !")
|
Loading…
x
Reference in New Issue
Block a user