Files
simple_backup/simple_backup/simple_backup.py
T

920 lines
29 KiB
Python
Raw Normal View History

2023-05-29 23:10:29 +02:00
#!/usr/bin/env python3
"""
A simple python script that calls rsync to perform a backup
Parameters can be specified on the command line or using a configuration file
Backup to a remote server is also supported (experimental)
Classes:
MyFormatter
Backup
"""
2023-06-20 17:36:12 +02:00
2023-05-04 23:16:15 +02:00
# Import libraries
2023-05-28 21:30:40 +02:00
import sys
2023-05-04 23:16:15 +02:00
import os
2024-09-28 09:47:33 +02:00
from typing import Callable, List, Optional, ParamSpec, TypeVar, Union
2023-05-31 19:30:31 +02:00
import warnings
2023-05-04 23:16:15 +02:00
from functools import wraps
2023-06-02 00:09:14 +02:00
from shutil import rmtree, which
2023-05-29 18:33:02 +02:00
import shlex
2023-05-04 23:16:15 +02:00
import argparse
import configparser
import logging
from logging import StreamHandler
from timeit import default_timer
from subprocess import Popen, PIPE, STDOUT
from datetime import datetime
from tempfile import mkstemp
2023-07-16 08:22:51 +02:00
from getpass import GetPassWarning, getpass
2023-06-04 12:09:30 +02:00
from glob import glob
2023-05-28 21:30:40 +02:00
2023-05-04 23:16:15 +02:00
from dotenv import load_dotenv
2023-05-28 21:30:40 +02:00
2023-05-31 19:30:31 +02:00
warnings.filterwarnings('error')
2023-06-18 22:53:29 +02:00
try:
import paramiko
from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
except ImportError:
pass
2023-05-04 23:16:15 +02:00
try:
from systemd import journal
except ImportError:
journal = None
2023-05-04 23:16:15 +02:00
2023-05-25 23:44:59 +02:00
try:
import dbus
except ImportError:
pass
2023-05-04 23:16:15 +02:00
load_dotenv()
logging.getLogger().setLevel(logging.DEBUG)
logger = logging.getLogger(os.path.basename(__file__))
c_handler = StreamHandler()
c_handler.setLevel(logging.INFO)
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)
if journal:
2023-05-04 23:16:15 +02:00
j_handler = journal.JournalHandler()
j_handler.setLevel(logging.INFO)
j_format = logging.Formatter('%(levelname)s - %(message)s')
j_handler.setFormatter(j_format)
logger.addHandler(j_handler)
2024-09-28 09:47:33 +02:00
P = ParamSpec('P')
R = TypeVar('R')
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def timing(func: Callable[P, R]) -> Callable[P, R]:
2023-05-29 23:10:29 +02:00
"""Decorator to measure execution time of a function
Parameters:
2024-09-28 09:47:33 +02:00
func: Function to decorate
2023-05-29 23:10:29 +02:00
"""
2024-09-28 09:47:33 +02:00
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
start = default_timer()
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
value = func(*args, **kwargs)
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
end = default_timer()
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
logger.info('Elapsed time: %.3f seconds', end - start)
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
return value
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
return wrapper
2023-05-04 23:16:15 +02:00
class MyFormatter(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
2023-05-29 23:10:29 +02:00
"""Custom format for argparse help text"""
2023-05-04 23:16:15 +02:00
class Backup:
2023-05-29 23:10:29 +02:00
"""Main class defining parameters and functions for performing backup
Attributes:
inputs: list
Files and folders that will be backup up
output: str
Path where the backup will be saved
exclude: list
List of files/folders/patterns to exclude from backup
options: str
String representing main backup options for rsync
keep: int
Number of old backup to preserve
2023-06-20 17:36:12 +02:00
ssh_host: str
2023-05-29 23:10:29 +02:00
Hostname of server (for remote backup)
2023-06-20 17:36:12 +02:00
ssh_user: str
2023-05-29 23:10:29 +02:00
Username for server login (for remote backup)
ssh_keyfile: str
Location of ssh key
2023-06-16 17:42:13 +02:00
remote_sudo: bool
Run remote rsync with sudo
2023-05-29 23:10:29 +02:00
remove_before: bool
Indicate if removing old backups will be performed before copying files
Methods:
check_params():
Check if parameters for the backup are valid
define_backup_dir():
Define the actual backup dir
remove_old_backups():
Remove old backups if there are more than indicated by 'keep'
find_last_backup():
Get path of last backup (from last_backup symlink) for rsync --link-dest
run():
Perform the backup
"""
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def __init__(self, inputs: List[str], output: str, exclude: List[str], keep: int, options: str,
ssh_host: Optional[str] = None, ssh_user: Optional[str] = None, ssh_keyfile: Optional[str] = None,
remote_sudo: bool = False, remove_before: bool = False, verbose: bool = False) -> None:
2023-05-04 23:16:15 +02:00
self.inputs = inputs
self.output = output
self.exclude = exclude
self.options = options
self.keep = keep
2023-06-20 17:36:12 +02:00
self.ssh_host = ssh_host
self.ssh_user = ssh_user
2023-05-28 21:30:40 +02:00
self.ssh_keyfile = ssh_keyfile
2023-06-15 23:12:19 +02:00
self.remote_sudo = remote_sudo
2023-06-03 16:09:34 +02:00
self._remove_before = remove_before
2023-06-25 11:49:02 +02:00
self._verbose = verbose
2023-05-04 23:16:15 +02:00
self._last_backup = ''
2023-05-28 23:19:08 +02:00
self._server = ''
2023-05-04 23:16:15 +02:00
self._output_dir = ''
self._inputs_path = ''
self._exclude_path = ''
2024-09-28 09:47:33 +02:00
self._remote = False
2023-05-28 23:19:08 +02:00
self._ssh = None
2023-06-02 00:09:14 +02:00
self._password_auth = False
self._password = None
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def check_params(self, homedir: str = '') -> int:
2023-05-29 23:10:29 +02:00
"""Check if parameters for the backup are valid"""
2023-05-04 23:16:15 +02:00
if self.inputs is None or len(self.inputs) == 0:
2025-03-30 15:02:50 +02:00
logger.info(
'No existing files or directories specified for backup. Nothing to do')
2023-05-04 23:16:15 +02:00
2023-06-15 16:58:56 +02:00
return 1
2023-05-04 23:16:15 +02:00
if self.output is None:
2025-03-30 15:02:50 +02:00
logger.critical(
'No output path specified. Use -o argument or specify output path in configuration file')
2023-05-04 23:16:15 +02:00
2023-06-15 16:58:56 +02:00
return 2
2023-05-04 23:16:15 +02:00
2023-06-20 17:36:12 +02:00
if self.ssh_host is not None and self.ssh_user is not None:
2023-05-28 21:30:40 +02:00
self._remote = True
2023-05-04 23:16:15 +02:00
2023-05-28 21:30:40 +02:00
if self._remote:
2023-06-20 19:22:22 +02:00
self._ssh = self._ssh_connect(homedir)
2023-05-28 23:19:08 +02:00
2023-05-29 00:09:54 +02:00
if self._ssh is None:
2023-06-20 19:22:22 +02:00
return 5
2023-05-29 00:09:54 +02:00
2025-03-30 15:02:50 +02:00
_, stdout, _ = self._ssh.exec_command(
f'if [ -d "{self.output}" ]; then echo "ok"; fi')
2023-05-28 23:19:08 +02:00
2023-05-29 17:57:12 +02:00
output = stdout.read().decode('utf-8').strip()
if output != 'ok':
2023-05-28 23:19:08 +02:00
logger.critical('Output path for backup does not exist')
2023-06-15 22:10:19 +02:00
return 2
2023-05-28 21:30:40 +02:00
else:
if not os.path.isdir(self.output):
logger.critical('Output path for backup does not exist')
2023-06-15 22:10:19 +02:00
return 2
2023-05-04 23:16:15 +02:00
self.output = os.path.abspath(self.output)
if self.keep is None:
self.keep = -1
2023-06-15 16:58:56 +02:00
return 0
2023-05-04 23:16:15 +02:00
# Function to create the actual backup directory
2024-09-28 09:47:33 +02:00
def define_backup_dir(self) -> None:
2023-05-29 23:10:29 +02:00
"""Define the actual backup dir"""
2023-05-04 23:16:15 +02:00
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self._output_dir = f'{self.output}/simple_backup/{now}'
2023-05-28 21:30:40 +02:00
if self._remote:
2023-06-20 17:36:12 +02:00
self._server = f'{self.ssh_user}@{self.ssh_host}:'
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def remove_old_backups(self) -> None:
2023-05-29 23:10:29 +02:00
"""Remove old backups if there are more than indicated by 'keep'"""
2023-05-28 21:30:40 +02:00
if self._remote:
2024-09-28 09:47:33 +02:00
assert self._ssh is not None
2025-03-30 15:02:50 +02:00
_, stdout, _ = self._ssh.exec_command(
f'ls {self.output}/simple_backup')
2023-05-29 17:57:12 +02:00
dirs = stdout.read().decode('utf-8').strip().split('\n')
2023-06-03 16:09:34 +02:00
n_backup = len(dirs)
if not self._remove_before:
n_backup -= 1
2023-05-28 21:30:40 +02:00
count = 0
2023-05-29 17:57:12 +02:00
if n_backup > self.keep:
logger.info('Removing old backups...')
dirs.sort()
for i in range(n_backup - self.keep):
if self.remote_sudo:
2025-03-30 15:02:50 +02:00
_, _, stderr = self._ssh.exec_command(
f'sudo rm -r "{self.output}/simple_backup/{dirs[i]}"')
else:
2025-03-30 15:02:50 +02:00
_, _, stderr = self._ssh.exec_command(
f'rm -r "{self.output}/simple_backup/{dirs[i]}"')
2023-05-29 17:57:12 +02:00
err = stderr.read().decode('utf-8').strip().split('\n')[0]
if err != '':
2025-03-30 15:02:50 +02:00
logger.error(
'Error while removing backup %s.', {dirs[i]})
2023-05-29 17:57:12 +02:00
logger.error(err)
else:
count += 1
2023-05-28 21:30:40 +02:00
else:
try:
dirs = os.listdir(f'{self.output}/simple_backup')
except FileNotFoundError:
return
2023-05-04 23:16:15 +02:00
2023-06-03 16:09:34 +02:00
n_backup = len(dirs)
if not self._remove_before:
n_backup -= 1
2023-05-04 23:16:15 +02:00
2023-05-28 21:30:40 +02:00
count = 0
2023-05-04 23:16:15 +02:00
2023-05-28 21:30:40 +02:00
if n_backup > self.keep:
logger.info('Removing old backups...')
dirs.sort()
2023-05-04 23:16:15 +02:00
2023-05-28 21:30:40 +02:00
for i in range(n_backup - self.keep):
try:
rmtree(f'{self.output}/simple_backup/{dirs[i]}')
count += 1
except FileNotFoundError:
2025-03-30 15:02:50 +02:00
logger.error(
'Error while removing backup %s. Directory not found', dirs[i])
2023-05-28 21:30:40 +02:00
except PermissionError:
2025-03-30 15:02:50 +02:00
logger.error(
'Error while removing backup %s. Permission denied', dirs[i])
2023-05-04 23:16:15 +02:00
2023-05-28 21:30:40 +02:00
if count == 1:
2023-05-29 23:10:29 +02:00
logger.info('Removed %d backup', count)
2023-05-28 21:30:40 +02:00
elif count > 1:
2023-05-29 23:10:29 +02:00
logger.info('Removed %d backups', count)
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def find_last_backup(self) -> None:
2023-05-29 23:10:29 +02:00
"""Get path of last backup (from last_backup symlink) for rsync --link-dest"""
2023-05-28 21:30:40 +02:00
if self._remote:
2023-05-28 23:19:08 +02:00
if self._ssh is None:
logger.critical('SSH connection to server failed')
2023-06-18 22:53:29 +02:00
sys.exit(5)
2023-05-28 23:19:08 +02:00
2025-03-30 15:02:50 +02:00
_, stdout, _ = self._ssh.exec_command(
f'find {self.output}/simple_backup/ -mindepth 1 -maxdepth 1 -type d | sort')
2023-06-02 20:12:53 +02:00
output = stdout.read().decode('utf-8').strip().split('\n')
2023-05-28 23:19:08 +02:00
2023-06-02 20:12:53 +02:00
if output[-1] != '':
self._last_backup = output[-1]
2023-05-28 23:19:08 +02:00
else:
logger.info('No previous backups available')
2023-05-28 21:30:40 +02:00
else:
2023-06-02 20:12:53 +02:00
try:
2025-03-30 15:02:50 +02:00
dirs = sorted([f.path for f in os.scandir(
f'{self.output}/simple_backup') if f.is_dir(follow_symlinks=False)])
2023-06-02 20:12:53 +02:00
except FileNotFoundError:
logger.info('No previous backups available')
2023-06-02 20:12:53 +02:00
return
2023-06-15 22:10:19 +02:00
except PermissionError:
2025-03-30 15:02:50 +02:00
logger.critical(
'Cannot access the backup directory. Permission denied')
2023-06-15 22:10:19 +02:00
try:
2024-09-28 09:47:33 +02:00
_notify('Backup failed (check log for details)')
2023-06-15 22:10:19 +02:00
except NameError:
pass
sys.exit(3)
2023-06-02 20:12:53 +02:00
try:
self._last_backup = dirs[-1]
except IndexError:
logger.info('No previous backups available')
2023-05-28 21:30:40 +02:00
2024-09-28 09:47:33 +02:00
def _ssh_connect(self, homedir: str = '') -> paramiko.client.SSHClient:
2023-06-18 22:53:29 +02:00
try:
ssh = paramiko.SSHClient()
except NameError:
logger.error('Install paramiko for ssh support')
2024-09-28 09:47:33 +02:00
2023-06-18 22:53:29 +02:00
return None
2023-06-15 11:48:23 +02:00
try:
ssh.load_host_keys(filename=f'{homedir}/.ssh/known_hosts')
except FileNotFoundError:
2024-09-28 09:47:33 +02:00
logger.warning('Cannot find file %s/.ssh/known_hosts', homedir)
2023-06-15 11:48:23 +02:00
2023-05-31 19:30:31 +02:00
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
2023-05-28 21:30:40 +02:00
2023-05-31 19:30:31 +02:00
try:
2023-06-20 17:36:12 +02:00
ssh.connect(self.ssh_host, username=self.ssh_user)
2023-05-29 00:09:54 +02:00
2023-05-31 19:30:31 +02:00
return ssh
except UserWarning:
2025-03-30 15:02:50 +02:00
k = input(
f'Unknown key for host {self.ssh_host}. Continue anyway? (Y/N) ')
2023-05-31 19:30:31 +02:00
if k[0].upper() == 'Y':
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
else:
return None
2023-06-15 11:48:23 +02:00
except paramiko.BadHostKeyException as e:
logger.critical('Can\'t connect to the server.')
logger.critical(e)
return None
2023-05-31 19:30:31 +02:00
except paramiko.SSHException:
pass
try:
2023-06-20 17:36:12 +02:00
ssh.connect(self.ssh_host, username=self.ssh_user)
2023-05-31 19:30:31 +02:00
return ssh
except paramiko.SSHException:
pass
2023-05-29 00:09:54 +02:00
if self.ssh_keyfile is None:
2023-06-02 00:09:14 +02:00
try:
2025-03-30 15:02:50 +02:00
password = getpass(
f'{self.ssh_user}@{self.ssh_host}\'s password: ')
ssh.connect(self.ssh_host, username=self.ssh_user,
password=password)
2023-05-29 00:09:54 +02:00
2023-06-02 00:09:14 +02:00
self._password_auth = True
os.environ['SSHPASS'] = password
return ssh
2023-07-16 08:22:51 +02:00
except GetPassWarning as e:
logger.critical('Unable to get password')
logger.critical(e)
return None
2023-06-02 00:09:14 +02:00
except paramiko.SSHException as e:
logger.critical('Can\'t connect to the server.')
logger.critical(e)
return None
pkey = None
2023-05-29 00:09:54 +02:00
2023-05-28 21:30:40 +02:00
try:
pkey = RSAKey.from_private_key_file(self.ssh_keyfile)
except paramiko.PasswordRequiredException:
2025-03-30 15:02:50 +02:00
password = getpass(
f'Enter passwphrase for key \'{self.ssh_keyfile}\': ')
2023-05-28 21:30:40 +02:00
try:
pkey = RSAKey.from_private_key_file(self.ssh_keyfile, password)
except paramiko.SSHException:
pass
if pkey is None:
try:
pkey = Ed25519Key.from_private_key_file(self.ssh_keyfile)
except paramiko.PasswordRequiredException:
try:
2025-03-30 15:02:50 +02:00
pkey = Ed25519Key.from_private_key_file(
self.ssh_keyfile, password)
2023-05-28 21:30:40 +02:00
except paramiko.SSHException:
pass
if pkey is None:
try:
pkey = ECDSAKey.from_private_key_file(self.ssh_keyfile)
except paramiko.PasswordRequiredException:
try:
2025-03-30 15:02:50 +02:00
pkey = ECDSAKey.from_private_key_file(
self.ssh_keyfile, password)
2023-05-28 21:30:40 +02:00
except paramiko.SSHException:
pass
if pkey is None:
try:
pkey = DSSKey.from_private_key_file(self.ssh_keyfile)
except paramiko.PasswordRequiredException:
try:
2025-03-30 15:02:50 +02:00
pkey = DSSKey.from_private_key_file(
self.ssh_keyfile, password)
2023-05-28 21:30:40 +02:00
except paramiko.SSHException:
pass
try:
2023-06-20 17:36:12 +02:00
ssh.connect(self.ssh_host, username=self.ssh_user, pkey=pkey)
2023-05-31 19:30:31 +02:00
except paramiko.SSHException:
logger.critical('SSH connection to server failed')
2023-05-28 21:30:40 +02:00
return None
return ssh
2023-05-04 23:16:15 +02:00
2024-09-28 09:47:33 +02:00
def _returncode_log(self, returncode: int) -> None:
2023-06-25 11:49:02 +02:00
match returncode:
case 2:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 2) - Protocol incompatibility')
2023-06-25 11:49:02 +02:00
case 3:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 3) - Errors selecting input/output files, dirs')
2023-06-25 11:49:02 +02:00
case 4:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 4) - Requested action not supported')
2023-06-25 11:49:02 +02:00
case 5:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 5) - Error starting client-server protocol')
2023-06-25 11:49:02 +02:00
case 10:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 10) - Error in socket I/O')
2023-06-25 11:49:02 +02:00
case 11:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 11) - Error in file I/O')
2023-06-25 11:49:02 +02:00
case 12:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 12) - Error in rsync protocol data stream')
2023-06-25 11:49:02 +02:00
case 22:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 22) - Error allocating core memory buffers')
2023-06-25 11:49:02 +02:00
case 23:
2025-03-30 15:02:50 +02:00
logger.warning(
'Rsync error (return code 23) - Partial transfer due to error')
2023-06-25 11:49:02 +02:00
case 24:
2025-03-30 15:02:50 +02:00
logger.warning(
'Rsync error (return code 24) - Partial transfer due to vanished source files')
2023-06-25 11:49:02 +02:00
case 30:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 30) - Timeout in data send/receive')
2023-06-25 11:49:02 +02:00
case 35:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code 35) - Timeout waiting for daemon connection')
2023-06-25 11:49:02 +02:00
case _:
2025-03-30 15:02:50 +02:00
logger.error(
'Rsync error (return code %d) - Check rsync(1) for details', returncode)
2023-06-25 11:49:02 +02:00
2023-05-04 23:16:15 +02:00
# Function to read configuration file
2024-09-28 09:47:33 +02:00
@timing
def run(self) -> int:
2023-05-29 23:10:29 +02:00
"""Perform the backup"""
2023-05-04 23:16:15 +02:00
logger.info('Starting backup...')
2023-05-25 23:44:59 +02:00
try:
2023-05-29 23:10:29 +02:00
_notify('Starting backup...')
2023-05-25 23:44:59 +02:00
except NameError:
pass
2023-05-28 21:30:40 +02:00
self.define_backup_dir()
2023-05-04 23:16:15 +02:00
self.find_last_backup()
_, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
2023-06-15 18:44:22 +02:00
count = 0
2023-05-04 23:16:15 +02:00
2023-05-29 23:10:29 +02:00
with open(self._inputs_path, 'w', encoding='utf-8') as fp:
2023-05-04 23:16:15 +02:00
for i in self.inputs:
if not os.path.exists(i):
2023-05-29 23:10:29 +02:00
logger.warning('Input %s not found. Skipping', i)
2023-05-04 23:16:15 +02:00
else:
fp.write(i)
fp.write('\n')
2023-06-15 18:44:22 +02:00
count += 1
if count == 0:
2025-03-30 15:02:50 +02:00
logger.info(
'No existing files or directories specified for backup. Nothing to do')
2023-06-15 18:44:22 +02:00
try:
2024-09-28 09:47:33 +02:00
_notify('Backup finished. No files copied')
2023-06-15 18:44:22 +02:00
except NameError:
pass
return 1
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
2023-05-04 23:16:15 +02:00
2023-05-29 23:10:29 +02:00
with open(self._exclude_path, 'w', encoding='utf-8') as fp:
2023-05-31 19:07:50 +02:00
if self.exclude is not None:
for e in self.exclude:
fp.write(e)
fp.write('\n')
2023-05-04 23:16:15 +02:00
2023-06-03 16:09:34 +02:00
if self.keep != -1 and self._remove_before:
self.remove_old_backups()
2023-05-04 23:16:15 +02:00
logger.info('Copying files. This may take a long time...')
if self._last_backup == '':
2023-05-29 18:33:02 +02:00
rsync = f'/usr/bin/rsync {self.options} --exclude-from={self._exclude_path} ' +\
2025-03-30 15:02:50 +02:00
f'--files-from={self._inputs_path} / "{self._server}{self._output_dir}"'
2023-05-04 23:16:15 +02:00
else:
2023-05-29 18:33:02 +02:00
rsync = f'/usr/bin/rsync {self.options} --link-dest="{self._last_backup}" --exclude-from=' +\
2025-03-30 15:02:50 +02:00
f'{self._exclude_path} --files-from={self._inputs_path} / "{self._server}{self._output_dir}"'
2023-05-28 21:30:40 +02:00
2023-06-20 19:22:22 +02:00
euid = os.geteuid()
if euid == 0 and self.ssh_keyfile is not None:
2023-06-15 11:48:23 +02:00
rsync = f'{rsync} -e \'ssh -i {self.ssh_keyfile} -o StrictHostKeyChecking=no\''
2023-06-02 00:09:14 +02:00
elif self._password_auth and which('sshpass'):
2023-06-20 17:36:12 +02:00
rsync = f'{rsync} -e \'sshpass -e ssh -l {self.ssh_user} -o StrictHostKeyChecking=no\''
2023-06-15 11:48:23 +02:00
else:
rsync = f'{rsync} -e \'ssh -o StrictHostKeyChecking=no\''
2023-06-15 23:12:19 +02:00
if self._remote and self.remote_sudo:
rsync = f'{rsync} --rsync-path="sudo rsync"'
2023-05-29 18:33:02 +02:00
args = shlex.split(rsync)
2023-05-04 23:16:15 +02:00
2023-05-29 23:10:29 +02:00
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=STDOUT, shell=False) as p:
2024-09-28 09:47:33 +02:00
output: Union[bytes, List[str]]
2023-05-29 23:10:29 +02:00
output, _ = p.communicate()
2023-05-04 23:16:15 +02:00
2023-06-02 20:12:53 +02:00
try:
del os.environ['SSHPASS']
except KeyError:
pass
2023-06-02 00:09:14 +02:00
2023-06-25 11:49:02 +02:00
returncode = p.returncode
2023-05-25 17:20:32 +02:00
2023-05-04 23:16:15 +02:00
output = output.decode("utf-8").split('\n')
2023-06-25 11:49:02 +02:00
if returncode == 0:
if self._verbose:
logger.info('rsync: %s', output)
else:
logger.info('rsync: %s', output[-3])
logger.info('rsync: %s', output[-2])
2023-05-29 17:57:12 +02:00
else:
2023-06-25 11:49:02 +02:00
self._returncode_log(returncode)
if self._verbose:
if returncode in [23, 24]:
logger.warning(output)
else:
logger.error(output)
2023-05-04 23:16:15 +02:00
2023-06-03 16:09:34 +02:00
if self.keep != -1 and not self._remove_before:
2023-05-04 23:16:15 +02:00
self.remove_old_backups()
os.remove(self._inputs_path)
os.remove(self._exclude_path)
2023-06-15 11:48:23 +02:00
if self._remote:
2024-09-28 09:47:33 +02:00
assert self._ssh is not None
2025-03-30 15:02:50 +02:00
_, stdout, _ = self._ssh.exec_command(
f'if [ -d "{self._output_dir}" ]; then echo "ok"; fi')
2023-05-04 23:16:15 +02:00
2023-06-15 11:48:23 +02:00
output = stdout.read().decode('utf-8').strip()
2023-05-04 23:16:15 +02:00
2023-06-15 11:48:23 +02:00
if output == 'ok':
logger.info('Backup completed')
try:
_notify('Backup completed')
except NameError:
pass
else:
logger.error('Backup failed')
try:
_notify('Backup failed (check log for details)')
except NameError:
pass
if self._ssh:
self._ssh.close()
2023-05-25 23:44:59 +02:00
else:
2023-06-25 11:49:02 +02:00
if returncode != 0:
2025-03-30 15:02:50 +02:00
logger.error(
'Some errors occurred while performing the backup')
2023-06-15 11:48:23 +02:00
try:
2025-03-30 15:02:50 +02:00
_notify(
'Some errors occurred while performing the backup. Check log for details')
2023-06-15 11:48:23 +02:00
except NameError:
pass
2023-06-15 22:10:19 +02:00
return 4
2023-06-15 11:48:23 +02:00
2023-06-25 11:49:02 +02:00
logger.info('Backup completed')
try:
_notify('Backup completed')
except NameError:
pass
2023-05-28 21:30:40 +02:00
2023-06-15 17:14:17 +02:00
return 0
2023-05-04 23:16:15 +02:00
2025-03-30 15:02:04 +02:00
def _parse_arguments() -> argparse.Namespace:
2023-06-20 19:22:22 +02:00
euid = os.geteuid()
if euid == 0:
user = os.getenv('SUDO_USER')
else:
user = os.getenv('USER')
2024-09-28 09:47:33 +02:00
2023-06-20 19:22:22 +02:00
homedir = os.path.expanduser(f'~{user}')
2023-05-04 23:16:15 +02:00
parser = argparse.ArgumentParser(prog='simple_backup',
description='Simple backup script written in Python that uses rsync to copy files',
2023-06-01 22:18:32 +02:00
epilog='See simple_backup(1) manpage for full documentation',
2023-05-04 23:16:15 +02:00
formatter_class=MyFormatter)
2025-03-30 15:02:50 +02:00
parser.add_argument('-v', '--verbose', action='store_true',
help='More verbose output')
2023-05-04 23:16:15 +02:00
parser.add_argument('-c', '--config', default=f'{homedir}/.config/simple_backup/simple_backup.conf',
help='Specify location of configuration file')
2025-03-30 15:02:50 +02:00
parser.add_argument('-i', '--inputs', nargs='+',
help='Paths/files to backup')
parser.add_argument(
'-o', '--output', help='Output directory for the backup')
parser.add_argument('-e', '--exclude', nargs='+',
help='Files/directories/patterns to exclude from the backup')
parser.add_argument('-k', '--keep', type=int,
help='Number of old backups to keep')
parser.add_argument(
'-u', '--user', help='Explicitly specify the user running the backup')
parser.add_argument('-s', '--checksum', action='store_true',
help='Use checksum rsync option to compare files')
parser.add_argument(
'--ssh-host', help='Server hostname (for remote backup)')
parser.add_argument(
'--ssh-user', help='Username to connect to server (for remote backup)')
2023-05-28 21:30:40 +02:00
parser.add_argument('--keyfile', help='SSH key location')
2025-03-30 15:02:50 +02:00
parser.add_argument('-z', '--compress', action='store_true',
help='Compress data during the transfer')
parser.add_argument('--remove-before-backup', action='store_true',
help='Remove old backups before executing the backup, instead of after')
2025-03-30 15:02:50 +02:00
parser.add_argument('--no-syslog', action='store_true',
help='Disable systemd journal logging')
2023-06-15 21:30:43 +02:00
parser.add_argument('--rsync-options', nargs='+',
2025-03-30 15:02:50 +02:00
choices=['a', 'l', 'p', 't', 'g', 'o',
'c', 'h', 's', 'D', 'H', 'X'],
2023-06-15 21:30:43 +02:00
help='Specify options for rsync')
2025-03-30 15:02:50 +02:00
parser.add_argument('--remote-sudo', action='store_true',
help='Run rsync on remote server with sudo if allowed')
2023-06-16 17:42:13 +02:00
parser.add_argument('--numeric-ids', action='store_true',
2023-06-19 16:00:21 +02:00
help='Use rsync \'--numeric-ids\' option (don\'t map uid/gid values by name)')
2023-05-04 23:16:15 +02:00
args = parser.parse_args()
return args
2025-03-30 15:02:04 +02:00
def _expand_inputs(inputs, user: Optional[str] = None):
2023-06-04 12:09:30 +02:00
expanded_inputs = []
for i in inputs:
2023-06-15 15:34:00 +02:00
if i == '':
continue
2023-06-20 19:22:22 +02:00
if user is not None:
i_ex = glob(os.path.expanduser(i.replace('~', f'~{user}')))
else:
i_ex = glob(i)
if '~' in i:
logger.warning('Cannot expand \'~\'. No user specified')
2023-06-04 12:09:30 +02:00
if len(i_ex) == 0:
2025-03-30 15:02:50 +02:00
logger.warning(
'No file or directory matching input %s. Skipping...', i)
2023-06-04 12:09:30 +02:00
else:
2023-06-15 15:34:00 +02:00
expanded_inputs.extend(i_ex)
2023-06-04 12:09:30 +02:00
return expanded_inputs
2025-03-30 15:02:04 +02:00
def _read_config(config_file, user: Optional[str] = None):
2023-06-18 22:58:53 +02:00
config_args = {'inputs': None,
'output': None,
'exclude': None,
'keep': -1,
2023-06-20 17:36:12 +02:00
'ssh_host': None,
'ssh_user': None,
2023-06-18 22:58:53 +02:00
'ssh_keyfile': None,
'remote_sudo': False,
'numeric_ids': False}
2023-06-16 17:42:13 +02:00
2023-05-04 23:16:15 +02:00
if not os.path.isfile(config_file):
2025-03-30 14:37:30 +02:00
if user is not None:
logger.warning('Config file %s does not exist', config_file)
else:
2025-03-30 15:02:50 +02:00
logger.warning(
'User not specified. Can\'t read configuration file')
2023-05-04 23:16:15 +02:00
2023-06-16 17:42:13 +02:00
return config_args
2023-05-04 23:16:15 +02:00
config = configparser.ConfigParser()
config.read(config_file)
2023-06-15 09:30:59 +02:00
section = 'backup'
# Allow compatibility with previous version of config file
try:
inputs = config.get(section, 'inputs')
except configparser.NoSectionError:
section = 'default'
inputs = config.get(section, 'inputs')
2023-05-04 23:16:15 +02:00
inputs = inputs.split(',')
2023-06-20 19:22:22 +02:00
inputs = _expand_inputs(inputs, user)
2023-06-04 12:09:30 +02:00
inputs = list(set(inputs))
2023-06-16 17:42:13 +02:00
config_args['inputs'] = inputs
2023-06-15 09:30:59 +02:00
output = config.get(section, 'backup_dir')
2023-06-20 19:22:22 +02:00
if user is not None:
output = os.path.expanduser(output.replace('~', f'~{user}'))
elif user is None and '~' in output:
logger.warning('Cannot expand \'~\', no user specified')
2023-06-15 23:12:19 +02:00
2023-06-16 17:42:13 +02:00
config_args['output'] = output
2023-06-15 23:12:19 +02:00
try:
exclude = config.get(section, 'exclude')
exclude = exclude.split(',')
except configparser.NoOptionError:
exclude = []
2023-06-16 17:42:13 +02:00
config_args['exclude'] = exclude
2023-06-15 23:12:19 +02:00
try:
keep = config.getint(section, 'keep')
except configparser.NoOptionError:
keep = -1
2023-05-28 21:30:40 +02:00
2023-06-16 17:42:13 +02:00
config_args['keep'] = keep
2023-05-28 21:30:40 +02:00
try:
2023-06-20 17:36:12 +02:00
ssh_host = config.get('server', 'ssh_host')
ssh_user = config.get('server', 'ssh_user')
2023-05-28 21:30:40 +02:00
except (configparser.NoSectionError, configparser.NoOptionError):
2023-06-20 17:36:12 +02:00
ssh_host = None
ssh_user = None
2023-06-03 15:44:31 +02:00
2023-06-20 17:36:12 +02:00
config_args['ssh_host'] = ssh_host
config_args['ssh_user'] = ssh_user
2023-06-16 17:42:13 +02:00
2023-06-03 15:44:31 +02:00
try:
ssh_keyfile = config.get('server', 'ssh_keyfile')
except (configparser.NoSectionError, configparser.NoOptionError):
2023-05-28 21:30:40 +02:00
ssh_keyfile = None
2023-05-04 23:16:15 +02:00
2023-06-16 17:42:13 +02:00
config_args['ssh_keyfile'] = ssh_keyfile
2023-06-15 23:12:19 +02:00
try:
remote_sudo = config.getboolean('server', 'remote_sudo')
except (configparser.NoSectionError, configparser.NoOptionError):
remote_sudo = False
2023-06-16 17:42:13 +02:00
config_args['remote_sudo'] = remote_sudo
2023-06-16 16:18:12 +02:00
try:
numeric_ids = config.getboolean('server', 'numeric_ids')
except (configparser.NoSectionError, configparser.NoOptionError):
numeric_ids = False
2023-06-16 17:42:13 +02:00
config_args['numeric_ids'] = numeric_ids
return config_args
2023-05-04 23:16:15 +02:00
2025-03-30 15:02:04 +02:00
def _notify(text: str) -> None:
2023-06-20 19:22:22 +02:00
euid = os.geteuid()
2023-05-25 23:44:59 +02:00
2023-06-20 19:22:22 +02:00
if euid == 0:
2023-05-25 23:44:59 +02:00
uid = os.getenv('SUDO_UID')
else:
2023-06-20 19:22:22 +02:00
uid = euid
if uid is None:
return
2023-05-25 23:44:59 +02:00
os.seteuid(int(uid))
os.environ['DBUS_SESSION_BUS_ADDRESS'] = f'unix:path=/run/user/{uid}/bus'
2025-03-30 15:02:50 +02:00
obj = dbus.SessionBus().get_object('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
2023-05-25 23:44:59 +02:00
obj = dbus.Interface(obj, 'org.freedesktop.Notifications')
obj.Notify('', 0, '', 'simple_backup', text, [], {'urgency': 1}, 10000)
2023-06-20 19:22:22 +02:00
os.seteuid(int(euid))
2023-05-25 23:44:59 +02:00
2025-03-30 15:02:04 +02:00
def simple_backup() -> int:
2023-05-29 23:10:29 +02:00
"""Main"""
2023-05-04 23:16:15 +02:00
args = _parse_arguments()
2023-06-04 10:16:50 +02:00
2023-06-20 19:22:22 +02:00
if args.user:
user = args.user
homedir = os.path.expanduser(f'~{user}')
else:
euid = os.geteuid()
if euid == 0:
user = os.getenv('SUDO_USER')
2025-03-30 14:37:30 +02:00
if user is not None:
homedir = os.path.expanduser(f'~{user}')
else:
2025-03-30 15:02:50 +02:00
logger.warning(
'Failed to detect user. You can use -u/--user parameter to manually specify it')
2025-03-30 14:37:30 +02:00
homedir = None
2023-06-20 19:22:22 +02:00
else:
user = os.getenv('USER')
homedir = os.getenv('HOME')
if homedir is None:
homedir = ''
2023-06-04 10:16:50 +02:00
if args.no_syslog:
try:
logger.removeHandler(j_handler)
except NameError:
pass
2023-06-15 09:30:59 +02:00
try:
2023-06-20 19:22:22 +02:00
config_args = _read_config(args.config, user)
2023-06-15 09:30:59 +02:00
except (configparser.NoSectionError, configparser.NoOptionError):
logger.critical('Bad configuration file')
2024-09-28 09:47:33 +02:00
2023-06-20 19:22:22 +02:00
return 6
2023-05-04 23:16:15 +02:00
2023-06-16 17:42:13 +02:00
inputs = args.inputs if args.inputs is not None else config_args['inputs']
output = args.output if args.output is not None else config_args['output']
exclude = args.exclude if args.exclude is not None else config_args['exclude']
keep = args.keep if args.keep is not None else config_args['keep']
2023-06-20 17:36:12 +02:00
ssh_host = args.ssh_host if args.ssh_host is not None else config_args['ssh_host']
ssh_user = args.ssh_user if args.ssh_user is not None else config_args['ssh_user']
2023-06-16 17:42:13 +02:00
ssh_keyfile = args.keyfile if args.keyfile is not None else config_args['ssh_keyfile']
2023-06-25 10:12:07 +02:00
remote_sudo = args.remote_sudo or config_args['remote_sudo']
2023-05-28 21:30:40 +02:00
2023-06-15 21:30:43 +02:00
if args.rsync_options is None:
2025-03-30 15:02:50 +02:00
rsync_options = ['-a', '-r', '-v', '-h', '-H',
'-X', '-s', '--ignore-missing-args', '--mkpath']
2023-05-04 23:16:15 +02:00
else:
2023-06-15 22:10:19 +02:00
rsync_options = ['-r', '-v']
2023-06-15 21:30:43 +02:00
for ro in args.rsync_options:
2023-06-15 22:10:19 +02:00
rsync_options.append(f'-{ro}')
2023-05-29 18:33:02 +02:00
2023-05-04 23:16:15 +02:00
if args.checksum:
2023-06-15 22:10:19 +02:00
rsync_options.append('-c')
2023-05-29 18:33:02 +02:00
if args.compress:
2023-06-15 22:10:19 +02:00
rsync_options.append('-z')
2023-05-29 18:33:02 +02:00
2023-06-16 17:42:13 +02:00
if args.numeric_ids or config_args['numeric_ids']:
2023-06-16 16:18:12 +02:00
rsync_options.append('--numeric-ids')
2023-06-15 22:10:19 +02:00
rsync_options = ' '.join(rsync_options)
2023-05-04 23:16:15 +02:00
2023-06-20 17:36:12 +02:00
backup = Backup(inputs, output, exclude, keep, rsync_options, ssh_host, ssh_user, ssh_keyfile,
2023-06-25 11:49:02 +02:00
remote_sudo, remove_before=args.remove_before_backup, verbose=args.verbose)
2023-05-04 23:16:15 +02:00
2023-06-20 19:22:22 +02:00
return_code = backup.check_params(homedir)
2023-06-15 16:58:56 +02:00
if return_code == 0:
2023-05-28 21:30:40 +02:00
return backup.run()
2023-05-04 23:16:15 +02:00
2023-06-15 16:58:56 +02:00
return return_code
2023-05-04 23:16:15 +02:00
if __name__ == '__main__':
simple_backup()