qvm-backup 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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> [vmname ...]"
  34. parser = OptionParser (usage)
  35. parser.add_option ("-x", "--exclude", action="append", dest="exclude_list",
  36. help="Exclude the specified VM from backup (might be repeated)")
  37. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  38. help="Force to run, even with root privileges")
  39. parser.add_option ("-d", "--dest-vm", action="store", dest="appvm",
  40. help="The AppVM to send backups to")
  41. parser.add_option ("-e", "--encrypt", action="store_true", dest="encrypt", default=False,
  42. help="Encrypts the backup")
  43. parser.add_option ("-z", "--compress", action="store_true", dest="compress", default=False,
  44. help="Compress the backup")
  45. (options, args) = parser.parse_args ()
  46. if (len (args) < 1):
  47. print >> sys.stderr, "You must specify the target backup directory (e.g. /mnt/backup)"
  48. print >> sys.stderr, "qvm-backup will create a subdirectory there for each individual backup."
  49. exit (0)
  50. base_backup_dir = args[0]
  51. if os.geteuid() == 0:
  52. if not options.force_root:
  53. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  54. print >> sys.stderr, "Retry as unprivileged user."
  55. print >> sys.stderr, "... or use --force-root to continue anyway."
  56. exit(1)
  57. # Only for locking
  58. qvm_collection = QubesVmCollection()
  59. qvm_collection.lock_db_for_reading()
  60. qvm_collection.load()
  61. vms = None
  62. if (len (args) > 1):
  63. vms = [qvm_collection.get_vm_by_name(vmname) for vmname in args[1:]]
  64. files_to_backup = None
  65. try:
  66. files_to_backup = backup_prepare(
  67. vms_list=vms,
  68. exclude_list=options.exclude_list,
  69. hide_vm_names=options.encrypt)
  70. except QubesException as e:
  71. print >>sys.stderr, "ERROR: %s" % str(e)
  72. exit(1)
  73. total_backup_sz = reduce(lambda size, file: size+file["size"],
  74. files_to_backup, 0)
  75. if not options.appvm:
  76. appvm = None
  77. stat = os.statvfs(base_backup_dir)
  78. backup_fs_free_sz = stat.f_bsize * stat.f_bavail
  79. print
  80. if (total_backup_sz > backup_fs_free_sz):
  81. print >>sys.stderr, "ERROR: Not enough space available on the backup filesystem!"
  82. exit(1)
  83. print "-> Available space: {0}".format(size_to_human(backup_fs_free_sz))
  84. else:
  85. appvm = qvm_collection.get_vm_by_name(options.appvm)
  86. if appvm is None:
  87. print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
  88. exit(1)
  89. stat = os.statvfs('/var/tmp')
  90. backup_fs_free_sz = stat.f_bsize * stat.f_bavail
  91. print
  92. if (backup_fs_free_sz < 1000000000):
  93. print >>sys.stderr, "ERROR: Not enough space available " \
  94. "on the local filesystem (needs 1GB for temporary files)!"
  95. exit(1)
  96. prompt = raw_input ("Do you want to proceed? [y/N] ")
  97. if not (prompt == "y" or prompt == "Y"):
  98. exit (0)
  99. passphrase = getpass.getpass("Please enter the pass phrase that will be used to encrypt/verify the backup: ")
  100. passphrase2 = getpass.getpass("Enter again for verification: ")
  101. if passphrase != passphrase2:
  102. print >>sys.stderr, "ERROR: Password mismatch"
  103. exit(1)
  104. try:
  105. backup_do(base_backup_dir, files_to_backup, passphrase,
  106. progress_callback=print_progress,
  107. encrypt=options.encrypt,
  108. compress=options.compress,
  109. appvm=appvm)
  110. except QubesException as e:
  111. print >>sys.stderr, "ERROR: %s" % str(e)
  112. exit(1)
  113. print
  114. print "-> Backup completed."
  115. qvm_collection.unlock_db()
  116. main()