Browse Source

Merge pull request #692 from manwegit/feature/format_archive

Archive name formatting on create
TW 9 years ago
parent
commit
de644a041f
3 changed files with 32 additions and 2 deletions
  1. 15 0
      borg/helpers.py
  2. 11 0
      borg/testsuite/helpers.py
  3. 6 2
      docs/usage.rst

+ 15 - 0
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

+ 11 - 0
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):

+ 6 - 2
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