mirror of
https://github.com/borgmatic-collective/borgmatic.git
synced 2026-08-04 23:43:01 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b22b552bf3 | ||
|
|
938392b25b | ||
|
|
481dbc14c3 | ||
|
|
600c438951 | ||
|
|
2e3e68d2cb | ||
|
|
331adca23e | ||
|
|
e1e5db22f8 | ||
|
|
377e3948ff | ||
|
|
4533fec167 | ||
|
|
633700c0af | ||
|
|
007b3e6d4e | ||
|
|
175761c757 | ||
|
|
c7e23fe9ed | ||
|
|
9e45da75cb | ||
|
|
0ea5824427 | ||
|
|
5b66dc69a1 | ||
|
|
8210172d7f | ||
|
|
82e8dae948 | ||
|
|
fa87aed263 | ||
|
|
c3b4cb21ed | ||
|
|
030b321e39 | ||
|
|
15bf273e6e | ||
|
|
cf545ae93a | ||
|
|
45a2b9cded | ||
|
|
953d08ba63 | ||
|
|
88da0c3039 | ||
|
|
0012e0cdea | ||
|
|
049f9c8853 | ||
|
|
31482ee559 | ||
|
|
1ffff3255a | ||
|
|
9e52be6ffd | ||
|
|
978096b402 | ||
|
|
cc6aa7af05 | ||
|
|
32858fb0b4 | ||
|
|
e59845d4e1 | ||
|
|
9437e95849 | ||
|
|
3a3851d2a5 | ||
|
|
80318e6e30 | ||
|
|
6756ca8311 | ||
|
|
fa7955b8cf | ||
|
|
944c0212c3 | ||
|
|
2456fc67f1 | ||
|
|
8a58b72934 | ||
|
|
6dc0173b74 | ||
|
|
5c58f85be1 | ||
|
|
3a9e32a411 | ||
|
|
30f6ec4f7d | ||
|
|
c67ab09e4d | ||
|
|
5299046b6b | ||
|
|
204e515bf7 | ||
|
|
1334da99e2 | ||
|
|
996ca19dac | ||
|
|
61969d17a2 | ||
|
|
d041e23d35 | ||
|
|
e996e09657 | ||
|
|
9c06874073 | ||
|
|
f5e0e10143 | ||
|
|
952a691f60 | ||
|
|
f94181480c | ||
|
|
c27b4a3497 |
@@ -0,0 +1,6 @@
|
|||||||
|
*.egg-info
|
||||||
|
*.pyc
|
||||||
|
*.swp
|
||||||
|
.tox
|
||||||
|
build
|
||||||
|
dist
|
||||||
@@ -14,3 +14,17 @@ cf4c7065f0711deda1cba878398bc05390e2c3f9 0.0.7
|
|||||||
ac5dfa01e9d14d09845f5e94c2c679e21c5eb2f9 0.1.1
|
ac5dfa01e9d14d09845f5e94c2c679e21c5eb2f9 0.1.1
|
||||||
ac5dfa01e9d14d09845f5e94c2c679e21c5eb2f9 0.1.1
|
ac5dfa01e9d14d09845f5e94c2c679e21c5eb2f9 0.1.1
|
||||||
7b6c87dca7ea312b2257ac1b46857b3f8c56b39c 0.1.1
|
7b6c87dca7ea312b2257ac1b46857b3f8c56b39c 0.1.1
|
||||||
|
83067f995dd391e38544a7722dc3b254b59c5521 0.1.2
|
||||||
|
acc7fb61566fe8028c179f43ecc735c851220b06 0.1.3
|
||||||
|
6dda59c12de88f060eb7244e6d330173985a9639 0.1.4
|
||||||
|
6dda59c12de88f060eb7244e6d330173985a9639 0.1.4
|
||||||
|
e58246fc92bb22c2b2fd8b86a1227de69d2d0315 0.1.4
|
||||||
|
0afff209b902698c2266986129d6dc9f5f913101 0.1.5
|
||||||
|
4c63f3d90ec2bf6af1714a3acec84654a7c9edf3 0.1.6
|
||||||
|
5a458ebef804be14e30d7375e3e9fbc26aedb80d 0.1.7
|
||||||
|
977f19c2f6a515be6c5ef69cf17b0e0989532209 github/yaml_config_files
|
||||||
|
0000000000000000000000000000000000000000 github/yaml_config_files
|
||||||
|
28434dd0440cc8da44c2f3e9bd7e9402a59c3b40 github/master
|
||||||
|
dbc96d3f83bd5570b6826537616d4160b3374836 0.1.8
|
||||||
|
0e1fbee9358de4f062fa9539e1355db83db70caa 1.0.0
|
||||||
|
de2d7721cdec93a52d20222a9ddd579ed93c1017 1.0.1
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ Dan Helfman <witten@torsion.org>: Main developer
|
|||||||
|
|
||||||
Alexander Görtz: Python 3 compatibility
|
Alexander Görtz: Python 3 compatibility
|
||||||
Henning Schroeder: Copy editing
|
Henning Schroeder: Copy editing
|
||||||
|
Robin `ypid` Schneider: Support additional options of Borg
|
||||||
|
|||||||
@@ -1,3 +1,51 @@
|
|||||||
|
1.0.2
|
||||||
|
|
||||||
|
* #20: Fix for traceback when remote_path option is missing.
|
||||||
|
|
||||||
|
1.0.1
|
||||||
|
|
||||||
|
* #19: Support for Borg's --remote-path option to use an alternate Borg
|
||||||
|
executable. See sample/config.
|
||||||
|
|
||||||
|
1.0.0
|
||||||
|
|
||||||
|
* Attic is no longer supported, as there hasn't been any recent development on
|
||||||
|
it. Dropping Attic support will allow faster iteration on Borg-specific
|
||||||
|
features. If you're still using Attic, this is a good time to switch to Borg!
|
||||||
|
* Project renamed from atticmatic to borgmatic. See the borgmatic README for
|
||||||
|
information on upgrading.
|
||||||
|
|
||||||
|
0.1.8
|
||||||
|
|
||||||
|
* Fix for handling of spaces in source_directories which resulted in backup up everything.
|
||||||
|
* Fix for broken links to Borg documentation.
|
||||||
|
* At verbosity zero, suppressing Borg check stderr spew about "Checking segments".
|
||||||
|
* Support for Borg --one-file-system.
|
||||||
|
* Support for Borg create --umask.
|
||||||
|
* Support for file globs in source_directories.
|
||||||
|
|
||||||
|
0.1.7
|
||||||
|
|
||||||
|
* #11: Fixed parsing of punctuation in configuration file.
|
||||||
|
* Better error message when configuration file is missing.
|
||||||
|
|
||||||
|
0.1.6
|
||||||
|
|
||||||
|
* #9: New configuration option for the encryption passphrase.
|
||||||
|
* #10: Support for Borg's new archive compression feature.
|
||||||
|
|
||||||
|
0.1.5
|
||||||
|
|
||||||
|
* Changes to support release on PyPI. Now pip installable by name!
|
||||||
|
|
||||||
|
0.1.4
|
||||||
|
|
||||||
|
* Adding test that setup.py version matches release version.
|
||||||
|
|
||||||
|
0.1.3
|
||||||
|
|
||||||
|
* #1: Add support for "borg check --last N" to Borg backend.
|
||||||
|
|
||||||
0.1.2
|
0.1.2
|
||||||
|
|
||||||
* As a convenience to new users, allow a missing default excludes file.
|
* As a convenience to new users, allow a missing default excludes file.
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
title: Atticmatic
|
title: Borgmatic
|
||||||
date:
|
|
||||||
save_as: atticmatic/index.html
|
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
atticmatic is a simple Python wrapper script for the
|
borgmatic (formerly atticmatic) is a simple Python wrapper script for the
|
||||||
[Attic](https://attic-backup.org/) and
|
[Borg](https://borgbackup.readthedocs.org/en/stable/) backup software that
|
||||||
[Borg](https://borgbackup.github.io/borgbackup/) backup software that
|
|
||||||
initiates a backup, prunes any old backups according to a retention policy,
|
initiates a backup, prunes any old backups according to a retention policy,
|
||||||
and validates backups for consistency. The script supports specifying your
|
and validates backups for consistency. The script supports specifying your
|
||||||
settings in a declarative configuration file rather than having to put them
|
settings in a declarative configuration file rather than having to put them
|
||||||
@@ -14,57 +11,53 @@ all on the command-line, and handles common errors.
|
|||||||
|
|
||||||
Here's an example config file:
|
Here's an example config file:
|
||||||
|
|
||||||
[location]
|
```INI
|
||||||
# Space-separated list of source directories to backup.
|
[location]
|
||||||
source_directories: /home /etc
|
# Space-separated list of source directories to backup.
|
||||||
|
# Globs are expanded.
|
||||||
|
source_directories: /home /etc /var/log/syslog*
|
||||||
|
|
||||||
# Path to local or remote backup repository.
|
# Path to local or remote backup repository.
|
||||||
repository: user@backupserver:sourcehostname.attic
|
repository: user@backupserver:sourcehostname.borg
|
||||||
|
|
||||||
[retention]
|
[retention]
|
||||||
# Retention policy for how many backups to keep in each category.
|
# Retention policy for how many backups to keep in each category.
|
||||||
keep_daily: 7
|
keep_daily: 7
|
||||||
keep_weekly: 4
|
keep_weekly: 4
|
||||||
keep_monthly: 6
|
keep_monthly: 6
|
||||||
|
|
||||||
[consistency]
|
[consistency]
|
||||||
# Consistency checks to run, or "disabled" to prevent checks.
|
# Consistency checks to run, or "disabled" to prevent checks.
|
||||||
checks: repository archives
|
checks: repository archives
|
||||||
|
```
|
||||||
|
|
||||||
Additionally, exclude patterns can be specified in a separate excludes config
|
Additionally, exclude patterns can be specified in a separate excludes config
|
||||||
file, one pattern per line.
|
file, one pattern per line.
|
||||||
|
|
||||||
atticmatic is hosted at <https://torsion.org/atticmatic> with [source code
|
borgmatic is hosted at <https://torsion.org/borgmatic> with [source code
|
||||||
available](https://torsion.org/hg/atticmatic). It's also mirrored on
|
available](https://torsion.org/hg/borgmatic). It's also mirrored on
|
||||||
[GitHub](https://github.com/witten/atticmatic) and
|
[GitHub](https://github.com/witten/borgmatic) and
|
||||||
[BitBucket](https://bitbucket.org/dhelfman/atticmatic) for convenience.
|
[BitBucket](https://bitbucket.org/dhelfman/borgmatic) for convenience.
|
||||||
|
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
To get up and running, follow the [Attic Quick
|
To get up and running, follow the [Borg Quick
|
||||||
Start](https://attic-backup.org/quickstart.html) or the [Borg Quick
|
Start](https://borgbackup.readthedocs.org/en/latest/quickstart.html) to create
|
||||||
Start](https://borgbackup.github.io/borgbackup/quickstart.html) to create a
|
a repository on a local or remote host. Note that if you plan to run
|
||||||
repository on a local or remote host. Note that if you plan to run atticmatic
|
borgmatic on a schedule with cron, and you encrypt your Borg repository with
|
||||||
on a schedule with cron, and you encrypt your attic repository with a
|
a passphrase instead of a key file, you'll need to set the borgmatic
|
||||||
passphrase instead of a key file, you'll need to set the `ATTIC_PASSPHRASE`
|
`encryption_passphrase` configuration variable. See the repository encryption
|
||||||
environment variable. See the repository encryption section of the Quick Start
|
section of the Quick Start for more info.
|
||||||
for more info.
|
|
||||||
|
|
||||||
If the repository is on a remote host, make sure that your local root user has
|
If the repository is on a remote host, make sure that your local root user has
|
||||||
key-based ssh access to the desired user account on the remote host.
|
key-based ssh access to the desired user account on the remote host.
|
||||||
|
|
||||||
To install atticmatic, run the following command to download and install it:
|
To install borgmatic, run the following command to download and install it:
|
||||||
|
|
||||||
sudo pip install --upgrade hg+https://torsion.org/hg/atticmatic
|
sudo pip install --upgrade borgmatic
|
||||||
|
|
||||||
If you are using Attic, copy the following configuration files:
|
Then, copy the following configuration files:
|
||||||
|
|
||||||
sudo cp sample/atticmatic.cron /etc/cron.d/atticmatic
|
|
||||||
sudo mkdir /etc/atticmatic/
|
|
||||||
sudo cp sample/config sample/excludes /etc/atticmatic/
|
|
||||||
|
|
||||||
If you are using Borg, copy the files like this instead:
|
|
||||||
|
|
||||||
sudo cp sample/borgmatic.cron /etc/cron.d/borgmatic
|
sudo cp sample/borgmatic.cron /etc/cron.d/borgmatic
|
||||||
sudo mkdir /etc/borgmatic/
|
sudo mkdir /etc/borgmatic/
|
||||||
@@ -73,16 +66,31 @@ If you are using Borg, copy the files like this instead:
|
|||||||
Lastly, modify the /etc files with your desired configuration.
|
Lastly, modify the /etc files with your desired configuration.
|
||||||
|
|
||||||
|
|
||||||
|
## Upgrading from atticmatic
|
||||||
|
|
||||||
|
You can ignore this section if you're not an atticmatic user (the former name
|
||||||
|
of borgmatic).
|
||||||
|
|
||||||
|
borgmatic only supports Borg now and no longer supports Attic. So if you're
|
||||||
|
an Attic user, consider switching to Borg. See the [Borg upgrade
|
||||||
|
command](https://borgbackup.readthedocs.io/en/stable/usage.html#borg-upgrade)
|
||||||
|
for more information. Then, follow the instructions above about setting up
|
||||||
|
your borgmatic configuration files.
|
||||||
|
|
||||||
|
If you were already using Borg with atticmatic, then you can easily upgrade
|
||||||
|
from atticmatic to borgmatic. Simply run the following commands:
|
||||||
|
|
||||||
|
sudo pip uninstall atticmatic
|
||||||
|
sudo pip install borgmatic
|
||||||
|
|
||||||
|
That's it! borgmatic will continue using your /etc/borgmatic configuration
|
||||||
|
files.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
You can run atticmatic and start a backup simply by invoking it without
|
You can run borgmatic and start a backup simply by invoking it without
|
||||||
arguments:
|
arguments:
|
||||||
|
|
||||||
atticmatic
|
|
||||||
|
|
||||||
Or, if you're using Borg, use this command instead to make use of the Borg
|
|
||||||
backend:
|
|
||||||
|
|
||||||
borgmatic
|
borgmatic
|
||||||
|
|
||||||
This will also prune any old backups as per the configured retention policy,
|
This will also prune any old backups as per the configured retention policy,
|
||||||
@@ -92,15 +100,15 @@ By default, the backup will proceed silently except in the case of errors. But
|
|||||||
if you'd like to to get additional information about the progress of the
|
if you'd like to to get additional information about the progress of the
|
||||||
backup as it proceeds, use the verbosity option:
|
backup as it proceeds, use the verbosity option:
|
||||||
|
|
||||||
atticmattic --verbosity 1
|
borgmatic --verbosity 1
|
||||||
|
|
||||||
Or, for even more progress spew:
|
Or, for even more progress spew:
|
||||||
|
|
||||||
atticmattic --verbosity 2
|
borgmatic --verbosity 2
|
||||||
|
|
||||||
If you'd like to see the available command-line arguments, view the help:
|
If you'd like to see the available command-line arguments, view the help:
|
||||||
|
|
||||||
atticmattic --help
|
borgmatic --help
|
||||||
|
|
||||||
|
|
||||||
## Running tests
|
## Running tests
|
||||||
@@ -118,12 +126,12 @@ Then, to actually run tests, run:
|
|||||||
|
|
||||||
### Broken pipe with remote repository
|
### Broken pipe with remote repository
|
||||||
|
|
||||||
When running atticmatic on a large remote repository, you may receive errors
|
When running borgmatic on a large remote repository, you may receive errors
|
||||||
like the following, particularly while "attic check" is valiating backups for
|
like the following, particularly while "borg check" is validating backups for
|
||||||
consistency:
|
consistency:
|
||||||
|
|
||||||
Write failed: Broken pipe
|
Write failed: Broken pipe
|
||||||
attic: Error: Connection closed by remote host
|
borg: Error: Connection closed by remote host
|
||||||
|
|
||||||
This error can be caused by an ssh timeout, which you can rectify by adding
|
This error can be caused by an ssh timeout, which you can rectify by adding
|
||||||
the following to the ~/.ssh/config file on the client:
|
the following to the ~/.ssh/config file on the client:
|
||||||
@@ -137,7 +145,9 @@ backups.
|
|||||||
|
|
||||||
## Issues and feedback
|
## Issues and feedback
|
||||||
|
|
||||||
Got an issue or an idea for a feature enhancement? Check out the [atticmatic
|
Got an issue or an idea for a feature enhancement? Check out the [borgmatic
|
||||||
issue tracker](https://tree.taiga.io/project/witten-atticmatic/issues).
|
issue tracker](https://tree.taiga.io/project/witten-borgmatic/issues?page=1&status=399951,399952,399955). In
|
||||||
|
order to create a new issue or comment on an issue, you'll need to [login
|
||||||
|
first](https://tree.taiga.io/login).
|
||||||
|
|
||||||
Other questions or comments? Contact <mailto:witten@torsion.org>.
|
Other questions or comments? Contact <mailto:witten@torsion.org>.
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
from functools import partial
|
|
||||||
|
|
||||||
from atticmatic.backends import shared
|
|
||||||
|
|
||||||
# An atticmatic backend that supports Attic for actually handling backups.
|
|
||||||
|
|
||||||
COMMAND = 'attic'
|
|
||||||
|
|
||||||
|
|
||||||
create_archive = partial(shared.create_archive, command=COMMAND)
|
|
||||||
prune_archives = partial(shared.prune_archives, command=COMMAND)
|
|
||||||
check_archives = partial(shared.check_archives, command=COMMAND)
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
from functools import partial
|
|
||||||
|
|
||||||
from atticmatic.backends import shared
|
|
||||||
|
|
||||||
# An atticmatic backend that supports Borg for actually handling backups.
|
|
||||||
|
|
||||||
COMMAND = 'borg'
|
|
||||||
|
|
||||||
|
|
||||||
create_archive = partial(shared.create_archive, command=COMMAND)
|
|
||||||
prune_archives = partial(shared.prune_archives, command=COMMAND)
|
|
||||||
check_archives = partial(shared.check_archives, command=COMMAND)
|
|
||||||
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
from __future__ import print_function
|
|
||||||
from argparse import ArgumentParser
|
|
||||||
from importlib import import_module
|
|
||||||
import os
|
|
||||||
from subprocess import CalledProcessError
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from atticmatic.config import parse_configuration
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_CONFIG_FILENAME_PATTERN = '/etc/{}/config'
|
|
||||||
DEFAULT_EXCLUDES_FILENAME_PATTERN = '/etc/{}/excludes'
|
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments(command_name, *arguments):
|
|
||||||
'''
|
|
||||||
Given the name of the command with which this script was invoked and command-line arguments,
|
|
||||||
parse the arguments and return them as an ArgumentParser instance. Use the command name to
|
|
||||||
determine the default configuration and excludes paths.
|
|
||||||
'''
|
|
||||||
config_filename_default = DEFAULT_CONFIG_FILENAME_PATTERN.format(command_name)
|
|
||||||
excludes_filename_default = DEFAULT_EXCLUDES_FILENAME_PATTERN.format(command_name)
|
|
||||||
|
|
||||||
parser = ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
'-c', '--config',
|
|
||||||
dest='config_filename',
|
|
||||||
default=config_filename_default,
|
|
||||||
help='Configuration filename',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--excludes',
|
|
||||||
dest='excludes_filename',
|
|
||||||
default=excludes_filename_default if os.path.exists(excludes_filename_default) else None,
|
|
||||||
help='Excludes filename',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-v', '--verbosity',
|
|
||||||
type=int,
|
|
||||||
help='Display verbose progress (1 for some, 2 for lots)',
|
|
||||||
)
|
|
||||||
|
|
||||||
return parser.parse_args(arguments)
|
|
||||||
|
|
||||||
|
|
||||||
def load_backend(command_name):
|
|
||||||
'''
|
|
||||||
Given the name of the command with which this script was invoked, return the corresponding
|
|
||||||
backend module responsible for actually dealing with backups.
|
|
||||||
'''
|
|
||||||
backend_name = {
|
|
||||||
'atticmatic': 'attic',
|
|
||||||
'borgmatic': 'borg',
|
|
||||||
}.get(command_name, 'attic')
|
|
||||||
|
|
||||||
return import_module('atticmatic.backends.{}'.format(backend_name))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
try:
|
|
||||||
command_name = os.path.basename(sys.argv[0])
|
|
||||||
args = parse_arguments(command_name, *sys.argv[1:])
|
|
||||||
config = parse_configuration(args.config_filename)
|
|
||||||
repository = config.location['repository']
|
|
||||||
backend = load_backend(command_name)
|
|
||||||
|
|
||||||
backend.create_archive(args.excludes_filename, args.verbosity, **config.location)
|
|
||||||
backend.prune_archives(args.verbosity, repository, config.retention)
|
|
||||||
backend.check_archives(args.verbosity, repository, config.consistency)
|
|
||||||
except (ValueError, IOError, CalledProcessError) as error:
|
|
||||||
print(error, file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from flexmock import flexmock
|
|
||||||
|
|
||||||
from atticmatic.backends import shared as module
|
|
||||||
from atticmatic.tests.builtins import builtins_mock
|
|
||||||
from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
|
||||||
|
|
||||||
|
|
||||||
def insert_subprocess_mock(check_call_command, **kwargs):
|
|
||||||
subprocess = flexmock()
|
|
||||||
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
|
|
||||||
flexmock(module).subprocess = subprocess
|
|
||||||
|
|
||||||
|
|
||||||
def insert_subprocess_never():
|
|
||||||
subprocess = flexmock()
|
|
||||||
subprocess.should_receive('check_call').never()
|
|
||||||
flexmock(module).subprocess = subprocess
|
|
||||||
|
|
||||||
|
|
||||||
def insert_platform_mock():
|
|
||||||
flexmock(module.platform).should_receive('node').and_return('host')
|
|
||||||
|
|
||||||
|
|
||||||
def insert_datetime_mock():
|
|
||||||
flexmock(module).datetime = flexmock().should_receive('now').and_return(
|
|
||||||
flexmock().should_receive('isoformat').and_return('now').mock
|
|
||||||
).mock
|
|
||||||
|
|
||||||
|
|
||||||
CREATE_COMMAND_WITHOUT_EXCLUDES = ('attic', 'create', 'repo::host-now', 'foo', 'bar')
|
|
||||||
CREATE_COMMAND = CREATE_COMMAND_WITHOUT_EXCLUDES + ('--exclude-from', 'excludes')
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_archive_should_call_attic_with_parameters():
|
|
||||||
insert_subprocess_mock(CREATE_COMMAND)
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.create_archive(
|
|
||||||
excludes_filename='excludes',
|
|
||||||
verbosity=None,
|
|
||||||
source_directories='foo bar',
|
|
||||||
repository='repo',
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_archive_with_none_excludes_filename_should_call_attic_without_excludes():
|
|
||||||
insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES)
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.create_archive(
|
|
||||||
excludes_filename=None,
|
|
||||||
verbosity=None,
|
|
||||||
source_directories='foo bar',
|
|
||||||
repository='repo',
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter():
|
|
||||||
insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.create_archive(
|
|
||||||
excludes_filename='excludes',
|
|
||||||
verbosity=VERBOSITY_SOME,
|
|
||||||
source_directories='foo bar',
|
|
||||||
repository='repo',
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_archive_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
|
||||||
insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.create_archive(
|
|
||||||
excludes_filename='excludes',
|
|
||||||
verbosity=VERBOSITY_LOTS,
|
|
||||||
source_directories='foo bar',
|
|
||||||
repository='repo',
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
BASE_PRUNE_FLAGS = (
|
|
||||||
('--keep-daily', '1'),
|
|
||||||
('--keep-weekly', '2'),
|
|
||||||
('--keep-monthly', '3'),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_make_prune_flags_should_return_flags_from_config():
|
|
||||||
retention_config = OrderedDict(
|
|
||||||
(
|
|
||||||
('keep_daily', 1),
|
|
||||||
('keep_weekly', 2),
|
|
||||||
('keep_monthly', 3),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
result = module._make_prune_flags(retention_config)
|
|
||||||
|
|
||||||
assert tuple(result) == BASE_PRUNE_FLAGS
|
|
||||||
|
|
||||||
|
|
||||||
PRUNE_COMMAND = (
|
|
||||||
'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_prune_archives_should_call_attic_with_parameters():
|
|
||||||
retention_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
|
||||||
BASE_PRUNE_FLAGS,
|
|
||||||
)
|
|
||||||
insert_subprocess_mock(PRUNE_COMMAND)
|
|
||||||
|
|
||||||
module.prune_archives(
|
|
||||||
verbosity=None,
|
|
||||||
repository='repo',
|
|
||||||
retention_config=retention_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_parameter():
|
|
||||||
retention_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
|
||||||
BASE_PRUNE_FLAGS,
|
|
||||||
)
|
|
||||||
insert_subprocess_mock(PRUNE_COMMAND + ('--stats',))
|
|
||||||
|
|
||||||
module.prune_archives(
|
|
||||||
repository='repo',
|
|
||||||
verbosity=VERBOSITY_SOME,
|
|
||||||
retention_config=retention_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
|
||||||
retention_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
|
||||||
BASE_PRUNE_FLAGS,
|
|
||||||
)
|
|
||||||
insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
|
|
||||||
|
|
||||||
module.prune_archives(
|
|
||||||
repository='repo',
|
|
||||||
verbosity=VERBOSITY_LOTS,
|
|
||||||
retention_config=retention_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_checks_returns_them_as_tuple():
|
|
||||||
checks = module._parse_checks({'checks': 'foo disabled bar'})
|
|
||||||
|
|
||||||
assert checks == ('foo', 'bar')
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_checks_with_missing_value_returns_defaults():
|
|
||||||
checks = module._parse_checks({})
|
|
||||||
|
|
||||||
assert checks == module.DEFAULT_CHECKS
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_checks_with_blank_value_returns_defaults():
|
|
||||||
checks = module._parse_checks({'checks': ''})
|
|
||||||
|
|
||||||
assert checks == module.DEFAULT_CHECKS
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_checks_with_disabled_returns_no_checks():
|
|
||||||
checks = module._parse_checks({'checks': 'disabled'})
|
|
||||||
|
|
||||||
assert checks == ()
|
|
||||||
|
|
||||||
|
|
||||||
def test_make_check_flags_with_checks_returns_flags():
|
|
||||||
flags = module._make_check_flags(('foo', 'bar'))
|
|
||||||
|
|
||||||
assert flags == ('--foo-only', '--bar-only')
|
|
||||||
|
|
||||||
|
|
||||||
def test_make_check_flags_with_default_checks_returns_no_flags():
|
|
||||||
flags = module._make_check_flags(module.DEFAULT_CHECKS)
|
|
||||||
|
|
||||||
assert flags == ()
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_archives_should_call_attic_with_parameters():
|
|
||||||
consistency_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
||||||
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
||||||
stdout = flexmock()
|
|
||||||
insert_subprocess_mock(
|
|
||||||
('attic', 'check', 'repo'),
|
|
||||||
stdout=stdout,
|
|
||||||
)
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
builtins_mock().should_receive('open').and_return(stdout)
|
|
||||||
flexmock(module.os).should_receive('devnull')
|
|
||||||
|
|
||||||
module.check_archives(
|
|
||||||
verbosity=None,
|
|
||||||
repository='repo',
|
|
||||||
consistency_config=consistency_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
|
|
||||||
consistency_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
||||||
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
||||||
insert_subprocess_mock(
|
|
||||||
('attic', 'check', 'repo', '--verbose'),
|
|
||||||
stdout=None,
|
|
||||||
)
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.check_archives(
|
|
||||||
verbosity=VERBOSITY_SOME,
|
|
||||||
repository='repo',
|
|
||||||
consistency_config=consistency_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
|
||||||
consistency_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
||||||
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
||||||
insert_subprocess_mock(
|
|
||||||
('attic', 'check', 'repo', '--verbose'),
|
|
||||||
stdout=None,
|
|
||||||
)
|
|
||||||
insert_platform_mock()
|
|
||||||
insert_datetime_mock()
|
|
||||||
|
|
||||||
module.check_archives(
|
|
||||||
verbosity=VERBOSITY_LOTS,
|
|
||||||
repository='repo',
|
|
||||||
consistency_config=consistency_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_archives_without_any_checks_should_bail():
|
|
||||||
consistency_config = flexmock()
|
|
||||||
flexmock(module).should_receive('_parse_checks').and_return(())
|
|
||||||
insert_subprocess_never()
|
|
||||||
|
|
||||||
module.check_archives(
|
|
||||||
verbosity=None,
|
|
||||||
repository='repo',
|
|
||||||
consistency_config=consistency_config,
|
|
||||||
command='attic',
|
|
||||||
)
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
from flexmock import flexmock
|
|
||||||
|
|
||||||
from atticmatic import command as module
|
|
||||||
|
|
||||||
|
|
||||||
def test_load_backend_with_atticmatic_command_should_return_attic_backend():
|
|
||||||
backend = flexmock()
|
|
||||||
(
|
|
||||||
flexmock(module).should_receive('import_module').with_args('atticmatic.backends.attic')
|
|
||||||
.and_return(backend).once()
|
|
||||||
)
|
|
||||||
|
|
||||||
assert module.load_backend('atticmatic') == backend
|
|
||||||
|
|
||||||
|
|
||||||
def test_load_backend_with_unknown_command_should_return_attic_backend():
|
|
||||||
backend = flexmock()
|
|
||||||
(
|
|
||||||
flexmock(module).should_receive('import_module').with_args('atticmatic.backends.attic')
|
|
||||||
.and_return(backend).once()
|
|
||||||
)
|
|
||||||
|
|
||||||
assert module.load_backend('unknownmatic') == backend
|
|
||||||
|
|
||||||
|
|
||||||
def test_load_backend_with_borgmatic_command_should_return_borg_backend():
|
|
||||||
backend = flexmock()
|
|
||||||
(
|
|
||||||
flexmock(module).should_receive('import_module').with_args('atticmatic.backends.borg')
|
|
||||||
.and_return(backend).once()
|
|
||||||
)
|
|
||||||
|
|
||||||
assert module.load_backend('borgmatic') == backend
|
|
||||||
@@ -1,24 +1,45 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from glob import glob
|
||||||
|
from itertools import chain
|
||||||
|
|
||||||
from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
||||||
|
|
||||||
|
|
||||||
# Common backend functionality shared by Attic and Borg. As the two backup
|
# Integration with Borg for actually handling backups.
|
||||||
# commands diverge, these shared functions will likely need to be replaced
|
|
||||||
# with non-shared functions within atticmatic.backends.attic and
|
|
||||||
# atticmatic.backends.borg.
|
|
||||||
|
|
||||||
|
|
||||||
def create_archive(excludes_filename, verbosity, source_directories, repository, command):
|
COMMAND = 'borg'
|
||||||
|
|
||||||
|
|
||||||
|
def initialize(storage_config, command=COMMAND):
|
||||||
|
passphrase = storage_config.get('encryption_passphrase')
|
||||||
|
|
||||||
|
if passphrase:
|
||||||
|
os.environ['{}_PASSPHRASE'.format(command.upper())] = passphrase
|
||||||
|
|
||||||
|
|
||||||
|
def create_archive(
|
||||||
|
excludes_filename, verbosity, storage_config, source_directories, repository, command=COMMAND,
|
||||||
|
one_file_system=None, remote_path=None,
|
||||||
|
):
|
||||||
'''
|
'''
|
||||||
Given an excludes filename (or None), a vebosity flag, a space-separated list of source
|
Given an excludes filename (or None), a vebosity flag, a storage config dict, a space-separated
|
||||||
directories, a local or remote repository path, and a command to run, create an attic archive.
|
list of source directories, a local or remote repository path, and a command to run, create an
|
||||||
|
attic archive.
|
||||||
'''
|
'''
|
||||||
sources = tuple(source_directories.split(' '))
|
sources = re.split('\s+', source_directories)
|
||||||
|
sources = tuple(chain.from_iterable(glob(x) or [x] for x in sources))
|
||||||
exclude_flags = ('--exclude-from', excludes_filename) if excludes_filename else ()
|
exclude_flags = ('--exclude-from', excludes_filename) if excludes_filename else ()
|
||||||
|
compression = storage_config.get('compression', None)
|
||||||
|
compression_flags = ('--compression', compression) if compression else ()
|
||||||
|
umask = storage_config.get('umask', None)
|
||||||
|
umask_flags = ('--umask', str(umask)) if umask else ()
|
||||||
|
one_file_system_flags = ('--one-file-system',) if one_file_system else ()
|
||||||
|
remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
|
||||||
verbosity_flags = {
|
verbosity_flags = {
|
||||||
VERBOSITY_SOME: ('--stats',),
|
VERBOSITY_SOME: ('--stats',),
|
||||||
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
||||||
@@ -31,7 +52,8 @@ def create_archive(excludes_filename, verbosity, source_directories, repository,
|
|||||||
hostname=platform.node(),
|
hostname=platform.node(),
|
||||||
timestamp=datetime.now().isoformat(),
|
timestamp=datetime.now().isoformat(),
|
||||||
),
|
),
|
||||||
) + sources + exclude_flags + verbosity_flags
|
) + sources + exclude_flags + compression_flags + one_file_system_flags + \
|
||||||
|
remote_path_flags + umask_flags + verbosity_flags
|
||||||
|
|
||||||
subprocess.check_call(full_command)
|
subprocess.check_call(full_command)
|
||||||
|
|
||||||
@@ -58,12 +80,13 @@ def _make_prune_flags(retention_config):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def prune_archives(verbosity, repository, retention_config, command):
|
def prune_archives(verbosity, repository, retention_config, command=COMMAND, remote_path=None):
|
||||||
'''
|
'''
|
||||||
Given a verbosity flag, a local or remote repository path, a retention config dict, and a
|
Given a verbosity flag, a local or remote repository path, a retention config dict, and a
|
||||||
command to run, prune attic archives according the the retention policy specified in that
|
command to run, prune attic archives according the the retention policy specified in that
|
||||||
configuration.
|
configuration.
|
||||||
'''
|
'''
|
||||||
|
remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
|
||||||
verbosity_flags = {
|
verbosity_flags = {
|
||||||
VERBOSITY_SOME: ('--stats',),
|
VERBOSITY_SOME: ('--stats',),
|
||||||
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
VERBOSITY_LOTS: ('--verbose', '--stats'),
|
||||||
@@ -76,7 +99,7 @@ def prune_archives(verbosity, repository, retention_config, command):
|
|||||||
element
|
element
|
||||||
for pair in _make_prune_flags(retention_config)
|
for pair in _make_prune_flags(retention_config)
|
||||||
for element in pair
|
for element in pair
|
||||||
) + verbosity_flags
|
) + remote_path_flags + verbosity_flags
|
||||||
|
|
||||||
subprocess.check_call(full_command)
|
subprocess.check_call(full_command)
|
||||||
|
|
||||||
@@ -110,7 +133,7 @@ def _parse_checks(consistency_config):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _make_check_flags(checks):
|
def _make_check_flags(checks, check_last=None):
|
||||||
'''
|
'''
|
||||||
Given a parsed sequence of checks, transform it into tuple of command-line flags.
|
Given a parsed sequence of checks, transform it into tuple of command-line flags.
|
||||||
|
|
||||||
@@ -119,18 +142,22 @@ def _make_check_flags(checks):
|
|||||||
('repository',)
|
('repository',)
|
||||||
|
|
||||||
This will be returned as:
|
This will be returned as:
|
||||||
|
|
||||||
('--repository-only',)
|
('--repository-only',)
|
||||||
|
|
||||||
|
Additionally, if a check_last value is given, a "--last" flag will be added. Note that only
|
||||||
|
Borg supports this flag.
|
||||||
'''
|
'''
|
||||||
|
last_flag = ('--last', check_last) if check_last else ()
|
||||||
if checks == DEFAULT_CHECKS:
|
if checks == DEFAULT_CHECKS:
|
||||||
return ()
|
return last_flag
|
||||||
|
|
||||||
return tuple(
|
return tuple(
|
||||||
'--{}-only'.format(check) for check in checks
|
'--{}-only'.format(check) for check in checks
|
||||||
)
|
) + last_flag
|
||||||
|
|
||||||
|
|
||||||
def check_archives(verbosity, repository, consistency_config, command):
|
def check_archives(verbosity, repository, consistency_config, command=COMMAND, remote_path=None):
|
||||||
'''
|
'''
|
||||||
Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
|
Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
|
||||||
command to run, check the contained attic archives for consistency.
|
command to run, check the contained attic archives for consistency.
|
||||||
@@ -138,9 +165,11 @@ def check_archives(verbosity, repository, consistency_config, command):
|
|||||||
If there are no consistency checks to run, skip running them.
|
If there are no consistency checks to run, skip running them.
|
||||||
'''
|
'''
|
||||||
checks = _parse_checks(consistency_config)
|
checks = _parse_checks(consistency_config)
|
||||||
|
check_last = consistency_config.get('check_last', None)
|
||||||
if not checks:
|
if not checks:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
|
||||||
verbosity_flags = {
|
verbosity_flags = {
|
||||||
VERBOSITY_SOME: ('--verbose',),
|
VERBOSITY_SOME: ('--verbose',),
|
||||||
VERBOSITY_LOTS: ('--verbose',),
|
VERBOSITY_LOTS: ('--verbose',),
|
||||||
@@ -149,9 +178,9 @@ def check_archives(verbosity, repository, consistency_config, command):
|
|||||||
full_command = (
|
full_command = (
|
||||||
command, 'check',
|
command, 'check',
|
||||||
repository,
|
repository,
|
||||||
) + _make_check_flags(checks) + verbosity_flags
|
) + _make_check_flags(checks, check_last) + remote_path_flags + verbosity_flags
|
||||||
|
|
||||||
# The check command spews to stdout even without the verbose flag. Suppress it.
|
# The check command spews to stdout/stderr even without the verbose flag. Suppress it.
|
||||||
stdout = None if verbosity_flags else open(os.devnull, 'w')
|
stdout = None if verbosity_flags else open(os.devnull, 'w')
|
||||||
|
|
||||||
subprocess.check_call(full_command, stdout=stdout)
|
subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT)
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
import os
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from borgmatic import borg
|
||||||
|
from borgmatic.config import parse_configuration, CONFIG_FORMAT
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_CONFIG_FILENAME = '/etc/borgmatic/config'
|
||||||
|
DEFAULT_EXCLUDES_FILENAME = '/etc/borgmatic/excludes'
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments(*arguments):
|
||||||
|
'''
|
||||||
|
Given the name of the command with which this script was invoked and command-line arguments,
|
||||||
|
parse the arguments and return them as an ArgumentParser instance. Use the command name to
|
||||||
|
determine the default configuration and excludes paths.
|
||||||
|
'''
|
||||||
|
parser = ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
'-c', '--config',
|
||||||
|
dest='config_filename',
|
||||||
|
default=DEFAULT_CONFIG_FILENAME,
|
||||||
|
help='Configuration filename',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--excludes',
|
||||||
|
dest='excludes_filename',
|
||||||
|
default=DEFAULT_EXCLUDES_FILENAME if os.path.exists(DEFAULT_EXCLUDES_FILENAME) else None,
|
||||||
|
help='Excludes filename',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-v', '--verbosity',
|
||||||
|
type=int,
|
||||||
|
help='Display verbose progress (1 for some, 2 for lots)',
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser.parse_args(arguments)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
args = parse_arguments(*sys.argv[1:])
|
||||||
|
config = parse_configuration(args.config_filename, CONFIG_FORMAT)
|
||||||
|
repository = config.location['repository']
|
||||||
|
remote_path = config.location.get('remote_path')
|
||||||
|
|
||||||
|
borg.initialize(config.storage)
|
||||||
|
borg.create_archive(
|
||||||
|
args.excludes_filename, args.verbosity, config.storage, **config.location
|
||||||
|
)
|
||||||
|
borg.prune_archives(args.verbosity, repository, config.retention, remote_path=remote_path)
|
||||||
|
borg.check_archives(args.verbosity, repository, config.consistency, remote_path=remote_path)
|
||||||
|
except (ValueError, IOError, CalledProcessError) as error:
|
||||||
|
print(error, file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
@@ -2,10 +2,10 @@ from collections import OrderedDict, namedtuple
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Python 2
|
# Python 2
|
||||||
from ConfigParser import ConfigParser
|
from ConfigParser import RawConfigParser
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Python 3
|
# Python 3
|
||||||
from configparser import ConfigParser
|
from configparser import RawConfigParser
|
||||||
|
|
||||||
|
|
||||||
Section_format = namedtuple('Section_format', ('name', 'options'))
|
Section_format = namedtuple('Section_format', ('name', 'options'))
|
||||||
@@ -25,9 +25,19 @@ CONFIG_FORMAT = (
|
|||||||
'location',
|
'location',
|
||||||
(
|
(
|
||||||
option('source_directories'),
|
option('source_directories'),
|
||||||
|
option('one_file_system', value_type=bool, required=False),
|
||||||
|
option('remote_path', required=False),
|
||||||
option('repository'),
|
option('repository'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Section_format(
|
||||||
|
'storage',
|
||||||
|
(
|
||||||
|
option('encryption_passphrase', required=False),
|
||||||
|
option('compression', required=False),
|
||||||
|
option('umask', required=False),
|
||||||
|
),
|
||||||
|
),
|
||||||
Section_format(
|
Section_format(
|
||||||
'retention',
|
'retention',
|
||||||
(
|
(
|
||||||
@@ -44,6 +54,7 @@ CONFIG_FORMAT = (
|
|||||||
'consistency',
|
'consistency',
|
||||||
(
|
(
|
||||||
option('checks', required=False),
|
option('checks', required=False),
|
||||||
|
option('check_last', required=False),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -51,7 +62,7 @@ CONFIG_FORMAT = (
|
|||||||
|
|
||||||
def validate_configuration_format(parser, config_format):
|
def validate_configuration_format(parser, config_format):
|
||||||
'''
|
'''
|
||||||
Given an open ConfigParser and an expected config file format, validate that the parsed
|
Given an open RawConfigParser and an expected config file format, validate that the parsed
|
||||||
configuration file has the expected sections, that any required options are present in those
|
configuration file has the expected sections, that any required options are present in those
|
||||||
sections, and that there aren't any unexpected options.
|
sections, and that there aren't any unexpected options.
|
||||||
|
|
||||||
@@ -110,14 +121,9 @@ def validate_configuration_format(parser, config_format):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Describes a parsed configuration, where each attribute is the name of a configuration file section
|
|
||||||
# and each value is a dict of that section's parsed options.
|
|
||||||
Parsed_config = namedtuple('Config', (section_format.name for section_format in CONFIG_FORMAT))
|
|
||||||
|
|
||||||
|
|
||||||
def parse_section_options(parser, section_format):
|
def parse_section_options(parser, section_format):
|
||||||
'''
|
'''
|
||||||
Given an open ConfigParser and an expected section format, return the option values from that
|
Given an open RawConfigParser and an expected section format, return the option values from that
|
||||||
section as a dict mapping from option name to value. Omit those options that are not present in
|
section as a dict mapping from option name to value. Omit those options that are not present in
|
||||||
the parsed options.
|
the parsed options.
|
||||||
|
|
||||||
@@ -126,6 +132,7 @@ def parse_section_options(parser, section_format):
|
|||||||
type_getter = {
|
type_getter = {
|
||||||
str: parser.get,
|
str: parser.get,
|
||||||
int: parser.getint,
|
int: parser.getint,
|
||||||
|
bool: parser.getboolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
return OrderedDict(
|
return OrderedDict(
|
||||||
@@ -135,21 +142,26 @@ def parse_section_options(parser, section_format):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def parse_configuration(config_filename):
|
def parse_configuration(config_filename, config_format):
|
||||||
'''
|
'''
|
||||||
Given a config filename of the expected format, return the parsed configuration as Parsed_config
|
Given a config filename and an expected config file format, return the parsed configuration
|
||||||
data structure.
|
as a namedtuple with one attribute for each parsed section.
|
||||||
|
|
||||||
Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
|
Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
|
||||||
'''
|
'''
|
||||||
parser = ConfigParser()
|
parser = RawConfigParser()
|
||||||
parser.read(config_filename)
|
if not parser.read(config_filename):
|
||||||
|
raise ValueError('Configuration file cannot be opened: {}'.format(config_filename))
|
||||||
|
|
||||||
validate_configuration_format(parser, CONFIG_FORMAT)
|
validate_configuration_format(parser, config_format)
|
||||||
|
|
||||||
|
# Describes a parsed configuration, where each attribute is the name of a configuration file
|
||||||
|
# section and each value is a dict of that section's parsed options.
|
||||||
|
Parsed_config = namedtuple('Parsed_config', (section_format.name for section_format in config_format))
|
||||||
|
|
||||||
return Parsed_config(
|
return Parsed_config(
|
||||||
*(
|
*(
|
||||||
parse_section_options(parser, section_format)
|
parse_section_options(parser, section_format)
|
||||||
for section_format in CONFIG_FORMAT
|
for section_format in config_format
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
+15
-18
@@ -2,28 +2,25 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from flexmock import flexmock
|
from flexmock import flexmock
|
||||||
from nose.tools import assert_raises
|
import pytest
|
||||||
|
|
||||||
from atticmatic import command as module
|
from borgmatic import command as module
|
||||||
|
|
||||||
|
|
||||||
COMMAND_NAME = 'foomatic'
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_arguments_with_no_arguments_uses_defaults():
|
def test_parse_arguments_with_no_arguments_uses_defaults():
|
||||||
flexmock(os.path).should_receive('exists').and_return(True)
|
flexmock(os.path).should_receive('exists').and_return(True)
|
||||||
|
|
||||||
parser = module.parse_arguments(COMMAND_NAME)
|
parser = module.parse_arguments()
|
||||||
|
|
||||||
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME
|
||||||
assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME
|
||||||
assert parser.verbosity == None
|
assert parser.verbosity == None
|
||||||
|
|
||||||
|
|
||||||
def test_parse_arguments_with_filename_arguments_overrides_defaults():
|
def test_parse_arguments_with_filename_arguments_overrides_defaults():
|
||||||
flexmock(os.path).should_receive('exists').and_return(True)
|
flexmock(os.path).should_receive('exists').and_return(True)
|
||||||
|
|
||||||
parser = module.parse_arguments(COMMAND_NAME, '--config', 'myconfig', '--excludes', 'myexcludes')
|
parser = module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
|
||||||
|
|
||||||
assert parser.config_filename == 'myconfig'
|
assert parser.config_filename == 'myconfig'
|
||||||
assert parser.excludes_filename == 'myexcludes'
|
assert parser.excludes_filename == 'myexcludes'
|
||||||
@@ -33,9 +30,9 @@ def test_parse_arguments_with_filename_arguments_overrides_defaults():
|
|||||||
def test_parse_arguments_with_missing_default_excludes_file_sets_filename_to_none():
|
def test_parse_arguments_with_missing_default_excludes_file_sets_filename_to_none():
|
||||||
flexmock(os.path).should_receive('exists').and_return(False)
|
flexmock(os.path).should_receive('exists').and_return(False)
|
||||||
|
|
||||||
parser = module.parse_arguments(COMMAND_NAME)
|
parser = module.parse_arguments()
|
||||||
|
|
||||||
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME
|
||||||
assert parser.excludes_filename == None
|
assert parser.excludes_filename == None
|
||||||
assert parser.verbosity == None
|
assert parser.verbosity == None
|
||||||
|
|
||||||
@@ -43,9 +40,9 @@ def test_parse_arguments_with_missing_default_excludes_file_sets_filename_to_non
|
|||||||
def test_parse_arguments_with_missing_overridden_excludes_file_retains_filename():
|
def test_parse_arguments_with_missing_overridden_excludes_file_retains_filename():
|
||||||
flexmock(os.path).should_receive('exists').and_return(False)
|
flexmock(os.path).should_receive('exists').and_return(False)
|
||||||
|
|
||||||
parser = module.parse_arguments(COMMAND_NAME, '--excludes', 'myexcludes')
|
parser = module.parse_arguments('--excludes', 'myexcludes')
|
||||||
|
|
||||||
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME
|
||||||
assert parser.excludes_filename == 'myexcludes'
|
assert parser.excludes_filename == 'myexcludes'
|
||||||
assert parser.verbosity == None
|
assert parser.verbosity == None
|
||||||
|
|
||||||
@@ -53,10 +50,10 @@ def test_parse_arguments_with_missing_overridden_excludes_file_retains_filename(
|
|||||||
def test_parse_arguments_with_verbosity_flag_overrides_default():
|
def test_parse_arguments_with_verbosity_flag_overrides_default():
|
||||||
flexmock(os.path).should_receive('exists').and_return(True)
|
flexmock(os.path).should_receive('exists').and_return(True)
|
||||||
|
|
||||||
parser = module.parse_arguments(COMMAND_NAME, '--verbosity', '1')
|
parser = module.parse_arguments('--verbosity', '1')
|
||||||
|
|
||||||
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME
|
||||||
assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME_PATTERN.format(COMMAND_NAME)
|
assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME
|
||||||
assert parser.verbosity == 1
|
assert parser.verbosity == 1
|
||||||
|
|
||||||
|
|
||||||
@@ -66,7 +63,7 @@ def test_parse_arguments_with_invalid_arguments_exits():
|
|||||||
sys.stderr = sys.stdout
|
sys.stderr = sys.stdout
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with assert_raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
module.parse_arguments(COMMAND_NAME, '--posix-me-harder')
|
module.parse_arguments('--posix-me-harder')
|
||||||
finally:
|
finally:
|
||||||
sys.stderr = original_stderr
|
sys.stderr = original_stderr
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
try:
|
||||||
|
# Python 2
|
||||||
|
from cStringIO import StringIO
|
||||||
|
except ImportError:
|
||||||
|
# Python 3
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
|
import string
|
||||||
|
|
||||||
|
from borgmatic import config as module
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_section_options_with_punctuation_should_return_section_options():
|
||||||
|
parser = module.RawConfigParser()
|
||||||
|
parser.readfp(StringIO('[section]\nfoo: {}\n'.format(string.punctuation)))
|
||||||
|
|
||||||
|
section_format = module.Section_format(
|
||||||
|
'section',
|
||||||
|
(module.Config_option('foo', str, required=True),),
|
||||||
|
)
|
||||||
|
|
||||||
|
config = module.parse_section_options(parser, section_format)
|
||||||
|
|
||||||
|
assert config == OrderedDict(
|
||||||
|
(
|
||||||
|
('foo', string.punctuation),
|
||||||
|
)
|
||||||
|
)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def test_setup_version_matches_news_version():
|
||||||
|
setup_version = subprocess.check_output(('python', 'setup.py', '--version')).decode('ascii')
|
||||||
|
news_version = open('NEWS').readline()
|
||||||
|
|
||||||
|
assert setup_version == news_version
|
||||||
@@ -0,0 +1,476 @@
|
|||||||
|
from collections import OrderedDict
|
||||||
|
from subprocess import STDOUT
|
||||||
|
import os
|
||||||
|
|
||||||
|
from flexmock import flexmock
|
||||||
|
|
||||||
|
from borgmatic import borg as module
|
||||||
|
from borgmatic.tests.builtins import builtins_mock
|
||||||
|
from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
||||||
|
|
||||||
|
|
||||||
|
def test_initialize_with_passphrase_should_set_environment():
|
||||||
|
orig_environ = os.environ
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.environ = {}
|
||||||
|
module.initialize({'encryption_passphrase': 'pass'}, command='borg')
|
||||||
|
assert os.environ.get('BORG_PASSPHRASE') == 'pass'
|
||||||
|
finally:
|
||||||
|
os.environ = orig_environ
|
||||||
|
|
||||||
|
|
||||||
|
def test_initialize_without_passphrase_should_not_set_environment():
|
||||||
|
orig_environ = os.environ
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.environ = {}
|
||||||
|
module.initialize({}, command='borg')
|
||||||
|
assert os.environ.get('BORG_PASSPHRASE') == None
|
||||||
|
finally:
|
||||||
|
os.environ = orig_environ
|
||||||
|
|
||||||
|
|
||||||
|
def insert_subprocess_mock(check_call_command, **kwargs):
|
||||||
|
subprocess = flexmock(STDOUT=STDOUT)
|
||||||
|
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
|
||||||
|
flexmock(module).subprocess = subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def insert_subprocess_never():
|
||||||
|
subprocess = flexmock()
|
||||||
|
subprocess.should_receive('check_call').never()
|
||||||
|
flexmock(module).subprocess = subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def insert_platform_mock():
|
||||||
|
flexmock(module.platform).should_receive('node').and_return('host')
|
||||||
|
|
||||||
|
|
||||||
|
def insert_datetime_mock():
|
||||||
|
flexmock(module).datetime = flexmock().should_receive('now').and_return(
|
||||||
|
flexmock().should_receive('isoformat').and_return('now').mock
|
||||||
|
).mock
|
||||||
|
|
||||||
|
|
||||||
|
CREATE_COMMAND_WITHOUT_EXCLUDES = ('borg', 'create', 'repo::host-now', 'foo', 'bar')
|
||||||
|
CREATE_COMMAND = CREATE_COMMAND_WITHOUT_EXCLUDES + ('--exclude-from', 'excludes')
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_should_call_borg_with_parameters():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_two_spaces_in_source_directories():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_none_excludes_filename_should_call_borg_without_excludes():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename=None,
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_verbosity_some_should_call_borg_with_stats_parameter():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=VERBOSITY_SOME,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_verbosity_lots_should_call_borg_with_verbose_parameter():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=VERBOSITY_LOTS,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_compression_should_call_borg_with_compression_parameters():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--compression', 'rle'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={'compression': 'rle'},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_one_file_system_should_call_borg_with_one_file_system_parameters():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--one-file-system',))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
one_file_system=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_remote_path_should_call_borg_with_remote_path_parameters():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--remote-path', 'borg1'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
remote_path='borg1',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_umask_should_call_borg_with_umask_parameters():
|
||||||
|
insert_subprocess_mock(CREATE_COMMAND + ('--umask', '740'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename='excludes',
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={'umask': 740},
|
||||||
|
source_directories='foo bar',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_source_directories_glob_expands():
|
||||||
|
insert_subprocess_mock(('borg', 'create', 'repo::host-now', 'foo', 'food'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
flexmock(module).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename=None,
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo*',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_non_matching_source_directories_glob_passes_through():
|
||||||
|
insert_subprocess_mock(('borg', 'create', 'repo::host-now', 'foo*'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
flexmock(module).should_receive('glob').with_args('foo*').and_return([])
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename=None,
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo*',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_archive_with_glob_should_call_borg_with_expanded_directories():
|
||||||
|
insert_subprocess_mock(('borg', 'create', 'repo::host-now', 'foo', 'food'))
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
flexmock(module).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
|
||||||
|
|
||||||
|
module.create_archive(
|
||||||
|
excludes_filename=None,
|
||||||
|
verbosity=None,
|
||||||
|
storage_config={},
|
||||||
|
source_directories='foo*',
|
||||||
|
repository='repo',
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
BASE_PRUNE_FLAGS = (
|
||||||
|
('--keep-daily', '1'),
|
||||||
|
('--keep-weekly', '2'),
|
||||||
|
('--keep-monthly', '3'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_prune_flags_should_return_flags_from_config():
|
||||||
|
retention_config = OrderedDict(
|
||||||
|
(
|
||||||
|
('keep_daily', 1),
|
||||||
|
('keep_weekly', 2),
|
||||||
|
('keep_monthly', 3),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
result = module._make_prune_flags(retention_config)
|
||||||
|
|
||||||
|
assert tuple(result) == BASE_PRUNE_FLAGS
|
||||||
|
|
||||||
|
|
||||||
|
PRUNE_COMMAND = (
|
||||||
|
'borg', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_prune_archives_should_call_borg_with_parameters():
|
||||||
|
retention_config = flexmock()
|
||||||
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
||||||
|
BASE_PRUNE_FLAGS,
|
||||||
|
)
|
||||||
|
insert_subprocess_mock(PRUNE_COMMAND)
|
||||||
|
|
||||||
|
module.prune_archives(
|
||||||
|
verbosity=None,
|
||||||
|
repository='repo',
|
||||||
|
retention_config=retention_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_prune_archives_with_verbosity_some_should_call_borg_with_stats_parameter():
|
||||||
|
retention_config = flexmock()
|
||||||
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
||||||
|
BASE_PRUNE_FLAGS,
|
||||||
|
)
|
||||||
|
insert_subprocess_mock(PRUNE_COMMAND + ('--stats',))
|
||||||
|
|
||||||
|
module.prune_archives(
|
||||||
|
repository='repo',
|
||||||
|
verbosity=VERBOSITY_SOME,
|
||||||
|
retention_config=retention_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_prune_archives_with_verbosity_lots_should_call_borg_with_verbose_parameter():
|
||||||
|
retention_config = flexmock()
|
||||||
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
||||||
|
BASE_PRUNE_FLAGS,
|
||||||
|
)
|
||||||
|
insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
|
||||||
|
|
||||||
|
module.prune_archives(
|
||||||
|
repository='repo',
|
||||||
|
verbosity=VERBOSITY_LOTS,
|
||||||
|
retention_config=retention_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_prune_archive_with_remote_path_should_call_borg_with_remote_path_parameters():
|
||||||
|
retention_config = flexmock()
|
||||||
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
||||||
|
BASE_PRUNE_FLAGS,
|
||||||
|
)
|
||||||
|
insert_subprocess_mock(PRUNE_COMMAND + ('--remote-path', 'borg1'))
|
||||||
|
|
||||||
|
module.prune_archives(
|
||||||
|
verbosity=None,
|
||||||
|
repository='repo',
|
||||||
|
retention_config=retention_config,
|
||||||
|
command='borg',
|
||||||
|
remote_path='borg1',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_checks_returns_them_as_tuple():
|
||||||
|
checks = module._parse_checks({'checks': 'foo disabled bar'})
|
||||||
|
|
||||||
|
assert checks == ('foo', 'bar')
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_checks_with_missing_value_returns_defaults():
|
||||||
|
checks = module._parse_checks({})
|
||||||
|
|
||||||
|
assert checks == module.DEFAULT_CHECKS
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_checks_with_blank_value_returns_defaults():
|
||||||
|
checks = module._parse_checks({'checks': ''})
|
||||||
|
|
||||||
|
assert checks == module.DEFAULT_CHECKS
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_checks_with_disabled_returns_no_checks():
|
||||||
|
checks = module._parse_checks({'checks': 'disabled'})
|
||||||
|
|
||||||
|
assert checks == ()
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_check_flags_with_checks_returns_flags():
|
||||||
|
flags = module._make_check_flags(('foo', 'bar'))
|
||||||
|
|
||||||
|
assert flags == ('--foo-only', '--bar-only')
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_check_flags_with_default_checks_returns_no_flags():
|
||||||
|
flags = module._make_check_flags(module.DEFAULT_CHECKS)
|
||||||
|
|
||||||
|
assert flags == ()
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
|
||||||
|
flags = module._make_check_flags(('foo', 'bar'), check_last=3)
|
||||||
|
|
||||||
|
assert flags == ('--foo-only', '--bar-only', '--last', 3)
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_check_flags_with_last_returns_last_flag():
|
||||||
|
flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
|
||||||
|
|
||||||
|
assert flags == ('--last', 3)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_archives_should_call_borg_with_parameters():
|
||||||
|
checks = flexmock()
|
||||||
|
check_last = flexmock()
|
||||||
|
consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
||||||
|
flexmock(module).should_receive('_parse_checks').and_return(checks)
|
||||||
|
flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
|
||||||
|
stdout = flexmock()
|
||||||
|
insert_subprocess_mock(
|
||||||
|
('borg', 'check', 'repo'),
|
||||||
|
stdout=stdout, stderr=STDOUT,
|
||||||
|
)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
builtins_mock().should_receive('open').and_return(stdout)
|
||||||
|
flexmock(module.os).should_receive('devnull')
|
||||||
|
|
||||||
|
module.check_archives(
|
||||||
|
verbosity=None,
|
||||||
|
repository='repo',
|
||||||
|
consistency_config=consistency_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_archives_with_verbosity_some_should_call_borg_with_verbose_parameter():
|
||||||
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
||||||
|
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
||||||
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
||||||
|
insert_subprocess_mock(
|
||||||
|
('borg', 'check', 'repo', '--verbose'),
|
||||||
|
stdout=None, stderr=STDOUT,
|
||||||
|
)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.check_archives(
|
||||||
|
verbosity=VERBOSITY_SOME,
|
||||||
|
repository='repo',
|
||||||
|
consistency_config=consistency_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_archives_with_verbosity_lots_should_call_borg_with_verbose_parameter():
|
||||||
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
||||||
|
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
||||||
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
||||||
|
insert_subprocess_mock(
|
||||||
|
('borg', 'check', 'repo', '--verbose'),
|
||||||
|
stdout=None, stderr=STDOUT,
|
||||||
|
)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
|
||||||
|
module.check_archives(
|
||||||
|
verbosity=VERBOSITY_LOTS,
|
||||||
|
repository='repo',
|
||||||
|
consistency_config=consistency_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_archives_without_any_checks_should_bail():
|
||||||
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
||||||
|
flexmock(module).should_receive('_parse_checks').and_return(())
|
||||||
|
insert_subprocess_never()
|
||||||
|
|
||||||
|
module.check_archives(
|
||||||
|
verbosity=None,
|
||||||
|
repository='repo',
|
||||||
|
consistency_config=consistency_config,
|
||||||
|
command='borg',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_archives_with_remote_path_should_call_borg_with_remote_path_parameters():
|
||||||
|
checks = flexmock()
|
||||||
|
check_last = flexmock()
|
||||||
|
consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
||||||
|
flexmock(module).should_receive('_parse_checks').and_return(checks)
|
||||||
|
flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
|
||||||
|
stdout = flexmock()
|
||||||
|
insert_subprocess_mock(
|
||||||
|
('borg', 'check', 'repo', '--remote-path', 'borg1'),
|
||||||
|
stdout=stdout, stderr=STDOUT,
|
||||||
|
)
|
||||||
|
insert_platform_mock()
|
||||||
|
insert_datetime_mock()
|
||||||
|
builtins_mock().should_receive('open').and_return(stdout)
|
||||||
|
flexmock(module.os).should_receive('devnull')
|
||||||
|
|
||||||
|
module.check_archives(
|
||||||
|
verbosity=None,
|
||||||
|
repository='repo',
|
||||||
|
consistency_config=consistency_config,
|
||||||
|
command='borg',
|
||||||
|
remote_path='borg1',
|
||||||
|
)
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from flexmock import flexmock
|
from flexmock import flexmock
|
||||||
from nose.tools import assert_raises
|
import pytest
|
||||||
|
|
||||||
from atticmatic import config as module
|
from borgmatic import config as module
|
||||||
|
|
||||||
|
|
||||||
def test_option_should_create_config_option():
|
def test_option_should_create_config_option():
|
||||||
@@ -61,7 +61,7 @@ def test_validate_configuration_format_with_missing_required_section_should_rais
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
module.validate_configuration_format(parser, config_format)
|
module.validate_configuration_format(parser, config_format)
|
||||||
|
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ def test_validate_configuration_format_with_unknown_section_should_raise():
|
|||||||
module.Section_format('section', options=()),
|
module.Section_format('section', options=()),
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
module.validate_configuration_format(parser, config_format)
|
module.validate_configuration_format(parser, config_format)
|
||||||
|
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ def test_validate_configuration_format_with_missing_required_option_should_raise
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
module.validate_configuration_format(parser, config_format)
|
module.validate_configuration_format(parser, config_format)
|
||||||
|
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ def test_validate_configuration_format_with_extra_option_should_raise():
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
module.validate_configuration_format(parser, config_format)
|
module.validate_configuration_format(parser, config_format)
|
||||||
|
|
||||||
|
|
||||||
@@ -154,6 +154,7 @@ def test_parse_section_options_should_return_section_options():
|
|||||||
parser = flexmock()
|
parser = flexmock()
|
||||||
parser.should_receive('get').with_args('section', 'foo').and_return('value')
|
parser.should_receive('get').with_args('section', 'foo').and_return('value')
|
||||||
parser.should_receive('getint').with_args('section', 'bar').and_return(1)
|
parser.should_receive('getint').with_args('section', 'bar').and_return(1)
|
||||||
|
parser.should_receive('getboolean').never()
|
||||||
parser.should_receive('has_option').with_args('section', 'foo').and_return(True)
|
parser.should_receive('has_option').with_args('section', 'foo').and_return(True)
|
||||||
parser.should_receive('has_option').with_args('section', 'bar').and_return(True)
|
parser.should_receive('has_option').with_args('section', 'bar').and_return(True)
|
||||||
|
|
||||||
@@ -179,6 +180,7 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict():
|
|||||||
parser = flexmock()
|
parser = flexmock()
|
||||||
parser.should_receive('get').never()
|
parser.should_receive('get').never()
|
||||||
parser.should_receive('getint').never()
|
parser.should_receive('getint').never()
|
||||||
|
parser.should_receive('getboolean').never()
|
||||||
parser.should_receive('has_option').with_args('section', 'foo').and_return(False)
|
parser.should_receive('has_option').with_args('section', 'foo').and_return(False)
|
||||||
parser.should_receive('has_option').with_args('section', 'bar').and_return(False)
|
parser.should_receive('has_option').with_args('section', 'bar').and_return(False)
|
||||||
|
|
||||||
@@ -197,25 +199,34 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict():
|
|||||||
|
|
||||||
def insert_mock_parser():
|
def insert_mock_parser():
|
||||||
parser = flexmock()
|
parser = flexmock()
|
||||||
parser.should_receive('read')
|
parser.should_receive('read').and_return([flexmock()])
|
||||||
module.ConfigParser = lambda: parser
|
module.RawConfigParser = lambda: parser
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def test_parse_configuration_should_return_section_configs():
|
def test_parse_configuration_should_return_section_configs():
|
||||||
parser = insert_mock_parser()
|
parser = insert_mock_parser()
|
||||||
|
config_format = (flexmock(name='items'), flexmock(name='things'))
|
||||||
mock_module = flexmock(module)
|
mock_module = flexmock(module)
|
||||||
mock_module.should_receive('validate_configuration_format').with_args(
|
mock_module.should_receive('validate_configuration_format').with_args(
|
||||||
parser, module.CONFIG_FORMAT,
|
parser, config_format,
|
||||||
).once()
|
).once()
|
||||||
mock_section_configs = (flexmock(),) * len(module.CONFIG_FORMAT)
|
mock_section_configs = (flexmock(), flexmock())
|
||||||
|
|
||||||
for section_format, section_config in zip(module.CONFIG_FORMAT, mock_section_configs):
|
for section_format, section_config in zip(config_format, mock_section_configs):
|
||||||
mock_module.should_receive('parse_section_options').with_args(
|
mock_module.should_receive('parse_section_options').with_args(
|
||||||
parser, section_format,
|
parser, section_format,
|
||||||
).and_return(section_config).once()
|
).and_return(section_config).once()
|
||||||
|
|
||||||
parsed_config = module.parse_configuration('filename')
|
parsed_config = module.parse_configuration('filename', config_format)
|
||||||
|
|
||||||
assert parsed_config == module.Parsed_config(*mock_section_configs)
|
assert parsed_config == type(parsed_config)(*mock_section_configs)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_configuration_with_file_open_error_should_raise():
|
||||||
|
parser = insert_mock_parser()
|
||||||
|
parser.should_receive('read').and_return([])
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
module.parse_configuration('filename', config_format=flexmock())
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
# You can drop this file into /etc/cron.d/ to run atticmatic nightly.
|
|
||||||
|
|
||||||
0 3 * * * root PATH=$PATH:/usr/local/bin /usr/local/bin/atticmatic
|
|
||||||
+30
-6
@@ -1,24 +1,48 @@
|
|||||||
[location]
|
[location]
|
||||||
# Space-separated list of source directories to backup.
|
# Space-separated list of source directories to backup.
|
||||||
source_directories: /home /etc
|
# Globs are expanded.
|
||||||
|
source_directories: /home /etc /var/log/syslog*
|
||||||
|
|
||||||
|
# Stay in same file system (do not cross mount points).
|
||||||
|
#one_file_system: True
|
||||||
|
|
||||||
|
# Alternate Borg remote executable (defaults to "borg"):
|
||||||
|
#remote_path: borg1
|
||||||
|
|
||||||
# Path to local or remote repository.
|
# Path to local or remote repository.
|
||||||
repository: user@backupserver:sourcehostname.attic
|
repository: user@backupserver:sourcehostname.borg
|
||||||
|
|
||||||
|
[storage]
|
||||||
|
# Passphrase to unlock the encryption key with. Only use on repositories that
|
||||||
|
# were initialized with passphrase/repokey encryption.
|
||||||
|
#encryption_passphrase: foo
|
||||||
|
|
||||||
|
# Type of compression to use when creating archives. See
|
||||||
|
# https://borgbackup.readthedocs.org/en/stable/usage.html#borg-create
|
||||||
|
# for details. Defaults to no compression.
|
||||||
|
#compression: lz4
|
||||||
|
|
||||||
|
# Umask to be used for borg create.
|
||||||
|
#umask: 0740
|
||||||
|
|
||||||
[retention]
|
[retention]
|
||||||
# Retention policy for how many backups to keep in each category. See
|
# Retention policy for how many backups to keep in each category. See
|
||||||
# https://attic-backup.org/usage.html#attic-prune or
|
# https://borgbackup.readthedocs.org/en/stable/usage.html#borg-prune for details.
|
||||||
# https://borgbackup.github.io/borgbackup/usage.html#borg-prune for details.
|
#keep_within: 3H
|
||||||
#keep_within: 3h
|
|
||||||
#keep_hourly: 24
|
#keep_hourly: 24
|
||||||
keep_daily: 7
|
keep_daily: 7
|
||||||
keep_weekly: 4
|
keep_weekly: 4
|
||||||
keep_monthly: 6
|
keep_monthly: 6
|
||||||
keep_yearly: 1
|
keep_yearly: 1
|
||||||
|
|
||||||
#prefix: sourcehostname
|
#prefix: sourcehostname
|
||||||
|
|
||||||
[consistency]
|
[consistency]
|
||||||
# Space-separated list of consistency checks to run: "repository", "archives",
|
# Space-separated list of consistency checks to run: "repository", "archives",
|
||||||
# or both. Defaults to both. Set to "disabled" to disable all consistency
|
# or both. Defaults to both. Set to "disabled" to disable all consistency
|
||||||
# checks. See https://attic-backup.org/usage.html#attic-check for details.
|
# checks. See https://borgbackup.readthedocs.org/en/stable/usage.html#borg-check
|
||||||
|
# for details.
|
||||||
checks: repository archives
|
checks: repository archives
|
||||||
|
|
||||||
|
# Restrict the number of checked archives to the last n.
|
||||||
|
#check_last: 3
|
||||||
|
|||||||
@@ -1,2 +1,5 @@
|
|||||||
[nosetests]
|
[metadata]
|
||||||
detailed-errors=1
|
description-file=README.md
|
||||||
|
|
||||||
|
[bdist_wheel]
|
||||||
|
universal=1
|
||||||
|
|||||||
@@ -1,20 +1,37 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
|
||||||
|
VERSION = '1.0.2'
|
||||||
|
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='atticmatic',
|
name='borgmatic',
|
||||||
version='0.1.1',
|
version=VERSION,
|
||||||
description='A wrapper script for Attic/Borg backup software that creates and prunes backups',
|
description='A wrapper script for Borg backup software that creates and prunes backups',
|
||||||
author='Dan Helfman',
|
author='Dan Helfman',
|
||||||
author_email='witten@torsion.org',
|
author_email='witten@torsion.org',
|
||||||
|
url='https://torsion.org/borgmatic',
|
||||||
|
download_url='https://torsion.org/hg/borgmatic/archive/%s.tar.gz' % VERSION,
|
||||||
|
classifiers=(
|
||||||
|
'Development Status :: 5 - Production/Stable',
|
||||||
|
'Environment :: Console',
|
||||||
|
'Intended Audience :: System Administrators',
|
||||||
|
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
|
||||||
|
'Programming Language :: Python',
|
||||||
|
'Topic :: Security :: Cryptography',
|
||||||
|
'Topic :: System :: Archiving :: Backup',
|
||||||
|
),
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'atticmatic = atticmatic.command:main',
|
'borgmatic = borgmatic.command:main',
|
||||||
'borgmatic = atticmatic.command:main',
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
obsoletes=[
|
||||||
|
'atticmatic',
|
||||||
|
],
|
||||||
tests_require=(
|
tests_require=(
|
||||||
'flexmock',
|
'flexmock',
|
||||||
'nose',
|
'pytest',
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
flexmock==0.9.7
|
flexmock==0.10.2
|
||||||
nose==1.3.4
|
pytest==2.9.1
|
||||||
|
|||||||
Reference in New Issue
Block a user