From f7c1632aee4e316778591da25e573fce74310a36 Mon Sep 17 00:00:00 2001 From: Teemu Toivanen Date: Wed, 10 Feb 2016 11:39:35 +0200 Subject: [PATCH 1/3] Add format options to location. Add support for python format options for location: tags: pid fqdn hostname now utcnow user --- borg/helpers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/borg/helpers.py b/borg/helpers.py index d10b61cca..b8bd8e170 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -27,6 +27,7 @@ from . import shellpattern import msgpack import msgpack.fallback +import socket # return codes returned by borg command # when borg is killed by signal N, rc = 128 + N @@ -688,7 +689,21 @@ class Location: if not self.parse(self.orig): raise ValueError + def preformat_text(self, text): + """Format repository and archive path with common tags""" + current_time = datetime.now() + data = { + 'pid': os.getpid(), + 'fqdn': socket.getfqdn(), + 'hostname': socket.gethostname(), + 'now': current_time.now(), + 'utcnow': current_time.utcnow(), + 'user': uid2user(os.getuid(), os.getuid()) + } + return format_line(text, data) + def parse(self, text): + text = self.preformat_text(text) valid = self._parse(text) if valid: return True From 79f42571ae16a0622eab48f752c2d4a7be886eb6 Mon Sep 17 00:00:00 2001 From: Teemu Toivanen Date: Wed, 24 Feb 2016 23:32:02 +0200 Subject: [PATCH 2/3] unit test for archive format tests formatting with {pid} and not equal results from same time string. (adds import time to tests) --- borg/testsuite/helpers.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/borg/testsuite/helpers.py b/borg/testsuite/helpers.py index a1b5440a4..cdb96b964 100644 --- a/borg/testsuite/helpers.py +++ b/borg/testsuite/helpers.py @@ -8,6 +8,7 @@ import pytest import sys import msgpack import msgpack.fallback +import time from ..helpers import Location, format_file_size, format_timedelta, make_path_safe, \ prune_within, prune_split, get_cache_dir, get_keys_dir, Statistics, is_slow_msgpack, \ @@ -101,6 +102,16 @@ class TestLocationWithoutEnv: assert Location(location).canonical_path() == \ Location(Location(location).canonical_path()).canonical_path() + def test_format_path(self, monkeypatch): + monkeypatch.delenv('BORG_REPO', raising=False) + test_pid = os.getpid() + assert repr(Location('/some/path::archive{pid}')) == \ + "Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive{}')".format(test_pid) + location_time1 = Location('/some/path::archive{now:%s}') + time.sleep(1.1) + location_time2 = Location('/some/path::archive{now:%s}') + assert location_time1.archive != location_time2.archive + class TestLocationWithEnv: def test_ssh(self, monkeypatch): From a94471dc0ec6ce23b9ddc1b9b6188fe026cbae07 Mon Sep 17 00:00:00 2001 From: Teemu Toivanen Date: Wed, 24 Feb 2016 23:47:42 +0200 Subject: [PATCH 3/3] Usage examples for create format tags --- docs/usage.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 92c0101c1..b12aeac39 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -251,8 +251,7 @@ Examples # Backup the root filesystem into an archive named "root-YYYY-MM-DD" # use zlib compression (good, but slow) - default is no compression - NAME="root-`date +%Y-%m-%d`" - $ borg create -C zlib,6 /mnt/backup::$NAME / --one-file-system + $ borg create -C zlib,6 /mnt/backup::root-{now:%Y-%m-%d} / --one-file-system # Make a big effort in fine granular deduplication (big chunk management # overhead, needs a lot of RAM and disk space, see formula in internals @@ -274,6 +273,11 @@ Examples # Even slower, even higher compression (N = 0..9) $ borg create --compression lzma,N /mnt/backup::repo ~ + # Format tags available for archive name: + # {now}, {utcnow}, {fqdn}, {hostname}, {user}, {pid} + # add short hostname, backup username and current unixtime (seconds from epoch) + $ borg create /mnt/backup::{hostname}-{user}-{now:%s} ~ + .. include:: usage/extract.rst.inc Examples