Merge pull request #692 from manwegit/feature/format_archive

Archive name formatting on create
This commit is contained in:
TW 2016-02-25 23:49:03 +01:00
commit de644a041f
3 changed files with 32 additions and 2 deletions

View File

@ -27,6 +27,7 @@ from . import shellpattern
import msgpack import msgpack
import msgpack.fallback import msgpack.fallback
import socket
# return codes returned by borg command # return codes returned by borg command
# when borg is killed by signal N, rc = 128 + N # when borg is killed by signal N, rc = 128 + N
@ -688,7 +689,21 @@ class Location:
if not self.parse(self.orig): if not self.parse(self.orig):
raise ValueError 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): def parse(self, text):
text = self.preformat_text(text)
valid = self._parse(text) valid = self._parse(text)
if valid: if valid:
return True return True

View File

@ -8,6 +8,7 @@ import pytest
import sys import sys
import msgpack import msgpack
import msgpack.fallback import msgpack.fallback
import time
from ..helpers import Location, format_file_size, format_timedelta, make_path_safe, \ 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, \ 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() == \ assert Location(location).canonical_path() == \
Location(Location(location).canonical_path()).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: class TestLocationWithEnv:
def test_ssh(self, monkeypatch): def test_ssh(self, monkeypatch):

View File

@ -251,8 +251,7 @@ Examples
# Backup the root filesystem into an archive named "root-YYYY-MM-DD" # Backup the root filesystem into an archive named "root-YYYY-MM-DD"
# use zlib compression (good, but slow) - default is no compression # use zlib compression (good, but slow) - default is no compression
NAME="root-`date +%Y-%m-%d`" $ borg create -C zlib,6 /mnt/backup::root-{now:%Y-%m-%d} / --one-file-system
$ borg create -C zlib,6 /mnt/backup::$NAME / --one-file-system
# Make a big effort in fine granular deduplication (big chunk management # Make a big effort in fine granular deduplication (big chunk management
# overhead, needs a lot of RAM and disk space, see formula in internals # 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) # Even slower, even higher compression (N = 0..9)
$ borg create --compression lzma,N /mnt/backup::repo ~ $ 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 .. include:: usage/extract.rst.inc
Examples Examples