Backup Utilities

All of the following modules are used for creating backups for files in as painless a way as possible.

backup_nt_and_posix

Backup a directory by appending the date and time and copying over.

Backup NT and Posix

Motivation

This script aims to be platform agnostic and in the long term will be used on Windows, Linux, Mac and Android systems.

pyutil.backup_nt_and_posix.timestamped_dir(backup_dir, path='.')[source]

Create a backup of a directory. Append date and time to new dir name.

Todo

Change this so that it utilizes subprocess.check_call() so we handle return codes in a better way.

Parameters
  • backup_dir (str) – Directory to backup

  • path (str, optional) – Directory to back up to. Defaults to cwd.

Returns

  • err_code (int) – Non-zero value indicates error code, or zero on success.

  • err_msg (str or None) – Human readable error message, or None on success.

rclone

Backup files using rclone.

rclone

Requires

rclone, a Golang package.

  • Set up a simple single use case backup.
    • Realistically this should be more of the focus.

  • However if it’s not, then we could make a collections.defaultdict that holds default values for each option. * Actually wouldn’t configparser.ConfigParser make more sense?

  • In addition we could use collections.ChainMap() to set precedence of backupdir.

  • Expand argparse usage with argparse.fromfile_prefix_chars() to emulate rsync’s file input.

class pyutil.rclone.CloudProvider[source]

Bases: object

Emulate the provider rclone is syncing to.

property url[source]
pyutil.rclone._dir_checker(dir_)[source]

Check that necessary directories exist.

If the default dst doesn’t exist, definitely create it. If the user provided src doesn’t exist, crash without making one.

It’s more likely that they typed the src dir incorrectly rather than running the script aware of the fact that it is nonexistent.

pyutil.rclone._parse_arguments(cwd=None, **kwargs)[source]

Parse user-given arguments.

pyutil.rclone._set_debugging()[source]

Enable debug logging.

pyutil.rclone.main()[source]

Receive user arguments and begin executing module appropriately.

pyutil.rclone.rclone_base_case(src, dst)[source]

Base case that all other functions build off of.

This function shouldn’t be executed directly; however, it serves as a good template detailing a function and useful command with parameters that rclone uses.

For example, --follow is a flag that has conditionals associated it with it.

There are situations in which one wants to follow symlinks and others that they don’t.

This command assumes a use case and configures it rclone for it properly.

Todo

rclone takes an argument for user-agent

Parameters
  • src (str) – directory to clone files from

  • dst (str) – destination to send files to. Can be configured as a local directory, a dropbox directory, a google drive folder or a google cloud storage bucket among many other things.

pyutil.rclone.rclone_follow(dst, src)[source]

Follow symlinks.

Parameters
  • src (str) – directory to clone files from

  • dst (str) – destination to send files to. Can be configured as a local directory, a dropbox directory, a google drive folder or a google cloud storage bucket among many other things.

pyutil.rclone.run(cmd, **kwargs)[source]

Execute the required command in a subshell.

First the command is splited used typical shell grammer.

A new process is created, and from the resulting subprocess object, the subprocess.Popen().wait().

This function returns the return code of split cmd, so any non-zero value will lead to a SystemExit with a passed value of returncode.

Parameters

cmd (str) – The command to be called

Returns

process.returncode – The returncode from the process.

Return type

int