qvm-backup 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.qubesutils import backup_prepare, backup_do
  25. from optparse import OptionParser
  26. import os
  27. import sys
  28. def print_progress(progress):
  29. print >> sys.stderr, "\r-> Backing up files: {0}%...".format (progress),
  30. def main():
  31. usage = "usage: %prog [options] <backup-dir-path>"
  32. parser = OptionParser (usage)
  33. parser.add_option ("-x", "--exclude", action="append", dest="exclude_list",
  34. help="Exclude the specified VM from backup (might be repeated)")
  35. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  36. help="Force to run, even with root privileges")
  37. (options, args) = parser.parse_args ()
  38. if (len (args) != 1):
  39. print >> sys.stderr, "You must specify the target backup directory (e.g. /mnt/backup)"
  40. print >> sys.stderr, "qvm-backup will create a subdirectory there for each individual backup."
  41. exit (0)
  42. base_backup_dir = args[0]
  43. if os.geteuid() == 0:
  44. if not options.force_root:
  45. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  46. print >> sys.stderr, "Retry as unprivileged user."
  47. print >> sys.stderr, "... or use --force-root to continue anyway."
  48. exit(1)
  49. # Only for locking
  50. qvm_collection = QubesVmCollection()
  51. qvm_collection.lock_db_for_reading()
  52. qvm_collection.load()
  53. files_to_backup = None
  54. try:
  55. files_to_backup = backup_prepare(base_backup_dir, exclude_list=options.exclude_list)
  56. except QubesException as e:
  57. print >>sys.stderr, "ERROR: %s" % str(e)
  58. exit(1)
  59. prompt = raw_input ("Do you want to proceed? [y/N] ")
  60. if not (prompt == "y" or prompt == "Y"):
  61. exit (0)
  62. try:
  63. backup_do(base_backup_dir, files_to_backup, progress_callback=print_progress)
  64. except QubesException as e:
  65. print >>sys.stderr, "ERROR: %s" % str(e)
  66. exit(1)
  67. print
  68. print "-> Backup completed."
  69. qvm_collection.unlock_db()
  70. main()