qvm-backup 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection
  24. from qubes.qubes import QubesException
  25. from qubes.backup import backup_prepare, backup_do
  26. from qubes.qubesutils import size_to_human
  27. from optparse import OptionParser
  28. import os
  29. import sys
  30. import getpass
  31. def print_progress(progress):
  32. print >> sys.stderr, "\r-> Backing up files: {0}%...".format (progress),
  33. def main():
  34. usage = "usage: %prog [options] <backup-dir-path> [vms-to-be-included ...]"
  35. parser = OptionParser (usage)
  36. parser.add_option ("-x", "--exclude", action="append",
  37. dest="exclude_list", default=[],
  38. help="Exclude the specified VM from backup (may be "
  39. "repeated)")
  40. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  41. help="Force to run, even with root privileges")
  42. parser.add_option ("-d", "--dest-vm", action="store", dest="appvm",
  43. help="The AppVM to send backups to (implies -e)")
  44. parser.add_option ("-e", "--encrypt", action="store_true", dest="encrypt", default=False,
  45. help="Encrypts the backup")
  46. parser.add_option ("--no-encrypt", action="store_true",
  47. dest="no_encrypt", default=False,
  48. help="Skip encryption even if sending the backup to VM")
  49. parser.add_option ("-E", "--enc-algo", action="store",
  50. dest="crypto_algorithm", default=None,
  51. help="Specify non-default encryption algorithm. For "
  52. "list of supported algos execute 'openssl "
  53. "list-cipher-algorithms' (implies -e)")
  54. parser.add_option ("-H", "--hmac-algo", action="store",
  55. dest="hmac_algorithm", default=None,
  56. help="Specify non-default hmac algorithm. For list of "
  57. "supported algos execute 'openssl "
  58. "list-message-digest-algorithms'")
  59. parser.add_option ("-z", "--compress", action="store_true", dest="compress", default=False,
  60. help="Compress the backup")
  61. (options, args) = parser.parse_args ()
  62. if (len (args) < 1):
  63. print >> sys.stderr, "You must specify the target backup directory (e.g. /mnt/backup)"
  64. print >> sys.stderr, "qvm-backup will create a subdirectory there for each individual backup."
  65. exit (0)
  66. base_backup_dir = args[0]
  67. if os.geteuid() == 0:
  68. if not options.force_root:
  69. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  70. print >> sys.stderr, "Retry as unprivileged user."
  71. print >> sys.stderr, "... or use --force-root to continue anyway."
  72. exit(1)
  73. # Only for locking
  74. qvm_collection = QubesVmCollection()
  75. qvm_collection.lock_db_for_reading()
  76. qvm_collection.load()
  77. vms = None
  78. if (len (args) > 1):
  79. vms = [qvm_collection.get_vm_by_name(vmname) for vmname in args[1:]]
  80. if options.appvm:
  81. options.exclude_list.append(options.appvm)
  82. if options.appvm or options.crypto_algorithm:
  83. options.encrypt = True
  84. if options.no_encrypt:
  85. options.encrypt = False
  86. files_to_backup = None
  87. try:
  88. files_to_backup = backup_prepare(
  89. vms_list=vms,
  90. exclude_list=options.exclude_list,
  91. hide_vm_names=options.encrypt)
  92. except QubesException as e:
  93. print >>sys.stderr, "ERROR: %s" % str(e)
  94. exit(1)
  95. total_backup_sz = reduce(lambda size, file: size+file["size"],
  96. files_to_backup, 0)
  97. if not options.appvm:
  98. appvm = None
  99. if os.path.isdir(base_backup_dir):
  100. stat = os.statvfs(base_backup_dir)
  101. else:
  102. stat = os.statvfs(os.path.dirname(base_backup_dir))
  103. backup_fs_free_sz = stat.f_bsize * stat.f_bavail
  104. print
  105. if (total_backup_sz > backup_fs_free_sz):
  106. print >>sys.stderr, "ERROR: Not enough space available on the backup filesystem!"
  107. exit(1)
  108. print "-> Available space: {0}".format(size_to_human(backup_fs_free_sz))
  109. else:
  110. appvm = qvm_collection.get_vm_by_name(options.appvm)
  111. if appvm is None:
  112. print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
  113. exit(1)
  114. stat = os.statvfs('/var/tmp')
  115. backup_fs_free_sz = stat.f_bsize * stat.f_bavail
  116. print
  117. if (backup_fs_free_sz < 1000000000):
  118. print >>sys.stderr, "ERROR: Not enough space available " \
  119. "on the local filesystem (needs 1GB for temporary files)!"
  120. exit(1)
  121. if not appvm.is_running():
  122. appvm.start(verbose=True)
  123. if options.appvm:
  124. print >>sys.stderr, ("WARNING: VM {} excluded because it's used to "
  125. "store the backup.").format(options.appvm)
  126. options.exclude_list.append(options.appvm)
  127. if not options.encrypt:
  128. print >>sys.stderr, "WARNING: encryption will not be used"
  129. prompt = raw_input ("Do you want to proceed? [y/N] ")
  130. if not (prompt == "y" or prompt == "Y"):
  131. exit (0)
  132. if options.encrypt:
  133. passphrase = getpass.getpass("Please enter the pass phrase that will "
  134. "be used to encrypt and verify the "
  135. "backup: ")
  136. else:
  137. passphrase = getpass.getpass("Please enter the pass phrase that will "
  138. "be used to verify the backup: ")
  139. passphrase2 = getpass.getpass("Enter again for verification: ")
  140. if passphrase != passphrase2:
  141. print >>sys.stderr, "ERROR: Password mismatch"
  142. exit(1)
  143. passphrase = passphrase.decode(sys.stdin.encoding)
  144. kwargs = {}
  145. if options.hmac_algorithm:
  146. kwargs['hmac_algorithm'] = options.hmac_algorithm
  147. if options.crypto_algorithm:
  148. kwargs['crypto_algorithm'] = options.crypto_algorithm
  149. try:
  150. backup_do(base_backup_dir, files_to_backup, passphrase,
  151. progress_callback=print_progress,
  152. encrypted=options.encrypt,
  153. compressed=options.compress,
  154. appvm=appvm, **kwargs)
  155. except QubesException as e:
  156. print >>sys.stderr, "ERROR: %s" % str(e)
  157. exit(1)
  158. print
  159. print "-> Backup completed."
  160. qvm_collection.unlock_db()
  161. main()