BackupProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Backup\Providers;
  20. use MGProvision\Proxmox\v2\models\File;
  21. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RestoreVm;
  22. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  27. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  28. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  29. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  30. class BackupProvider extends BaseDataProvider implements ClientArea
  31. {
  32. use ProductService;
  33. use ApiService;
  34. public function read()
  35. {
  36. if ($this->actionElementId)
  37. {
  38. $this->data = json_decode(base64_decode($this->actionElementId), true);
  39. }
  40. $this->availableValues['compress'] = [
  41. "0" => sl("lang")->abtr("None"),
  42. "lzo" => sl("lang")->abtr("LZO (fast)"),
  43. "gzip" => sl("lang")->abtr("GZIP (good)"),
  44. "zstd" => sl("lang")->abtr("ZSTD (fast and good)")
  45. ];
  46. $optionBackupCompress = $this->configuration()->getPermissionBackupCompress();
  47. if(empty($optionBackupCompress)){
  48. return;
  49. }
  50. foreach ($this->availableValues['compress'] as $k => $option){
  51. if($k=="0" && !in_array("none", $optionBackupCompress)){
  52. unset($this->availableValues['compress'][$k]);
  53. }
  54. if(!in_array($k, $optionBackupCompress)){
  55. unset($this->availableValues['compress'][$k]);
  56. }
  57. }
  58. }
  59. public function create()
  60. {
  61. $this->acl()->backup();
  62. $this->resourceGuard()->backupLimit();
  63. $storage = $this->configuration()->getBackupStorage();
  64. $routing = $this->configuration()->isBackupRouting() ? 1 : 0;
  65. $maxFiles = $this->configuration()->getBackupMaxFiles();
  66. if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
  67. $maxFiles = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
  68. }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
  69. $maxFiles = null;
  70. }
  71. if($this->vm()->isLockBackup()){
  72. return (new HtmlDataJsonResponse())
  73. ->setStatusError()
  74. ->setMessageAndTranslate('The backup creation is in progress');
  75. }
  76. $this->vm()->backup($storage, $routing, $maxFiles, $this->formData['compress'], $this->formData['mode']);
  77. return (new HtmlDataJsonResponse())
  78. ->setStatusSuccess()
  79. ->setMessageAndTranslate('The backup creation is in progress')
  80. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  81. ->setCallBackFunction('pmToggleButton')
  82. ->addRefreshTargetId('resourcesContainer')
  83. ->addRefreshTargetId('backupDataTable');
  84. }
  85. public function restore()
  86. {
  87. queue(RestoreVm::class, ['volid' => $this->formData['volid']], null, 'hosting', $this->getWhmcsParamByKey('serviceid'));
  88. return (new HtmlDataJsonResponse())
  89. ->setStatusSuccess()
  90. ->setMessageAndTranslate('The virtual machine restore is in progress');
  91. }
  92. public function update()
  93. {
  94. }
  95. public function delete()
  96. {
  97. $this->acl()->backup();
  98. $storage = $this->configuration()->getBackupStorage();
  99. $volid = $this->formData['volid'];
  100. $backupFile = new File();
  101. $backupFile->setApi($this->api());
  102. $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
  103. $backupFile->delete();
  104. return (new HtmlDataJsonResponse())
  105. ->setStatusSuccess()
  106. ->setMessageAndTranslate('The backup has been deleted successfully')
  107. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  108. ->setCallBackFunction('pmToggleButton')
  109. ->addRefreshTargetId('resourcesContainer')
  110. ->addRefreshTargetId('backupDataTable');
  111. }
  112. public function deleteMass()
  113. {
  114. $this->acl()->backup();
  115. $storage = $this->configuration()->getBackupStorage();
  116. $backupFile = new File();
  117. $backupFile->setApi($this->api());
  118. foreach ($this->request->get('massActions') as $id)
  119. {
  120. $data = json_decode(base64_decode($id), true);
  121. $volid = $data['volid'];
  122. $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
  123. $backupFile->delete();
  124. }
  125. return (new HtmlDataJsonResponse())
  126. ->setStatusSuccess()
  127. ->setMessageAndTranslate('The backups have been deleted successfully')
  128. ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
  129. ->setCallBackFunction('pmToggleButton')
  130. ->addRefreshTargetId('resourcesContainer')
  131. ->addRefreshTargetId('backupDataTable');
  132. }
  133. }