4545import com .cloud .hypervisor .vmware .VmwareDatacenterZoneMap ;
4646import com .cloud .dc .dao .VmwareDatacenterDao ;
4747import com .cloud .hypervisor .vmware .dao .VmwareDatacenterZoneMapDao ;
48+ import com .cloud .storage .dao .VolumeDao ;
49+ import com .cloud .user .User ;
4850import com .cloud .utils .Pair ;
4951import com .cloud .utils .component .AdapterBase ;
5052import com .cloud .utils .exception .CloudRuntimeException ;
@@ -100,6 +102,8 @@ public class VeeamBackupProvider extends AdapterBase implements BackupProvider,
100102 private AgentManager agentMgr ;
101103 @ Inject
102104 private VirtualMachineManager virtualMachineManager ;
105+ @ Inject
106+ private VolumeDao volumeDao ;
103107
104108 protected VeeamClient getClient (final Long zoneId ) {
105109 try {
@@ -340,6 +344,59 @@ public List<Backup.RestorePoint> listRestorePoints(VirtualMachine vm) {
340344 return getClient (vm .getDataCenterId ()).listRestorePoints (backupName , vm .getInstanceName ());
341345 }
342346
347+ @ Override
348+ public void syncBackups (VirtualMachine vm , Backup .Metric metric ) {
349+ List <Backup .RestorePoint > restorePoints = listRestorePoints (vm );
350+ if (CollectionUtils .isEmpty (restorePoints )) {
351+ logger .debug ("Can't find any restore point to VM: {}" , vm );
352+ return ;
353+ }
354+ Transaction .execute (new TransactionCallbackNoReturn () {
355+ @ Override
356+ public void doInTransactionWithoutResult (TransactionStatus status ) {
357+ final List <Backup > backupsInDb = backupDao .listByVmId (null , vm .getId ());
358+ final List <Long > removeList = backupsInDb .stream ().map (InternalIdentity ::getId ).collect (Collectors .toList ());
359+ for (final Backup .RestorePoint restorePoint : restorePoints ) {
360+ if (!(restorePoint .getId () == null || restorePoint .getType () == null || restorePoint .getCreated () == null )) {
361+ Backup existingBackupEntry = checkAndUpdateIfBackupEntryExistsForRestorePoint (backupsInDb , restorePoint , metric );
362+ if (existingBackupEntry != null ) {
363+ removeList .remove (existingBackupEntry .getId ());
364+ continue ;
365+ }
366+
367+ BackupVO backup = new BackupVO ();
368+ backup .setVmId (vm .getId ());
369+ backup .setExternalId (restorePoint .getId ());
370+ backup .setType (restorePoint .getType ());
371+ backup .setDate (restorePoint .getCreated ());
372+ backup .setStatus (Backup .Status .BackedUp );
373+ if (metric != null ) {
374+ backup .setSize (metric .getBackupSize ());
375+ backup .setProtectedSize (metric .getDataSize ());
376+ }
377+ backup .setBackupOfferingId (vm .getBackupOfferingId ());
378+ backup .setAccountId (vm .getAccountId ());
379+ backup .setDomainId (vm .getDomainId ());
380+ backup .setZoneId (vm .getDataCenterId ());
381+ backup .setBackedUpVolumes (BackupManagerImpl .createVolumeInfoFromVolumes (volumeDao .findByInstance (vm .getId ())));
382+
383+ logger .debug ("Creating a new entry in backups: [id: {}, uuid: {}, name: {}, vm_id: {}, external_id: {}, type: {}, date: {}, backup_offering_id: {}, account_id: {}, "
384+ + "domain_id: {}, zone_id: {}]." , backup .getId (), backup .getUuid (), backup .getName (), backup .getVmId (), backup .getExternalId (), backup .getType (), backup .getDate (), backup .getBackupOfferingId (), backup .getAccountId (), backup .getDomainId (), backup .getZoneId ());
385+ backupDao .persist (backup );
386+
387+ ActionEventUtils .onCompletedActionEvent (User .UID_SYSTEM , vm .getAccountId (), EventVO .LEVEL_INFO , EventTypes .EVENT_VM_BACKUP_CREATE ,
388+ String .format ("Created backup %s for VM ID: %s" , backup .getUuid (), vm .getUuid ()),
389+ vm .getId (), ApiCommandResourceType .VirtualMachine .toString (),0 );
390+ }
391+ }
392+ for (final Long backupIdToRemove : removeList ) {
393+ logger .warn (String .format ("Removing backup with ID: [%s]." , backupIdToRemove ));
394+ backupDao .remove (backupIdToRemove );
395+ }
396+ }
397+ });
398+ }
399+
343400 @ Override
344401 public String getConfigComponentName () {
345402 return BackupService .class .getSimpleName ();
@@ -369,4 +426,4 @@ public String getName() {
369426 public String getDescription () {
370427 return "Veeam Backup Plugin" ;
371428 }
372- }
429+ }
0 commit comments