Skip to content

Commit

Permalink
profile_id detection for non-TurnKey systems
Browse files Browse the repository at this point in the history
  • Loading branch information
lirazsiri committed Nov 14, 2013
1 parent cd00ccb commit b666703
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 156 deletions.
4 changes: 2 additions & 2 deletions cmd_backup.py
Expand Up @@ -144,7 +144,7 @@
from registry import registry, update_profile, hub_backups
from conf import Conf

from version import Version
from version import detect_profile_id
from stdtrap import UnitedStdTrap

from utils import is_writeable, fmt_title, fmt_timestamp
Expand Down Expand Up @@ -373,7 +373,7 @@ def main():

if not registry.hbr:
registry.hbr = hb.new_backup_record(registry.key,
str(Version.from_system()),
detect_profile_id(),
get_server_id())

conf.address = registry.hbr.address
Expand Down
21 changes: 12 additions & 9 deletions cmd_init.py
Expand Up @@ -12,11 +12,13 @@
"""
Initialization (start here)
By default, this links TKLBAM to your Hub account and downloads a backup profile,
which is used to calculate the list of system changes we need to backup.
The profile usually describes the installation state of a TurnKey appliance and
contains a list of packages, filesystem paths to scan for changes and an index of
the contents of those paths which records timestamps, ownership and permissions.
By default, this links TKLBAM to your Hub account and downloads an appropriate
backup profile, which is used to calculate the list of system changes we need
to backup. On a TurnKey system the profile describes the installation state of
the appliance and contains a list of packages, filesystem paths to scan for
changes and an index of the contents of those paths which records timestamps,
ownership and permissions. On a non-TurnKey system the default backup profile
will not describe installation state, only a list of directories to backup.
Arguments:
Expand All @@ -29,11 +31,12 @@
--force-profile=PROFILE_ID Force a specific backup profile
(e.g., "core", "turnkey-core-13.0-wheezy-amd64")
Default value: String in /etc/turnkey_version
Without --force-profile the profile_id is
automatically detected.
Special value: "empty" creates an empty
backup profile. Backup configurations will
only be taken from /etc/tklbam.
--force-profile=empty "empty" is a special profile_id value that creates an empty
backup profile. Backup configurations will only be taken
from /etc/tklbam.
--force-profile=PATH Path to a custom backup profile
Details: tklbam-internal create-profile --help
Expand Down
106 changes: 0 additions & 106 deletions cmd_internals/cmd_detect_profile.py

This file was deleted.

41 changes: 41 additions & 0 deletions cmd_internals/cmd_detect_profile_id.py
@@ -0,0 +1,41 @@
#!/usr/bin/python
#
# Copyright (c) 2013 Liraz Siri <liraz@turnkeylinux.org>
#
# This file is part of TKLBAM (TurnKey Linux BAckup and Migration).
#
# TKLBAM is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
"""
Determine your system's default backup profile_id
"""
import sys
import getopt

from version import detect_profile_id

def usage(e=None):
if e:
print >> sys.stderr, "error: " + str(e)
print >> sys.stderr, "Syntax: %s [ path/to/root ]" % sys.argv[0]
sys.exit(1)

def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'h', ['help'])
except getopt.GetoptError, e:
usage(e)

for opt, val in opts:
if opt in ('-h', '--help'):
usage()

root = args[0] if args else '/'

print detect_profile_id(root)

if __name__ == "__main__":
main()
5 changes: 4 additions & 1 deletion cmd_list.py
Expand Up @@ -18,7 +18,7 @@
%id Backup id
%label Descriptive label
%turnkey_version Appliance version code
%profile_id Appliance version code
%server_id Associated server id (- if empty)
%created Date the backup record was created
%updated Date the backup record was last updated
Expand Down Expand Up @@ -63,6 +63,9 @@ def __init__(self, format):
self.tpl = string.Template(tpl)

def __call__(self, hbr):
# backwards compat. hack
hbr = hbr.copy()
hbr['turnkey_version'] = hbr['profile_id']
return self.tpl.substitute(**hbr)

def key_has_passphrase(key):
Expand Down
22 changes: 15 additions & 7 deletions cmd_restore.py
Expand Up @@ -151,7 +151,7 @@

from registry import registry, update_profile, hub_backups

from version import Version
from version import TurnKeyVersion
from utils import is_writeable, fmt_timestamp, fmt_title

from conf import Conf
Expand All @@ -169,10 +169,18 @@ class ExitCode:
INCOMPATIBLE = 10
BADPASSPHRASE = 11

def do_compatibility_check(backup_turnkey_version, interactive=True):
def do_compatibility_check(backup_profile_id, interactive=True):
# unless both backup and restore are TurnKey skip compatibility check
try:
backup_codename = TurnKeyVersion.from_string(backup_profile_id).codename
except TurnKeyVersion.Error:
return

backup_codename = Version.from_string(backup_turnkey_version).codename
local_codename = Version.from_system().codename
turnkey_version = TurnKeyVersion.from_system()
if not turnkey_version:
return

local_codename = turnkey_version.codename

if local_codename == backup_codename:
return
Expand All @@ -186,8 +194,8 @@ def fmt(s):
print "WARNING: INCOMPATIBLE APPLIANCE BACKUP"
print "======================================"
print
print "Restoring a %s backup to a %s appliance is not recommended." % (backup_codename, local_codename)
print "For best results, restore to a fresh %s installation instead." % backup_codename
print "Restoring a %s backup to a %s appliance may create complications." % (backup_codename, local_codename)
print "For best results try restoring instead to a fresh %s installation." % backup_codename

if not interactive:
sys.exit(ExitCode.INCOMPATIBLE)
Expand Down Expand Up @@ -434,7 +442,7 @@ def main():

if hbr:
if not opt_force and not raw_download_path:
do_compatibility_check(hbr.turnkey_version, interactive)
do_compatibility_check(hbr.profile_id, interactive)

if opt_key and \
keypacket.fingerprint(hbr.key) != keypacket.fingerprint(opt_key):
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Expand Up @@ -7,6 +7,6 @@ Standards-Version: 3.6.1

Package: tklbam
Architecture: any
Depends: python (>= 2.4), tklbam-squid (>= 2.7.STABLE9-2.1turnkey+25), tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.5), turnkey-version, python-crypto, pycurl-wrapper (>= 1.2), python-simplejson, ca-certificates, ntpdate
Depends: python (>= 2.4), tklbam-squid (>= 2.7.STABLE9-2.1turnkey+25), tklbam-duplicity (>= 0.6.18), tklbam-python-boto (>= 2.3.0), turnkey-pylib (>= 0.5), python-crypto, pycurl-wrapper (>= 1.2), python-simplejson, ca-certificates, ntpdate
Section: misc
Description: TurnKey Linux Backup and Migration agent
16 changes: 8 additions & 8 deletions dummyhub.py
Expand Up @@ -78,12 +78,12 @@ def subscribe(self):
def unsubscribe(self):
self.credentials = None

def new_backup(self, address, key, turnkey_version, server_id=None):
def new_backup(self, address, key, profile_id, server_id=None):
self.backups_max += 1

id = str(self.backups_max)
backup_record = DummyBackupRecord(id, address, key, \
turnkey_version, server_id)
profile_id, server_id)

self.backups[id] = backup_record

Expand Down Expand Up @@ -142,11 +142,11 @@ def _parse_duplicity_sessions(path):

class DummyBackupRecord(AttrDict):
# backup_id, address
def __init__(self, backup_id, address, key, turnkey_version, server_id):
def __init__(self, backup_id, address, key, profile_id, server_id):
self.backup_id = backup_id
self.address = address
self.key = key
self.turnkey_version = turnkey_version
self.profile_id = profile_id
self.server_id = server_id

self.created = datetime.now()
Expand Down Expand Up @@ -218,8 +218,8 @@ def add_user(self):
return user


def get_profile(self, turnkey_version):
matches = glob.glob("%s/%s.tar.*" % (self.path.profiles, turnkey_version))
def get_profile(self, profile_id):
matches = glob.glob("%s/%s.tar.*" % (self.path.profiles, profile_id))
if not matches:
return None

Expand Down Expand Up @@ -308,15 +308,15 @@ def get_new_profile(self, profile_id, profile_timestamp):

return DummyProfileArchive(profile_id, archive, archive_timestamp)

def new_backup_record(self, key, turnkey_version, server_id=None):
def new_backup_record(self, key, profile_id, server_id=None):
# in the real implementation the hub would create a bucket not a dir...
# the real implementation would have to make sure this is unique
path = "/var/tmp/duplicity/" + base64.b32encode(os.urandom(10))
os.makedirs(path)
address = "file://" + path

backup_record = self.user.new_backup(address, key,
turnkey_version, server_id)
profile_id, server_id)

dummydb.save()

Expand Down
6 changes: 3 additions & 3 deletions hub.py
Expand Up @@ -139,7 +139,7 @@ def __init__(self, response):
self.address = response['address']
self.backup_id = response['backup_id']
self.server_id = response['server_id']
self.turnkey_version = response['turnkey_version']
self.profile_id = response['turnkey_version']

self.created = self._parse_datetime(response['date_created'])
self.updated = self._parse_datetime(response['date_updated'])
Expand Down Expand Up @@ -215,8 +215,8 @@ def get_new_profile(self, profile_id, profile_timestamp):

return ProfileArchive(profile_id, archive_path, archive_timestamp)

def new_backup_record(self, key, turnkey_version, server_id=None):
attrs = {'key': key, 'turnkey_version': turnkey_version}
def new_backup_record(self, key, profile_id, server_id=None):
attrs = {'key': key, 'turnkey_version': profile_id}
if server_id:
attrs['server_id'] = server_id

Expand Down

0 comments on commit b666703

Please sign in to comment.