From 85fb38e2f3d7c4df581f2aa58605a507003db492 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Mon, 11 Dec 2017 23:00:38 +0100 Subject: [PATCH 1/2] Add placeholder for fqdn in reverse notation --- docs/man/borg-placeholders.1 | 3 +++ docs/usage/help.rst.inc | 3 +++ src/borg/archiver.py | 3 +++ src/borg/helpers/parseformat.py | 1 + 4 files changed, 10 insertions(+) diff --git a/docs/man/borg-placeholders.1 b/docs/man/borg-placeholders.1 index 9f5012015..90a0ff22e 100644 --- a/docs/man/borg-placeholders.1 +++ b/docs/man/borg-placeholders.1 @@ -42,6 +42,9 @@ The (short) hostname of the machine. .B {fqdn} The full name of the machine. .TP +.B {reverse-fqdn} +The full name of the machine in reverse domain name notation. +.TP .B {now} The current local date and time, by default in ISO\-8601 format. You can also supply your own \fI\%format string\fP, e.g. {now:%Y\-%m\-%d_%H:%M:%S} diff --git a/docs/usage/help.rst.inc b/docs/usage/help.rst.inc index 2d5455564..3201dab4f 100644 --- a/docs/usage/help.rst.inc +++ b/docs/usage/help.rst.inc @@ -169,6 +169,9 @@ placeholders: {fqdn} The full name of the machine. +{reverse-fqdn} + The full name of the machine in reverse domain name notation. + {now} The current local date and time, by default in ISO-8601 format. You can also supply your own `format string `_, e.g. {now:%Y-%m-%d_%H:%M:%S} diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 887cb7726..db7738192 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -1901,6 +1901,9 @@ class Archiver: {fqdn} The full name of the machine. + {reverse-fqdn} + The full name of the machine in reverse domain name notation. + {now} The current local date and time, by default in ISO-8601 format. You can also supply your own `format string `_, e.g. {now:%Y-%m-%d_%H:%M:%S} diff --git a/src/borg/helpers/parseformat.py b/src/borg/helpers/parseformat.py index 3682bf263..83481846b 100644 --- a/src/borg/helpers/parseformat.py +++ b/src/borg/helpers/parseformat.py @@ -183,6 +183,7 @@ def replace_placeholders(text): data = { 'pid': os.getpid(), 'fqdn': socket.getfqdn(), + 'reverse-fqdn': '.'.join(reversed(socket.getfqdn().split('.'))), 'hostname': socket.gethostname(), 'now': DatetimeWrapper(current_time.now()), 'utcnow': DatetimeWrapper(current_time.utcnow()), From 294f06b565885f2e78d64fa6c26c90fd8d8c3431 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 12 Dec 2017 12:44:17 +0100 Subject: [PATCH 2/2] Refactor: call getfqdn() once per call of replace_placeholders() --- src/borg/helpers/parseformat.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/borg/helpers/parseformat.py b/src/borg/helpers/parseformat.py index 83481846b..f6ec9defc 100644 --- a/src/borg/helpers/parseformat.py +++ b/src/borg/helpers/parseformat.py @@ -180,10 +180,11 @@ def format_line(format, data): def replace_placeholders(text): """Replace placeholders in text with their values.""" current_time = datetime.now() + fqdn = socket.getfqdn() data = { 'pid': os.getpid(), - 'fqdn': socket.getfqdn(), - 'reverse-fqdn': '.'.join(reversed(socket.getfqdn().split('.'))), + 'fqdn': fqdn, + 'reverse-fqdn': '.'.join(reversed(fqdn.split('.'))), 'hostname': socket.gethostname(), 'now': DatetimeWrapper(current_time.now()), 'utcnow': DatetimeWrapper(current_time.utcnow()),