commit5c22600d98Merge:bc12ca7bcdbb4aAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Tue Nov 29 16:54:28 2022 +0100 Merge pull request #462 from Spitfireap/randomize-api-call-time randomize api call time commitbcdbb4a2ceAuthor: Spitap <dev@asdrip.fr> Date: Tue Nov 29 14:53:05 2022 +0100 fix typo commitbd1ddcef21Author: Spitap <dev@asdrip.fr> Date: Tue Nov 29 13:45:31 2022 +0100 randomize api call time commitbc12ca7327Merge:d364239bd0ecd0Author: Antoine Nguyen <tonio@ngyn.org> Date: Mon Nov 14 15:49:41 2022 +0100 Merge pull request #458 from Spitfireap/fix-include_try fix typo in dovecot configuration file commitbd0ecd0949Author: Spitap <dev@asdrip.fr> Date: Thu Nov 10 14:57:43 2022 +0100 fix typo in dovecot configuration file commitd364239348Merge:61838db3763300Author: Antoine Nguyen <tonio@ngyn.org> Date: Wed Nov 9 10:51:30 2022 +0100 Merge pull request #456 from modoboa/feature/improved_backup_restore WIP: Improved backup/restore system. commit37633008cbAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Wed Nov 9 10:30:44 2022 +0100 Fixed restore mode commitd6f9a5b913Author: Antoine Nguyen <tonio@ngyn.org> Date: Tue Nov 8 17:20:25 2022 +0100 Few fixes. commit8b1d60ee59Author: Antoine Nguyen <tonio@ngyn.org> Date: Tue Nov 8 17:19:23 2022 +0100 Few fixes commit2b5edae5d5Author: Antoine Nguyen <tonio@ngyn.org> Date: Sun Nov 6 10:30:24 2022 +0100 WIP: Improved backup/restore system. commit61838dbe4dAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Sat Nov 5 09:30:50 2022 +0100 Check if restore is defined before doing anything else. fix #453 commit962cac3ad9Merge:1b192c5ef2359aAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Fri Nov 4 09:41:20 2022 +0100 Merge pull request #450 from Spitfireap/fixed-super-call fixed super call in modoboa's script commitef2359a2a8Author: Spitap <dev@asdrip.fr> Date: Thu Nov 3 23:10:21 2022 +0100 fixed super call commit1b192c5fd5Merge:754d652b0b0146Author: Antoine Nguyen <tonio@ngyn.org> Date: Thu Nov 3 15:34:48 2022 +0100 Merge pull request #449 from Spitfireap/fixed-import-typo fixed constants import commitb0b01465d9Author: Spitap <dev@asdrip.fr> Date: Thu Nov 3 15:00:07 2022 +0100 fixed constants import commit754d652fc2Author: Antoine Nguyen <tonio@ngyn.org> Date: Thu Nov 3 12:27:04 2022 +0100 Few fixes commitcb5fa75693Merge:1afb8e6e01265aAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Thu Nov 3 12:20:25 2022 +0100 Merge pull request #444 from Spitfireap/tighter-config-file-perm tighter config file permission commit1afb8e61fcMerge:15c17798dd0b7dAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Thu Nov 3 12:17:16 2022 +0100 Merge pull request #424 from Spitfireap/restore Backup & restore system commit8dd0b7d497Author: Spitap <dev@asdrip.fr> Date: Thu Nov 3 10:57:03 2022 +0100 Last camelCase commit554611b366Author: Spitap <dev@asdrip.fr> Date: Thu Nov 3 10:54:06 2022 +0100 review fix commit15c17796f2Merge:ce8e7e684d1363Author: Antoine Nguyen <tonio@ngyn.org> Date: Fri Oct 28 09:43:30 2022 +0200 Merge pull request #446 from Spitfireap/fix-ssl-min-protocol fixed ssl_min_protocol setting commit84d13633a1Author: Spitap <dev@asdrip.fr> Date: Thu Oct 27 22:37:47 2022 +0200 fixed ssl_min_protocol setting commitce8e7e6027Merge:8e8ae5ffe7df27Author: Antoine Nguyen <tonio@ngyn.org> Date: Thu Oct 27 17:56:37 2022 +0200 Merge pull request #445 from Spitfireap/dovecot-fixes Fixes ssl permission error, updated ssl_protocol parameter commite01265a4eeMerge:a5fba03235ef3bAuthor: Spitap <dev@asdrip.fr> Date: Thu Oct 27 17:44:37 2022 +0200 Merge branch 'tighter-config-file-perm' of https://github.com/Spitfireap/modoboa-installer into tighter-config-file-perm commita5fba03264Author: Spitap <dev@asdrip.fr> Date: Thu Oct 27 11:13:47 2022 +0200 tighter config file permission commitfe7df276fcAuthor: Spitap <dev@asdrip.fr> Date: Thu Oct 27 17:25:39 2022 +0200 Check dovecot version greater commit8f34f0af6fAuthor: Spitap <dev@asdrip.fr> Date: Thu Oct 27 17:00:58 2022 +0200 Fixes ssl permission error, updated ssl_protocol parameter commit8e8ae5fb9cMerge:67f6ceefefbf54Author: Antoine Nguyen <tonio@ngyn.org> Date: Thu Oct 27 16:49:20 2022 +0200 Merge pull request #439 from stefaweb/master Update config_dict_template.py for default max_servers value commit235ef3befbAuthor: Spitap <dev@asdrip.fr> Date: Thu Oct 27 11:13:47 2022 +0200 thighter config file permission commit67f6cee8eaMerge:b84abbb53f7f8eAuthor: Antoine Nguyen <tonio@ngyn.org> Date: Tue Oct 25 19:32:37 2022 +0200 Merge pull request #442 from Spitfireap/patch-1 Set $max_server to 2 to avoid amavis crash commit5c9d5c9a03Author: Spitap <dev@asdrip.fr> Date: Tue Oct 25 16:58:57 2022 +0200 DKIM keys restore, Radicale backup/restore, fixes commit4c1f8710b5Author: Spitap <dev@asdrip.fr> Date: Tue Oct 25 16:04:55 2022 +0200 Added dkim key backup commite34eb4b337Author: Spitap <dev@asdrip.fr> Date: Tue Oct 25 13:59:28 2022 +0200 fix database path commit53f7f8ef9dAuthor: Spitfireap <45575529+Spitfireap@users.noreply.github.com> Date: Wed Oct 19 08:19:40 2022 +0000 Update config_dict_template.py commit35778cd614Merge:6726f5bb84abbbAuthor: Spitfireap <45575529+Spitfireap@users.noreply.github.com> Date: Tue Oct 18 17:17:48 2022 +0200 Merge branch 'modoboa:master' into restore commitfefbf549a4Author: Stephane Leclerc <sleclerc@actionweb.fr> Date: Thu Oct 6 13:36:13 2022 +0200 Update config_dict_template.py for default max_server value commit6726f5b1a2Author: Spitap <dev@asdrip.fr> Date: Mon Sep 26 13:39:28 2022 +0200 Improved path generation, path mistake proofing commita192cbcbd0Author: Spitap <dev@asdrip.fr> Date: Mon Sep 19 16:40:25 2022 +0200 Updated doc, default path on conf file commit5bed9655eaAuthor: Spitap <dev@asdrip.fr> Date: Mon Sep 19 15:53:19 2022 +0200 fixed typo commit6b096a7470Author: Spitap <dev@asdrip.fr> Date: Mon Sep 19 15:50:03 2022 +0200 Simplified db dumps restore commite30add03fdMerge:d75d83f1f8dd1bAuthor: Spitap <dev@asdrip.fr> Date: Mon Sep 19 15:39:05 2022 +0200 Update from master commitd75d83f202Author: Spitap <dev@asdrip.fr> Date: Mon Sep 19 15:13:44 2022 +0200 more refactoring commitf3811b4b39Author: Spitap <dev@asdrip.fr> Date: Mon Sep 19 14:59:43 2022 +0200 refactoring commitb0d56b3989Author: Spitap <dev@asdrip.fr> Date: Thu Sep 15 11:32:57 2022 +0200 PEP formating commit53e3e3ec58Author: Spitap <dev@asdrip.fr> Date: Fri Aug 5 15:20:11 2022 +0200 Better UX, use of os to concatenate path commite546d2cb23Author: Spitap <dev@asdrip.fr> Date: Wed Jul 27 16:32:59 2022 +0200 Better UX commit70faa1c5cbAuthor: Spitap <dev@asdrip.fr> Date: Wed Jul 27 15:58:41 2022 +0200 Fixed backupdir index commit563979a7ddAuthor: Spitap <dev@asdrip.fr> Date: Wed Jul 27 15:51:22 2022 +0200 fixed mail backup/restore commitee2ccf0647Author: Spitap <dev@asdrip.fr> Date: Wed Jul 27 14:35:48 2022 +0200 Fixed postfix install, added restore to readme commit2077c94b52Author: Spitap <dev@asdrip.fr> Date: Tue Jul 26 17:05:00 2022 +0200 Fix amavis config file not copied to right location commit4a7222bd24Author: Spitap <dev@asdrip.fr> Date: Tue Jul 26 16:53:24 2022 +0200 Fixed nginx call to uwsgi commite7b6104195Author: Spitap <dev@asdrip.fr> Date: Tue Jul 26 16:39:41 2022 +0200 fixed install within class commit4a00590354Author: Spitap <dev@asdrip.fr> Date: Tue Jul 26 16:20:03 2022 +0200 fixed restore disclamer commit15768c429eAuthor: Spitap <dev@asdrip.fr> Date: Tue Jul 26 12:07:42 2022 +0200 Restore workflow done commit439ffb94c4Author: Spitap <dev@asdrip.fr> Date: Mon Jul 25 18:54:47 2022 +0200 initial commit commit37bc21dfd3Author: Spitap <dev@asdrip.fr> Date: Tue Jul 26 10:36:08 2022 +0200 Backup postewhite.conf instead of custom whitelist Postwhite.conf contains a custom host list commit26204143afMerge:2097055d495afdAuthor: Spitap <dev@asdrip.fr> Date: Mon Jul 25 22:10:26 2022 +0200 Merge branch 'master' into backup commit20970557deAuthor: Spitap <dev@asdrip.fr> Date: Mon Jul 25 22:05:35 2022 +0200 Allow to disable mail backup commit632c26596eAuthor: Spitap <dev@asdrip.fr> Date: Mon Jul 25 21:52:15 2022 +0200 Update backup readme commit9e1c18cd6bAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 19:09:53 2022 +0200 Fix argument passed as list instead of string commitdb6457c5f5Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 19:07:18 2022 +0200 better path handling commit579faccfa5Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 19:00:32 2022 +0200 added an automatic bash option (no path provided) or a path provided bash (for cron job) commit5318fa279bAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 18:00:50 2022 +0200 bash option commit74de6a9bb1Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 17:31:56 2022 +0200 Reset pgpass before trying to backup secondary dbs commit54185a7c5aAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 17:26:40 2022 +0200 Fix database backup logic issue commit1f9d69c37cAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 17:21:59 2022 +0200 Fix copy issue commit8d02d2a9fbAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 17:09:23 2022 +0200 added safe mkdir in utils, use utils.mkdir_safe() in backup commit6f604a5fecAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 16:53:56 2022 +0200 Fix loop logic commit568c4a65a0Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 16:51:32 2022 +0200 fix none-type passed to os.path commitdc84a79528Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 14:12:35 2022 +0200 Note : capitalize affects only first letter commit304e25fa3cAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 14:10:57 2022 +0200 Fix getattr commit070efd61c4Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 14:08:39 2022 +0200 Fix import commit9917d8023eAuthor: Spitap <dev@asdrip.fr> Date: Thu Jul 21 14:02:41 2022 +0200 Edited README, fix backup run process commit27b9de6755Author: Spitap <dev@asdrip.fr> Date: Thu Jul 21 13:48:44 2022 +0200 database backup commit56ed214fb5Author: Spitap <dev@asdrip.fr> Date: Tue Jul 19 19:06:53 2022 +0200 Starting work on backup system
402 lines
12 KiB
Python
402 lines
12 KiB
Python
"""Utility functions."""
|
|
|
|
import contextlib
|
|
import datetime
|
|
import getpass
|
|
import glob
|
|
import os
|
|
import pwd
|
|
import random
|
|
import shutil
|
|
import stat
|
|
import string
|
|
import subprocess
|
|
import sys
|
|
try:
|
|
import configparser
|
|
except ImportError:
|
|
import ConfigParser as configparser
|
|
|
|
from . import config_dict_template
|
|
|
|
|
|
ENV = {}
|
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
|
|
|
|
|
class FatalError(Exception):
|
|
|
|
"""A simple exception."""
|
|
|
|
pass
|
|
|
|
|
|
def user_input(message):
|
|
"""Ask something to the user."""
|
|
try:
|
|
from builtins import input
|
|
except ImportError:
|
|
answer = raw_input(message)
|
|
else:
|
|
answer = input(message)
|
|
return answer
|
|
|
|
|
|
def exec_cmd(cmd, sudo_user=None, pinput=None, login=True, **kwargs):
|
|
"""Execute a shell command.
|
|
Run a command using the current user. Set :keyword:`sudo_user` if
|
|
you need different privileges.
|
|
:param str cmd: the command to execute
|
|
:param str sudo_user: a valid system username
|
|
:param str pinput: data to send to process's stdin
|
|
:rtype: tuple
|
|
:return: return code, command output
|
|
"""
|
|
sudo_user = ENV.get("sudo_user", sudo_user)
|
|
if sudo_user is not None:
|
|
cmd = "sudo {}-u {} {}".format("-i " if login else "", sudo_user, cmd)
|
|
if "shell" not in kwargs:
|
|
kwargs["shell"] = True
|
|
if pinput is not None:
|
|
kwargs["stdin"] = subprocess.PIPE
|
|
capture_output = False
|
|
if "capture_output" in kwargs:
|
|
capture_output = kwargs.pop("capture_output")
|
|
elif not ENV.get("debug"):
|
|
capture_output = True
|
|
if capture_output:
|
|
kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
output = None
|
|
process = subprocess.Popen(cmd, **kwargs)
|
|
if pinput or capture_output:
|
|
c_args = [pinput] if pinput is not None else []
|
|
output = process.communicate(*c_args)[0]
|
|
else:
|
|
process.wait()
|
|
return process.returncode, output
|
|
|
|
|
|
def dist_info():
|
|
"""Try to return information about the system we're running on."""
|
|
path = "/etc/os-release"
|
|
if os.path.exists(path):
|
|
info = {}
|
|
with open(path) as fp:
|
|
for line in fp.readlines():
|
|
if line == '\n':
|
|
continue
|
|
key, value = line.split("=")
|
|
value = value.rstrip('"\n')
|
|
value = value.strip('"')
|
|
info[key] = value
|
|
return info["NAME"], info["VERSION_ID"]
|
|
printcolor(
|
|
"Failed to retrieve information about your system, aborting.",
|
|
RED)
|
|
sys.exit(1)
|
|
|
|
|
|
def dist_name():
|
|
"""Try to guess the distribution name."""
|
|
return dist_info()[0].lower()
|
|
|
|
|
|
def mkdir(path, mode, uid, gid):
|
|
"""Create a directory."""
|
|
if not os.path.exists(path):
|
|
os.mkdir(path, mode)
|
|
else:
|
|
os.chmod(path, mode)
|
|
os.chown(path, uid, gid)
|
|
|
|
|
|
def mkdir_safe(path, mode, uid, gid):
|
|
"""Create a directory. Safe way (-p)"""
|
|
if not os.path.exists(path):
|
|
os.makedirs(os.path.abspath(path), mode)
|
|
mkdir(path, mode, uid, gid)
|
|
|
|
|
|
def make_password(length=16):
|
|
"""Create a random password."""
|
|
return "".join(
|
|
random.SystemRandom().choice(
|
|
string.ascii_letters + string.digits) for _ in range(length))
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def settings(**kwargs):
|
|
"""Context manager to declare temporary settings."""
|
|
for key, value in kwargs.items():
|
|
ENV[key] = value
|
|
yield
|
|
for key in kwargs.keys():
|
|
del ENV[key]
|
|
|
|
|
|
class ConfigFileTemplate(string.Template):
|
|
|
|
"""Custom class for configuration files."""
|
|
|
|
delimiter = "%"
|
|
|
|
|
|
def backup_file(fname):
|
|
"""Create a backup of a given file."""
|
|
for f in glob.glob("{}.old.*".format(fname)):
|
|
os.unlink(f)
|
|
bak_name = "{}.old.{}".format(
|
|
fname, datetime.datetime.now().isoformat())
|
|
shutil.copy(fname, bak_name)
|
|
|
|
|
|
def copy_file(src, dest):
|
|
"""Copy a file to a destination and make a backup before."""
|
|
if os.path.isdir(dest):
|
|
dest = os.path.join(dest, os.path.basename(src))
|
|
if os.path.isfile(dest):
|
|
backup_file(dest)
|
|
shutil.copy(src, dest)
|
|
|
|
|
|
def copy_from_template(template, dest, context):
|
|
"""Create and copy a configuration file from a template."""
|
|
now = datetime.datetime.now().isoformat()
|
|
with open(template) as fp:
|
|
buf = fp.read()
|
|
if os.path.isfile(dest):
|
|
backup_file(dest)
|
|
with open(dest, "w") as fp:
|
|
fp.write(
|
|
"# This file was automatically installed on {}\n"
|
|
.format(now))
|
|
fp.write(ConfigFileTemplate(buf).substitute(context))
|
|
|
|
|
|
def check_config_file(dest, interactive=False, upgrade=False, backup=False, restore=False):
|
|
"""Create a new installer config file if needed."""
|
|
is_present = True
|
|
if os.path.exists(dest):
|
|
return is_present
|
|
if upgrade:
|
|
printcolor(
|
|
"You cannot upgrade an existing installation without a "
|
|
"configuration file.", RED)
|
|
sys.exit(1)
|
|
elif backup:
|
|
is_present = False
|
|
printcolor(
|
|
"Your configuration file hasn't been found. A new one will be generated. "
|
|
"Please edit it with correct password for the databases !", RED)
|
|
elif restore:
|
|
printcolor(
|
|
"You cannot restore an existing installation without a "
|
|
f"configuration file. (file : {dest} has not been found...", RED)
|
|
sys.exit(1)
|
|
|
|
printcolor(
|
|
"Configuration file {} not found, creating new one."
|
|
.format(dest), YELLOW)
|
|
gen_config(dest, interactive)
|
|
return is_present
|
|
|
|
|
|
def has_colours(stream):
|
|
"""Check if terminal supports colors."""
|
|
if not hasattr(stream, "isatty"):
|
|
return False
|
|
if not stream.isatty():
|
|
return False # auto color only on TTYs
|
|
try:
|
|
import curses
|
|
curses.setupterm()
|
|
return curses.tigetnum("colors") > 2
|
|
except:
|
|
# guess false in case of error
|
|
return False
|
|
|
|
|
|
has_colours = has_colours(sys.stdout)
|
|
|
|
|
|
def printcolor(message, color):
|
|
"""Print a message using a green color."""
|
|
if has_colours:
|
|
message = "\x1b[1;{}m{}\x1b[0m".format(30 + color, message)
|
|
print(message)
|
|
|
|
|
|
def error(message):
|
|
"""Print error message."""
|
|
printcolor(message, RED)
|
|
|
|
|
|
def success(message):
|
|
"""Print success message."""
|
|
printcolor(message, GREEN)
|
|
|
|
|
|
def convert_version_to_int(version):
|
|
"""Convert a version string to an integer."""
|
|
number_bits = (8, 8, 16)
|
|
|
|
numbers = [int(number_string) for number_string in version.split(".")]
|
|
if len(numbers) > len(number_bits):
|
|
raise NotImplementedError(
|
|
"Versions with more than {0} decimal places are not supported"
|
|
.format(len(number_bits) - 1)
|
|
)
|
|
# add 0s for missing numbers
|
|
numbers.extend([0] * (len(number_bits) - len(numbers)))
|
|
# convert to single int and return
|
|
number = 0
|
|
total_bits = 0
|
|
for num, bits in reversed(list(zip(numbers, number_bits))):
|
|
max_num = (bits + 1) - 1
|
|
if num >= 1 << max_num:
|
|
raise ValueError(
|
|
"Number {0} cannot be stored with only {1} bits. Max is {2}"
|
|
.format(num, bits, max_num)
|
|
)
|
|
number += num << total_bits
|
|
total_bits += bits
|
|
return number
|
|
|
|
|
|
def random_key(l=16):
|
|
"""Generate a random key.
|
|
|
|
:param integer l: the key's length
|
|
:return: a string
|
|
"""
|
|
punctuation = """!#$%&()*+,-./:;<=>?@[]^_`{|}~"""
|
|
population = string.digits + string.ascii_letters + punctuation
|
|
while True:
|
|
key = "".join(random.sample(population * l, l))
|
|
if len(key) == l:
|
|
return key
|
|
|
|
|
|
def validate(value, config_entry):
|
|
if value is None:
|
|
return False
|
|
if "values" not in config_entry and "validators" not in config_entry:
|
|
return True
|
|
if "values" in config_entry:
|
|
try:
|
|
value = int(value)
|
|
except ValueError:
|
|
return False
|
|
return value >= 0 and value < len(config_entry["values"])
|
|
if "validators" in config_entry:
|
|
for validator in config_entry["validators"]:
|
|
valide, message = validator(value)
|
|
if not valide:
|
|
printcolor(message, MAGENTA)
|
|
return False
|
|
return True
|
|
|
|
|
|
def get_entry_value(entry, interactive):
|
|
if callable(entry["default"]):
|
|
default_value = entry["default"]()
|
|
else:
|
|
default_value = entry["default"]
|
|
user_value = None
|
|
if entry.get("customizable") and interactive:
|
|
while (user_value != '' and not validate(user_value, entry)):
|
|
question = entry.get("question")
|
|
if entry.get("values"):
|
|
question += " from the list"
|
|
values = entry.get("values")
|
|
for index, value in enumerate(values):
|
|
question += "\n{} {}".format(index, value)
|
|
print(question)
|
|
print("default is <{}>".format(default_value))
|
|
user_value = user_input("-> ")
|
|
|
|
if entry.get("values") and user_value != "":
|
|
user_value = values[int(user_value)]
|
|
return user_value if user_value else default_value
|
|
|
|
|
|
def gen_config(dest, interactive=False):
|
|
"""Create config file from dict template"""
|
|
tpl_dict = config_dict_template.ConfigDictTemplate
|
|
config = configparser.ConfigParser()
|
|
# only ask about options we need, else still generate default
|
|
for section in tpl_dict:
|
|
if "if" in section:
|
|
config_key, value = section.get("if").split("=")
|
|
section_name, option = config_key.split(".")
|
|
interactive_section = (
|
|
config.get(section_name, option) == value and interactive)
|
|
else:
|
|
interactive_section = interactive
|
|
config.add_section(section["name"])
|
|
for config_entry in section["values"]:
|
|
value = get_entry_value(config_entry, interactive_section)
|
|
config.set(section["name"], config_entry["option"], value)
|
|
|
|
with open(dest, "w") as configfile:
|
|
config.write(configfile)
|
|
|
|
# Set file owner to running user and group, and set config file permission to 600
|
|
current_username = getpass.getuser()
|
|
current_user = pwd.getpwnam(current_username)
|
|
os.chown(dest, current_user[2], current_user[3])
|
|
os.chmod(dest, stat.S_IRUSR | stat.S_IWUSR)
|
|
|
|
|
|
def validate_backup_path(path: str, silent_mode: bool):
|
|
"""Check if provided backup path is valid or not."""
|
|
path_exists = os.path.exists(path)
|
|
if path_exists and os.path.isfile(path):
|
|
printcolor(
|
|
"Error, you provided a file instead of a directory!", RED)
|
|
return None
|
|
|
|
if not path_exists:
|
|
if not silent_mode:
|
|
create_dir = input(
|
|
f"\"{path}\" doesn't exist, would you like to create it? [Y/n]\n"
|
|
).lower()
|
|
|
|
if silent_mode or (not silent_mode and create_dir.startswith("y")):
|
|
pw = pwd.getpwnam("root")
|
|
mkdir_safe(path, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
|
|
else:
|
|
printcolor(
|
|
"Error, backup directory not present.", RED
|
|
)
|
|
return None
|
|
|
|
if len(os.listdir(path)) != 0:
|
|
if not silent_mode:
|
|
delete_dir = input(
|
|
"Warning: backup directory is not empty, it will be purged if you continue... [Y/n]\n").lower()
|
|
|
|
if silent_mode or (not silent_mode and delete_dir.startswith("y")):
|
|
try:
|
|
os.remove(os.path.join(path, "installer.cfg"))
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
shutil.rmtree(os.path.join(path, "custom"),
|
|
ignore_errors=False)
|
|
shutil.rmtree(os.path.join(path, "mails"), ignore_errors=False)
|
|
shutil.rmtree(os.path.join(path, "databases"),
|
|
ignore_errors=False)
|
|
else:
|
|
printcolor(
|
|
"Error: backup directory not clean.", RED
|
|
)
|
|
return None
|
|
|
|
backup_path = path
|
|
pw = pwd.getpwnam("root")
|
|
for dir in ["custom/", "databases/"]:
|
|
mkdir_safe(os.path.join(backup_path, dir),
|
|
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
|
|
return backup_path
|