Skip to content

Commit

Permalink
Merge pull request #10 from bartkl/feature/push-pull-option
Browse files Browse the repository at this point in the history
Push/pull configuration per module
  • Loading branch information
bartkl authored Nov 20, 2021
2 parents 6441680 + 4a13243 commit 6a53d89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ What it does for me:
* Configure different rsync calls in a human readable fashion through the `config.ini` file.
* Maintain the base parameters I wish to include for all rsync calls in a central place (the `rsync` section).
* It let's me define config blocks for each rsync call (module), in which I supply the local path and specific parameters. The path can make use of a shared base path you can set in the `rsync` config block.
* Using the `transfer direction` option, I can configure per module whether I wish to pull from the host, or push to it.
* It can easily be configured to log to file, but I have it configured to log to stdout when run manually, and to `journald` otherwise (see _Scheduling with journal logging_).
- To make it log to file, simply provide a base option `--log-file <FILEPATH>` in the `rsync` section.
* When backing up manually, it allows intuitive usage by simply stating the module name(s) you wish to back up, and that's it.
Expand Down Expand Up @@ -46,6 +47,7 @@ The config file contains:

Fields:
- `path`: The path of the source files to be synced _(Required)_.
- `transfer direction`: Either `push` or `pull`. Determines whether files for this module are sent to or received from the configured host _(Required)_.
- `opts`: Whitespace separated string of Rsync options that will be used (on top of the base options above) for this module specifically.

You can define your own fields for re-use later in the file as well. This is particularly useful when repeating something often, like a base path.
Expand All @@ -55,9 +57,9 @@ See the example below which demonstrates what's been layed out here. It's a vali
### Example
```ini
[rsync]
host = me@backup-host
host = me@host
password file = /home/me/.config/backup/rsync_password
source base path = /media/backup
source base path = /media/
base opts =
--itemize-changes
--verbose
Expand All @@ -66,17 +68,15 @@ base opts =
--delete
--password-file=${password file}

[module: books]
path = ${rsync:source base path}/data/books
[module: stuff]
transfer direction = pull
path = ${rsync:source base path}/data/stuff
opts = --partial --inplace

[module: chat-logs]
path = ${rsync:source base path}/data/chat-logs
[module: links]
transfer direction = push
path = ${rsync:source base path}/data/.torrents
opts = --whole-file --no-links

[module: photos]
path = ${rsync:source base path}/photos
opts = --whole-file
```


Expand Down
9 changes: 7 additions & 2 deletions backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ def backup_module(self, module, dry=False):

base_opts = rsync_config.getopts('base opts', '')
opts = module_config.getopts('opts', '')
transfer_direction = module_config['transfer direction']

logger.info(f'Backing up module {module} ({transfer_direction}ing)...')
if transfer_direction == "push":
rsync_cmd = shlex.split(f'rsync {base_opts} {opts} {path.absolute()}{os.sep} {host}::{module}')
elif transfer_direction == "pull":
rsync_cmd = shlex.split(f'rsync {base_opts} {opts} {host}::{module} {path.absolute()}{os.sep}')

rsync_cmd = shlex.split(f'rsync {base_opts} {opts} {path.absolute()}{os.sep} {host}::{module}')
logger.info(f'Backing up module {module}...')
logger.info(' '.join(rsync_cmd))

if dry:
Expand Down
2 changes: 1 addition & 1 deletion backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def get_config(config_file):
if not 'rsync' in config:
raise InvalidConfigError('Please provide a `[rsync]` config section.')

return config
return config
21 changes: 21 additions & 0 deletions config.example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[rsync]
host = me@host
password file = /home/me/.config/backup/rsync_password
source base path = /media/
base opts =
--itemize-changes
--verbose
--archive
--update
--delete
--password-file=${password file}

[module: stuff]
transfer direction = pull
path = ${rsync:source base path}/data/stuff
opts = --partial --inplace

[module: links]
transfer direction = push
path = ${rsync:source base path}/data/.torrents
opts = --whole-file --no-links
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
setup(name='Backup',
author='Bart Kleijngeld',
author_email='[email protected]',
version='1.0.1',
version='2.0.0',
license='MIT',
description='Backup script which provides an easily configurable rsync based solution.',
url='https://github.com/bartkl/backup',
packages=find_packages(),
install_requires=[''],
entry_points={'console_scripts': [
'backup = backup:cli']}
)
)

0 comments on commit 6a53d89

Please sign in to comment.