BackupRemove.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Cron;
  3. use MGProvision\Proxmox\v2\repository\FileRepository;
  4. use ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration;
  5. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  6. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  7. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  8. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  9. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  10. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  11. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server;
  12. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use function ModulesGarden\ProxmoxAddon\Core\Helper\di;
  17. use ModulesGarden\ProxmoxAddon\App\Enum\Vps;
  18. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud;
  19. class BackupRemove extends Command
  20. {
  21. use WhmcsParams;
  22. use ProductService;
  23. use ApiService;
  24. /**
  25. * Command name
  26. * @var string
  27. */
  28. protected $name = 'remove-backups';
  29. /**
  30. * Command description
  31. * @var string
  32. */
  33. protected $description = 'Updating backups';
  34. /**
  35. * Command help text
  36. * @var string
  37. */
  38. protected $help = '';
  39. protected $input;
  40. protected $output;
  41. protected $io;
  42. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  43. {
  44. $this->input = $input;
  45. $this->output = $output;
  46. $this->io = $io;
  47. $io->title('Update Backups: Starting');
  48. if (!function_exists('ModuleBuildParams'))
  49. {
  50. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  51. }
  52. $pc = (new ProductConfiguration())->getTable();
  53. $productIds = ProductConfiguration::pluck("product_id")
  54. ->all();
  55. if (empty($productIds))
  56. {
  57. $io->error("Store The Backup For X Days is not configured");
  58. return;
  59. }
  60. $h = (new Hosting())->getTable();
  61. $s = (new Server())->getTable();
  62. $hostings = Hosting::select("{$h}.id", "{$h}.packageid", "{$h}.lastupdate", "{$h}.bwusage", "{$h}.bwlimit", "{$h}.regdate")
  63. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  64. ->where("{$h}.domainstatus", "Active")
  65. ->whereIn("{$h}.packageid", $productIds)
  66. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps" ])
  67. ->orderBy("{$h}.server");
  68. $i = 0;
  69. /**
  70. * @var Hosting $hosting
  71. */
  72. foreach ($hostings->get() as $hosting)
  73. {
  74. $i++;
  75. $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
  76. try
  77. {
  78. $params = \ModuleBuildParams($hosting->id);
  79. $method = lcfirst($params['moduletype'])."Process";
  80. if(!method_exists($this, $method )){
  81. continue;
  82. }
  83. $this->{$method}($params);
  84. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  85. }
  86. catch (\Exception $ex)
  87. {
  88. $msg = $ex->getMessage();
  89. if ($hosting)
  90. {
  91. $msg = sprintf("Hosting Id #%s, %s", $hosting->id, $ex->getMessage());
  92. }
  93. $io->error($msg);
  94. }
  95. (new Hypervisor($this->getName(), $input->getOptions()))
  96. ->ping();
  97. }
  98. $output->writeln("");
  99. $io->success([
  100. sprintf("Synchronize hostings: %s Entries Processed.", $i),
  101. "Update Backups: Done"
  102. ]);
  103. }
  104. private function proxmoxVPSProcess($params){
  105. if (!$params['customfields'][Vps\CustomField::VMID])
  106. {
  107. throw new \Exception("Custom Field \"vmid\" is empty");
  108. }
  109. if (!$params['customfields'][Vps\CustomField::NODE])
  110. {
  111. throw new \Exception("Custom Field \"node\" is empty");
  112. }
  113. $whmcsParams = di('whmcsParams');
  114. $whmcsParams->setParams($params);
  115. unset($this->vm, $this->api, $this->configuration);
  116. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  117. $fileRepository = new FileRepository();
  118. $fileRepository->setApi($this->api());
  119. $fileRepository->findBackup($this->vm())
  120. ->findByStorages([$storage]);
  121. if($this->configuration()->getBackupStoreDays()){
  122. foreach ($fileRepository->fetch() as $id => $backup)
  123. {
  124. $matches = [];
  125. preg_match('/[0-9]{4}_[0-9]{2}_[0-9]{2}/', $backup->getVolid(), $matches);
  126. $dateCreated = str_replace("_", "-", $matches[0]);
  127. if (!$dateCreated)
  128. {
  129. continue;
  130. }
  131. $dateCreated = new \DateTime($dateCreated);
  132. $now = new \DateTime();
  133. $dDiff = $dateCreated->diff($now);
  134. if ($dDiff->days > $this->configuration()->getBackupStoreDays())
  135. {
  136. $backup->delete();
  137. $fileRepository->remove($id);
  138. $this->output->writeln(sprintf("Backup with date %s has been deleted", $params['serviceid'], $backup->getDate()));
  139. }
  140. }
  141. }
  142. $sizeMax = $this->getWhmcsConfigOption(Vps\ConfigurableOption::BACKUPS_SIZE, $this->configuration()->getBackupMaxSize());
  143. if($sizeMax && $sizeMax!="-1" && $fileRepository->getSizeInGb() > $sizeMax){
  144. $this->output->writeln(sprintf("The maximum size set for a backup has been exceeded of %s GB", $fileRepository->getSizeInGb()- $sizeMax ));
  145. foreach ($fileRepository->sortByTime()->fetch() as $id => &$backup)
  146. {
  147. if($fileRepository->getSizeInGb() <= $sizeMax){
  148. break;
  149. }
  150. $this->output->writeln(sprintf("Backup with date %s %s has been deleted", $backup->getDate(), $backup->getHour()));
  151. $fileRepository->remove($id);
  152. // $backup->delete();
  153. }
  154. }
  155. $limitMax = $this->getWhmcsConfigOption(Vps\ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles());
  156. if($limitMax && $limitMax!="-1" && $fileRepository->count() > $limitMax){
  157. $this->output->writeln(sprintf("The maximum number set for a backup has been exceeded of %s", $fileRepository->count() - $limitMax ));
  158. foreach ($fileRepository->sortByTime()->fetch() as $id => &$backup)
  159. {
  160. if($fileRepository->count() <= $limitMax){
  161. break;
  162. }
  163. $this->output->writeln(sprintf("Backup with date %s %s has been deleted", $params['serviceid'], $backup->getDate(), $backup->getHour()));
  164. $fileRepository->remove($id);
  165. $backup->delete();
  166. }
  167. }
  168. }
  169. private function proxmoxCloudVpsProcess($params){
  170. $whmcsParams = di('whmcsParams');
  171. $whmcsParams->setParams($params);
  172. unset($this->vm, $this->api, $this->configuration);
  173. $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
  174. $fileRepository = new FileRepository();
  175. $fileRepository->setApi($this->api());
  176. $fileRepository->findBackupByVmModel(VmModel::ofHostingId($params['serviceid'])->get())
  177. ->findByStorages([$storage]);
  178. if($this->configuration()->getBackupStoreDays()){
  179. foreach ($fileRepository->fetch() as $id => $backup)
  180. {
  181. $matches = [];
  182. preg_match('/[0-9]{4}_[0-9]{2}_[0-9]{2}/', $backup->getVolid(), $matches);
  183. $dateCreated = str_replace("_", "-", $matches[0]);
  184. if (!$dateCreated)
  185. {
  186. continue;
  187. }
  188. $dateCreated = new \DateTime($dateCreated);
  189. $now = new \DateTime();
  190. $dDiff = $dateCreated->diff($now);
  191. if ($dDiff->days > $this->configuration()->getBackupStoreDays())
  192. {
  193. $backup->delete();
  194. $fileRepository->remove($id);
  195. $this->output->writeln(sprintf("Backup with date %s has been deleted", $params['serviceid'], $backup->getDate()));
  196. }
  197. }
  198. }
  199. $sizeMax = $this->getWhmcsConfigOption(Cloud\ConfigurableOption::BACKUPS_SIZE, $this->configuration()->getBackupMaxSize());
  200. if($sizeMax && $sizeMax!="-1" && $fileRepository->getSizeInGb() > $sizeMax){
  201. $this->output->writeln(sprintf("The maximum size set for a backup has been exceeded of %s GB", $fileRepository->getSizeInGb()- $sizeMax ));
  202. foreach ($fileRepository->sortByTime()->fetch() as $id => &$backup)
  203. {
  204. if($fileRepository->getSizeInGb() <= $sizeMax){
  205. break;
  206. }
  207. $this->output->writeln(sprintf("Backup with date %s %s has been deleted", $params['serviceid'], $backup->getDate(), $backup->getHour()));
  208. $fileRepository->remove($id);
  209. $backup->delete();
  210. }
  211. }
  212. $limitMax = $this->getWhmcsConfigOption(Cloud\ConfigurableOption::BACKUPS_FILES, $this->configuration()->getBackupMaxFiles());
  213. if($limitMax && $limitMax!="-1" && $fileRepository->count() > $limitMax){
  214. $this->output->writeln(sprintf("The maximum number set for a backup has been exceeded of %s", $fileRepository->count() - $limitMax ));
  215. foreach ($fileRepository->sortByTime()->fetch() as $id => &$backup)
  216. {
  217. if($fileRepository->count() <= $limitMax){
  218. break;
  219. }
  220. $this->output->writeln(sprintf("Backup with date %s %s has been deleted", $backup->getDate(), $backup->getHour()));
  221. $fileRepository->remove($id);
  222. $backup->delete();
  223. }
  224. }
  225. }
  226. }