backup_utils.py 3.5 KB

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