qvm-backup 7.2 KB

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