backup_utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. def fill_appvms_list(dialog):
  31. dialog.appvm_combobox.clear()
  32. dialog.appvm_combobox.addItem("dom0")
  33. dialog.appvm_combobox.setCurrentIndex(0) # current selected is null ""
  34. for vm in dialog.qvm_collection.domains:
  35. if vm.klass == 'AppVM' and vm.features.get('internal', False):
  36. continue
  37. if vm.klass == 'TemplateVM' and vm.installed_by_rpm:
  38. continue
  39. if vm.is_running() and vm.qid != 0:
  40. dialog.appvm_combobox.addItem(vm.name)
  41. def enable_dir_line_edit(dialog, boolean):
  42. dialog.dir_line_edit.setEnabled(boolean)
  43. dialog.select_path_button.setEnabled(boolean)
  44. def select_path_button_clicked(dialog, select_file=False):
  45. backup_location = str(dialog.dir_line_edit.text())
  46. file_dialog = QtGui.QFileDialog()
  47. file_dialog.setReadOnly(True)
  48. new_path = None
  49. new_appvm = str(dialog.appvm_combobox.currentText())
  50. vm = dialog.qvm_collection.domains[new_appvm]
  51. try:
  52. new_path = utils.get_path_from_vm(
  53. vm,
  54. "qubes.SelectFile" if select_file
  55. else "qubes.SelectDirectory")
  56. except subprocess.CalledProcessError as ex:
  57. QtGui.QMessageBox.warning(
  58. None,
  59. dialog.tr("Nothing selected!"),
  60. dialog.tr("No file or directory selected."))
  61. if new_path:
  62. dialog.dir_line_edit.setText(new_path)
  63. if new_path and len(backup_location) > 0:
  64. dialog.select_dir_page.emit(QtCore.SIGNAL("completeChanged()"))
  65. def load_backup_profile(use_temp=False):
  66. path = get_profile_path(use_temp)
  67. with open(path) as profile_file:
  68. profile_data = yaml.safe_load(profile_file)
  69. return profile_data
  70. def write_backup_profile(args, use_temp=False):
  71. acceptable_fields = ['include', 'passphrase_text', 'compression',
  72. 'destination_vm', 'destination_path']
  73. profile_data = {key: value for key, value in args.items()
  74. if key in acceptable_fields}
  75. path = get_profile_path(use_temp)
  76. # TODO add compression parameter to GUI issue#943
  77. with open(path, 'w') as profile_file:
  78. yaml.safe_dump(profile_data, profile_file)
  79. def get_profile_name(use_temp):
  80. backup_profile_name = 'qubes-manager-backup'
  81. temp_backup_profile_name = 'qubes-manager-backup-tmp'
  82. return temp_backup_profile_name if use_temp else backup_profile_name
  83. def get_profile_path(use_temp):
  84. path = '/etc/qubes/backup/' + get_profile_name(use_temp) + '.conf'
  85. return path