backup_utils.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.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 Lesser General Public License along
  18. # with this program; if not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. import re
  22. import socket
  23. from PyQt5 import QtWidgets # pylint: disable=import-error
  24. import subprocess
  25. from . import utils
  26. import yaml
  27. path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
  28. path_max_len = 512
  29. def fill_appvms_list(dialog):
  30. """
  31. Helper function, designed to fill the destination vm combobox in both backup
  32. and restore GUI tools.
  33. :param dialog: QtGui.QWizard with a combobox called appvm_combobox
  34. """
  35. dialog.appvm_combobox.clear()
  36. dialog.appvm_combobox.addItem("dom0")
  37. dialog.appvm_combobox.setCurrentIndex(0) # current selected is null ""
  38. for vm in dialog.qubes_app.domains:
  39. if utils.get_feature(vm, 'internal', False) or vm.klass == 'TemplateVM':
  40. continue
  41. if utils.is_running(vm, False) and vm.klass != 'AdminVM':
  42. dialog.appvm_combobox.addItem(vm.name)
  43. def enable_dir_line_edit(dialog, boolean):
  44. dialog.dir_line_edit.setEnabled(boolean)
  45. dialog.select_path_button.setEnabled(boolean)
  46. def select_path_button_clicked(dialog, select_file=False, read_only=False):
  47. """
  48. Helper function that displays a file/directory selection wizard. Used by
  49. backup and restore GUI tools.
  50. :param dialog: QtGui.QWizard with a dir_line_edit text box that wants to
  51. receive a file/directory path and appvm_combobox with VM to use
  52. :param select_file: True: select file dialog; False: select directory
  53. dialog
  54. :param read_only: should the dir_line_edit be changed after selecting a file
  55. or directory
  56. :return:
  57. """
  58. backup_location = str(dialog.dir_line_edit.text())
  59. new_path = None
  60. new_appvm = str(dialog.appvm_combobox.currentText())
  61. vm = dialog.qubes_app.domains[new_appvm]
  62. try:
  63. if vm.name == socket.gethostname():
  64. file_dialog = QtWidgets.QFileDialog()
  65. if select_file:
  66. file_dialog_function = file_dialog.getOpenFileName
  67. else:
  68. file_dialog_function = file_dialog.getExistingDirectory
  69. result = file_dialog_function(
  70. dialog,
  71. dialog.tr("Select backup location."),
  72. backup_location if backup_location else '/')
  73. if isinstance(result, tuple):
  74. new_path = result[0]
  75. else:
  76. new_path = result
  77. else:
  78. try:
  79. new_path = utils.get_path_from_vm(
  80. vm,
  81. "qubes.SelectFile" if select_file
  82. else "qubes.SelectDirectory")
  83. except ValueError:
  84. QtWidgets.QMessageBox.warning(
  85. dialog,
  86. dialog.tr("Unexpected characters in path!"),
  87. dialog.tr("Backup path can only contain the following "
  88. "special characters: /:.,_+=() -"))
  89. except Exception as ex: # pylint: disable=broad-except
  90. QtWidgets.QMessageBox.warning(
  91. dialog,
  92. dialog.tr("Failed to select path!"),
  93. dialog.tr("Error {} occurred.".format(str(ex))))
  94. except subprocess.CalledProcessError:
  95. if not read_only:
  96. QtWidgets.QMessageBox.warning(
  97. dialog,
  98. dialog.tr("Nothing selected!"),
  99. dialog.tr("No file or directory selected."))
  100. else:
  101. return
  102. if new_path and not read_only:
  103. dialog.dir_line_edit.setText(new_path)
  104. if new_path and backup_location and not read_only:
  105. dialog.select_dir_page.completeChanged.emit()
  106. def get_profile_name(use_temp):
  107. backup_profile_name = 'qubes-manager-backup'
  108. temp_backup_profile_name = 'qubes-manager-backup-tmp'
  109. return temp_backup_profile_name if use_temp else backup_profile_name
  110. def get_profile_path(use_temp):
  111. path = '/etc/qubes/backup/' + get_profile_name(use_temp) + '.conf'
  112. return path
  113. def load_backup_profile(use_temp=False):
  114. path = get_profile_path(use_temp)
  115. with open(path) as profile_file:
  116. profile_data = yaml.safe_load(profile_file)
  117. return profile_data
  118. def write_backup_profile(args, use_temp=False):
  119. acceptable_fields = ['include', 'passphrase_text', 'compression',
  120. 'destination_vm', 'destination_path']
  121. profile_data = {key: value for key, value in args.items()
  122. if key in acceptable_fields}
  123. path = get_profile_path(use_temp)
  124. with open(path, 'w') as profile_file:
  125. yaml.safe_dump(profile_data, profile_file)