backup_utils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python2
  2. # pylint: skip-file
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.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 Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import re
  23. from PyQt4 import QtGui
  24. from PyQt4 import QtCore
  25. import subprocess
  26. from . import utils
  27. import yaml
  28. path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
  29. path_max_len = 512
  30. # TODO: replace it with a more dynamic approach: allowing user to turn on or off
  31. # profile saving
  32. backup_profile_path = '/etc/qubes/backup/qubes-manager-backup.conf'
  33. def fill_appvms_list(dialog):
  34. dialog.appvm_combobox.clear()
  35. dialog.appvm_combobox.addItem("dom0")
  36. dialog.appvm_combobox.setCurrentIndex(0) # current selected is null ""
  37. for vm in dialog.qvm_collection.domains:
  38. if vm.klass == 'AppVM' and vm.features.get('internal', False):
  39. continue
  40. if vm.klass == 'TemplateVM' and vm.installed_by_rpm:
  41. continue
  42. # TODO: is the is_running criterion really necessary? It's designed to
  43. # avoid backuping a VM into itself or surprising the user with starting
  44. # a VM when they didn't plan to.
  45. # TODO: remove debug
  46. debug = True
  47. if (debug or vm.is_running()) and vm.qid != 0:
  48. dialog.appvm_combobox.addItem(vm.name)
  49. def enable_dir_line_edit(dialog, boolean):
  50. dialog.dir_line_edit.setEnabled(boolean)
  51. dialog.select_path_button.setEnabled(boolean)
  52. def select_path_button_clicked(dialog, select_file=False):
  53. backup_location = str(dialog.dir_line_edit.text())
  54. file_dialog = QtGui.QFileDialog()
  55. file_dialog.setReadOnly(True)
  56. new_path = None
  57. # TODO: check if dom0 is available
  58. new_appvm = str(dialog.appvm_combobox.currentText())
  59. vm = dialog.qvm_collection.domains[new_appvm]
  60. try:
  61. new_path = utils.get_path_from_vm(
  62. vm,
  63. "qubes.SelectFile" if select_file
  64. else "qubes.SelectDirectory")
  65. except subprocess.CalledProcessError as ex:
  66. QtGui.QMessageBox.warning(
  67. None,
  68. dialog.tr("Nothing selected!"),
  69. dialog.tr("No file or directory selected."))
  70. # TODO: check if this works for restore
  71. if new_path:
  72. dialog.dir_line_edit.setText(new_path)
  73. if new_path and len(backup_location) > 0:
  74. dialog.select_dir_page.emit(QtCore.SIGNAL("completeChanged()"))
  75. def load_backup_profile():
  76. with open(backup_profile_path) as profile_file:
  77. profile_data = yaml.safe_load(profile_file)
  78. return profile_data
  79. def write_backup_profile(args):
  80. '''Format limited backup profile (for GUI purposes and print it to
  81. *output_stream* (a file or stdout)
  82. :param output_stream: file-like object ro print the profile to
  83. :param args: dictionary with arguments
  84. :param passphrase: passphrase to use
  85. '''
  86. acceptable_fields = ['include', 'passphrase_text', 'compression',
  87. 'destination_vm', 'destination_path']
  88. profile_data = {key: value for key, value in args.items()
  89. if key in acceptable_fields}
  90. # TODO add compression parameter to GUI issue#943
  91. with open(backup_profile_path, 'w') as profile_file:
  92. yaml.safe_dump(profile_data, profile_file)