Add modules for basic functions

This commit is contained in:
Eric Torres 2019-03-12 17:47:25 -07:00
parent a3717ce355
commit afa4eb9975
4 changed files with 85 additions and 0 deletions

0
rbackup/__init__.py Normal file
View File

12
rbackup/date.py Normal file
View File

@ -0,0 +1,12 @@
import datetime
import os.path
def gen_datetime(*paths):
"""Generate a path for a backup directory that contains
the current date and time.
:param paths: paths to prepend to the result
:type paths: str, bytes, or path-like object
:returns: stuff
"""
return os.path.join(*paths, datetime.datetime.utcnow().isoformat())

56
rbackup/hierarchy.py Normal file
View File

@ -0,0 +1,56 @@
"""This module contains a class for creating a backup hierarchy."""
import os.path
class Hierarchy():
dest = None
def Hierarchy(self, dest):
"""Default constructor for the hierarchy class.
:param dest: the root directory of the backup hierarchy
:type dest: str, bytes, or path-like object
"""
if not os.path.isdir(dest):
raise ValueError(f"{dest} is not a valid directory")
self.dest = dest
@property
def etc_dir:
"""Retrieve the /etc backup directory of this hierarchy.
:rtype: str
"""
return os.path.join(self.dest, backup, etc)
@property
def home_dir:
"""Retrieve the /home backup directory of this hierarchy.
:rtype: str
"""
return os.path.join(self.dest, backup, home)
@property
def snapshot_dir:
"""Retrieve the snapshot directory of this hierarchy.
:rtype: str
"""
return os.path.join(self.dest, backup, snapshots)
@property
def root_home_dir:
"""Retrieve root's home directory of this hierarchy.
:rtype: str
"""
return os.path.join(self.dest, backup, root)
@property
def base_dir:
"""Return the base directory of this hierarchy.
:rtype: str
"""
return dest

17
rbackup/rsync.py Normal file
View File

@ -0,0 +1,17 @@
import subprocess
# ========== Constants ==========
_RSYNC_BIN = '/usr/bin/rsync'
# ========== Functions ==========
def rsync(*args):
"""Run an rsync command.
:param args: all arguments to pass to rsync
:type args: str
:raises: subprocess.CalledProcessError if rsync process failed
"""
cmd = [_RSYNC_BIN, *args]
subprocess.run(cmd, capture_output=True, check=True)