mirror of https://github.com/borgbackup/borg.git
Merge pull request #3480 from ThomasWaldmann/borg-base-dir-1.1
added BORG_BASE_DIR, s/get_home_dir/get_base_dir/g, docs, fixes #3338
This commit is contained in:
commit
b500ab7509
|
@ -203,19 +203,22 @@ Some automatic "answerers" (if set, they automatically answer confirmation quest
|
||||||
allowed). So please test your scripts interactively before making them a non-interactive script.
|
allowed). So please test your scripts interactively before making them a non-interactive script.
|
||||||
|
|
||||||
Directories and files:
|
Directories and files:
|
||||||
BORG_KEYS_DIR
|
BORG_BASE_DIR
|
||||||
Default to '~/.config/borg/keys'. This directory contains keys for encrypted repositories.
|
Default to '$HOME', '~$USER', '~' (in that order)'.
|
||||||
BORG_KEY_FILE
|
If we refer to ~ below, we in fact mean BORG_BASE_DIR.
|
||||||
When set, use the given filename as repository key file.
|
BORG_CONFIG_DIR
|
||||||
|
Default to '~/.config/borg'. This directory contains the whole config directories.
|
||||||
|
BORG_CACHE_DIR
|
||||||
|
Default to '~/.cache/borg'. This directory contains the local cache and might need a lot
|
||||||
|
of space for dealing with big repositories.
|
||||||
BORG_SECURITY_DIR
|
BORG_SECURITY_DIR
|
||||||
Default to '~/.config/borg/security'. This directory contains information borg uses to
|
Default to '~/.config/borg/security'. This directory contains information borg uses to
|
||||||
track its usage of NONCES ("numbers used once" - usually in encryption context) and other
|
track its usage of NONCES ("numbers used once" - usually in encryption context) and other
|
||||||
security relevant data.
|
security relevant data.
|
||||||
BORG_CACHE_DIR
|
BORG_KEYS_DIR
|
||||||
Default to '~/.cache/borg'. This directory contains the local cache and might need a lot
|
Default to '~/.config/borg/keys'. This directory contains keys for encrypted repositories.
|
||||||
of space for dealing with big repositories).
|
BORG_KEY_FILE
|
||||||
BORG_CONFIG_DIR
|
When set, use the given filename as repository key file.
|
||||||
Default to '~/.config/borg'. This directory contains the whole config directories.
|
|
||||||
|
|
||||||
Building:
|
Building:
|
||||||
BORG_OPENSSL_PREFIX
|
BORG_OPENSSL_PREFIX
|
||||||
|
|
|
@ -465,18 +465,22 @@ def prune_split(archives, pattern, n, skip=[]):
|
||||||
return keep
|
return keep
|
||||||
|
|
||||||
|
|
||||||
def get_home_dir():
|
def get_base_dir():
|
||||||
"""Get user's home directory while preferring a possibly set HOME
|
"""Get home directory / base directory for borg:
|
||||||
environment variable
|
|
||||||
|
- BORG_BASE_DIR, if set
|
||||||
|
- HOME, if set
|
||||||
|
- ~$USER, if USER is set
|
||||||
|
- ~
|
||||||
"""
|
"""
|
||||||
|
base_dir = os.environ.get('BORG_BASE_DIR') or os.environ.get('HOME')
|
||||||
# os.path.expanduser() behaves differently for '~' and '~someuser' as
|
# os.path.expanduser() behaves differently for '~' and '~someuser' as
|
||||||
# parameters: when called with an explicit username, the possibly set
|
# parameters: when called with an explicit username, the possibly set
|
||||||
# environment variable HOME is no longer respected. So we have to check if
|
# environment variable HOME is no longer respected. So we have to check if
|
||||||
# it is set and only expand the user's home directory if HOME is unset.
|
# it is set and only expand the user's home directory if HOME is unset.
|
||||||
if os.environ.get('HOME', ''):
|
if not base_dir:
|
||||||
return os.environ.get('HOME')
|
base_dir = os.path.expanduser('~%s' % os.environ.get('USER', ''))
|
||||||
else:
|
return base_dir
|
||||||
return os.path.expanduser('~%s' % os.environ.get('USER', ''))
|
|
||||||
|
|
||||||
|
|
||||||
def get_keys_dir():
|
def get_keys_dir():
|
||||||
|
@ -502,7 +506,7 @@ def get_security_dir(repository_id=None):
|
||||||
|
|
||||||
def get_cache_dir():
|
def get_cache_dir():
|
||||||
"""Determine where to repository keys and cache"""
|
"""Determine where to repository keys and cache"""
|
||||||
xdg_cache = os.environ.get('XDG_CACHE_HOME', os.path.join(get_home_dir(), '.cache'))
|
xdg_cache = os.environ.get('XDG_CACHE_HOME', os.path.join(get_base_dir(), '.cache'))
|
||||||
cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(xdg_cache, 'borg'))
|
cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(xdg_cache, 'borg'))
|
||||||
if not os.path.exists(cache_dir):
|
if not os.path.exists(cache_dir):
|
||||||
os.makedirs(cache_dir)
|
os.makedirs(cache_dir)
|
||||||
|
@ -519,7 +523,7 @@ def get_cache_dir():
|
||||||
|
|
||||||
def get_config_dir():
|
def get_config_dir():
|
||||||
"""Determine where to store whole config"""
|
"""Determine where to store whole config"""
|
||||||
xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.join(get_home_dir(), '.config'))
|
xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.join(get_base_dir(), '.config'))
|
||||||
config_dir = os.environ.get('BORG_CONFIG_DIR', os.path.join(xdg_config, 'borg'))
|
config_dir = os.environ.get('BORG_CONFIG_DIR', os.path.join(xdg_config, 'borg'))
|
||||||
if not os.path.exists(config_dir):
|
if not os.path.exists(config_dir):
|
||||||
os.makedirs(config_dir)
|
os.makedirs(config_dir)
|
||||||
|
|
|
@ -23,7 +23,7 @@ from .compress import LZ4
|
||||||
from .constants import * # NOQA
|
from .constants import * # NOQA
|
||||||
from .helpers import Error, IntegrityError
|
from .helpers import Error, IntegrityError
|
||||||
from .helpers import bin_to_hex
|
from .helpers import bin_to_hex
|
||||||
from .helpers import get_home_dir
|
from .helpers import get_base_dir
|
||||||
from .helpers import get_limited_unpacker
|
from .helpers import get_limited_unpacker
|
||||||
from .helpers import hostname_is_unique
|
from .helpers import hostname_is_unique
|
||||||
from .helpers import replace_placeholders
|
from .helpers import replace_placeholders
|
||||||
|
@ -330,9 +330,9 @@ class RepositoryServer: # pragma: no cover
|
||||||
path = os.fsdecode(path)
|
path = os.fsdecode(path)
|
||||||
# Leading slash is always present with URI (ssh://), but not with short-form (who@host:path).
|
# Leading slash is always present with URI (ssh://), but not with short-form (who@host:path).
|
||||||
if path.startswith('/~/'): # /~/x = path x relative to home dir
|
if path.startswith('/~/'): # /~/x = path x relative to home dir
|
||||||
path = os.path.join(get_home_dir(), path[3:])
|
path = os.path.join(get_base_dir(), path[3:])
|
||||||
elif path.startswith('~/'):
|
elif path.startswith('~/'):
|
||||||
path = os.path.join(get_home_dir(), path[2:])
|
path = os.path.join(get_base_dir(), path[2:])
|
||||||
elif path.startswith('/~'): # /~username/x = relative to "user" home dir
|
elif path.startswith('/~'): # /~username/x = relative to "user" home dir
|
||||||
path = os.path.expanduser(path[1:])
|
path = os.path.expanduser(path[1:])
|
||||||
elif path.startswith('~'):
|
elif path.startswith('~'):
|
||||||
|
|
|
@ -18,7 +18,7 @@ from ..helpers import Buffer
|
||||||
from ..helpers import partial_format, format_file_size, parse_file_size, format_timedelta, format_line, PlaceholderError, replace_placeholders
|
from ..helpers import partial_format, format_file_size, parse_file_size, format_timedelta, format_line, PlaceholderError, replace_placeholders
|
||||||
from ..helpers import make_path_safe, clean_lines
|
from ..helpers import make_path_safe, clean_lines
|
||||||
from ..helpers import interval, prune_within, prune_split
|
from ..helpers import interval, prune_within, prune_split
|
||||||
from ..helpers import get_cache_dir, get_keys_dir, get_security_dir, get_config_dir
|
from ..helpers import get_base_dir, get_cache_dir, get_keys_dir, get_security_dir, get_config_dir
|
||||||
from ..helpers import is_slow_msgpack
|
from ..helpers import is_slow_msgpack
|
||||||
from ..helpers import yes, TRUISH, FALSISH, DEFAULTISH
|
from ..helpers import yes, TRUISH, FALSISH, DEFAULTISH
|
||||||
from ..helpers import StableDict, int_to_bigint, bigint_to_int, bin_to_hex
|
from ..helpers import StableDict, int_to_bigint, bigint_to_int, bin_to_hex
|
||||||
|
@ -447,6 +447,20 @@ class TestParseTimestamp(BaseTestCase):
|
||||||
self.assert_equal(parse_timestamp('2015-04-19T20:25:00'), datetime(2015, 4, 19, 20, 25, 0, 0, timezone.utc))
|
self.assert_equal(parse_timestamp('2015-04-19T20:25:00'), datetime(2015, 4, 19, 20, 25, 0, 0, timezone.utc))
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_base_dir(monkeypatch):
|
||||||
|
"""test that get_base_dir respects environment"""
|
||||||
|
monkeypatch.delenv('BORG_BASE_DIR', raising=False)
|
||||||
|
monkeypatch.delenv('HOME', raising=False)
|
||||||
|
monkeypatch.delenv('USER', raising=False)
|
||||||
|
assert get_base_dir() == os.path.expanduser('~')
|
||||||
|
monkeypatch.setenv('USER', 'root')
|
||||||
|
assert get_base_dir() == os.path.expanduser('~root')
|
||||||
|
monkeypatch.setenv('HOME', '/var/tmp/home')
|
||||||
|
assert get_base_dir() == '/var/tmp/home'
|
||||||
|
monkeypatch.setenv('BORG_BASE_DIR', '/var/tmp/base')
|
||||||
|
assert get_base_dir() == '/var/tmp/base'
|
||||||
|
|
||||||
|
|
||||||
def test_get_config_dir(monkeypatch):
|
def test_get_config_dir(monkeypatch):
|
||||||
"""test that get_config_dir respects environment"""
|
"""test that get_config_dir respects environment"""
|
||||||
monkeypatch.delenv('BORG_CONFIG_DIR', raising=False)
|
monkeypatch.delenv('BORG_CONFIG_DIR', raising=False)
|
||||||
|
|
|
@ -6,7 +6,7 @@ import time
|
||||||
from .crypto.key import KeyfileKey, KeyfileNotFoundError
|
from .crypto.key import KeyfileKey, KeyfileNotFoundError
|
||||||
from .constants import REPOSITORY_README
|
from .constants import REPOSITORY_README
|
||||||
from .helpers import ProgressIndicatorPercent
|
from .helpers import ProgressIndicatorPercent
|
||||||
from .helpers import get_home_dir, get_keys_dir, get_cache_dir
|
from .helpers import get_base_dir, get_keys_dir, get_cache_dir
|
||||||
from .locking import Lock
|
from .locking import Lock
|
||||||
from .logger import create_logger
|
from .logger import create_logger
|
||||||
from .repository import Repository, MAGIC
|
from .repository import Repository, MAGIC
|
||||||
|
@ -188,7 +188,7 @@ class AtticRepositoryUpgrader(Repository):
|
||||||
"""
|
"""
|
||||||
# copy of attic's get_cache_dir()
|
# copy of attic's get_cache_dir()
|
||||||
attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
|
attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
|
||||||
os.path.join(get_home_dir(),
|
os.path.join(get_base_dir(),
|
||||||
'.cache', 'attic'))
|
'.cache', 'attic'))
|
||||||
attic_cache_dir = os.path.join(attic_cache_dir, self.id_str)
|
attic_cache_dir = os.path.join(attic_cache_dir, self.id_str)
|
||||||
borg_cache_dir = os.path.join(get_cache_dir(), self.id_str)
|
borg_cache_dir = os.path.join(get_cache_dir(), self.id_str)
|
||||||
|
@ -249,7 +249,7 @@ class AtticKeyfileKey(KeyfileKey):
|
||||||
def get_keys_dir():
|
def get_keys_dir():
|
||||||
"""Determine where to repository keys and cache"""
|
"""Determine where to repository keys and cache"""
|
||||||
return os.environ.get('ATTIC_KEYS_DIR',
|
return os.environ.get('ATTIC_KEYS_DIR',
|
||||||
os.path.join(get_home_dir(), '.attic', 'keys'))
|
os.path.join(get_base_dir(), '.attic', 'keys'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_key_file(cls, repository):
|
def find_key_file(cls, repository):
|
||||||
|
@ -309,7 +309,7 @@ class Borg0xxKeyfileKey(KeyfileKey):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_keys_dir():
|
def get_keys_dir():
|
||||||
return os.environ.get('BORG_KEYS_DIR',
|
return os.environ.get('BORG_KEYS_DIR',
|
||||||
os.path.join(get_home_dir(), '.borg', 'keys'))
|
os.path.join(get_base_dir(), '.borg', 'keys'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_key_file(cls, repository):
|
def find_key_file(cls, repository):
|
||||||
|
|
Loading…
Reference in New Issue