3131import org .apache .cloudstack .backup .RestoreBackupCommand ;
3232import org .apache .commons .lang3 .RandomStringUtils ;
3333
34- import java .io .File ;
3534import java .io .IOException ;
3635import java .nio .file .Files ;
3736import java .nio .file .Paths ;
37+ import java .util .List ;
3838import java .util .Locale ;
39- import java .util .Map ;
4039import java .util .Objects ;
4140
4241@ ResourceWrapper (handles = RestoreBackupCommand .class )
@@ -58,22 +57,22 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
5857 String mountOptions = command .getMountOptions ();
5958 Boolean vmExists = command .isVmExists ();
6059 String diskType = command .getDiskType ();
61- Map <String , String > volumePathsAndUuids = command .getVolumePathsAndUuids ();
62- String restoreVolumeUuid = command .getRestoreVolumeUUID ();
60+ List <String > volumePaths = command .getVolumePaths ();
61+ List < String > backupFiles = command .getBackupFiles ();
6362
6463 String newVolumeId = null ;
6564 try {
6665 if (Objects .isNull (vmExists )) {
67- Map . Entry < String , String > firstEntry = volumePathsAndUuids . entrySet (). iterator (). next ( );
68- String volumePath = firstEntry . getKey ( );
66+ String volumePath = volumePaths . get ( 0 );
67+ String backupFile = backupFiles . get ( 0 );
6968 int lastIndex = volumePath .lastIndexOf ("/" );
7069 newVolumeId = volumePath .substring (lastIndex + 1 );
71- restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
70+ restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , backupFile ,
7271 new Pair <>(vmName , command .getVmState ()), mountOptions );
7372 } else if (Boolean .TRUE .equals (vmExists )) {
74- restoreVolumesOfExistingVM (volumePathsAndUuids , backupPath , backupRepoType , backupRepoAddress , mountOptions );
73+ restoreVolumesOfExistingVM (volumePaths , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
7574 } else {
76- restoreVolumesOfDestroyedVMs (volumePathsAndUuids , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
75+ restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
7776 }
7877 } catch (CloudRuntimeException e ) {
7978 String errorMessage = "Failed to restore backup for VM: " + vmName + "." ;
@@ -87,18 +86,18 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
8786 return new BackupAnswer (command , true , newVolumeId );
8887 }
8988
90- private void restoreVolumesOfExistingVM (Map <String , String > volumePaths , String backupPath ,
89+ private void restoreVolumesOfExistingVM (List <String > volumePaths , String backupPath , List < String > backupFiles ,
9190 String backupRepoType , String backupRepoAddress , String mountOptions ) {
9291 String diskType = "root" ;
9392 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
9493 try {
95- for (Map . Entry < String , String > entry : volumePaths .entrySet () ) {
96- String currentVolumePath = entry . getKey ( );
97- String backedVolumePath = entry . getValue ( );
98- Pair < String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , backedVolumePath , backupPath , diskType , null );
94+ for (int idx = 0 ; idx < volumePaths .size (); idx ++ ) {
95+ String volumePath = volumePaths . get ( idx );
96+ String backupFile = backupFiles . get ( idx );
97+ String bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
9998 diskType = "datadisk" ;
100- if (!replaceVolumeWithBackup (currentVolumePath , bkpPathAndVolUuid . first () )) {
101- throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid . second () ));
99+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
100+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
102101 }
103102 }
104103 } finally {
@@ -108,18 +107,18 @@ private void restoreVolumesOfExistingVM(Map<String,String> volumePaths, String b
108107
109108 }
110109
111- private void restoreVolumesOfDestroyedVMs (Map <String , String > volumePaths , String vmName , String backupPath ,
110+ private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath , List < String > backupFiles ,
112111 String backupRepoType , String backupRepoAddress , String mountOptions ) {
113112 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
114113 String diskType = "root" ;
115114 try {
116- for (Map . Entry < String , String > entry : volumePaths .entrySet () ) {
117- String currentVolumePath = entry . getKey ( );
118- String backedVolumePath = entry . getValue ( );
119- Pair < String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , backedVolumePath , backupPath , diskType , null );
115+ for (int idx = 0 ; idx < volumePaths .size (); idx ++ ) {
116+ String volumePath = volumePaths . get ( idx );
117+ String backupFile = backupFiles . get ( idx );
118+ String bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
120119 diskType = "datadisk" ;
121- if (!replaceVolumeWithBackup (currentVolumePath , bkpPathAndVolUuid . first () )) {
122- throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid . second () ));
120+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
121+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
123122 }
124123 }
125124 } finally {
@@ -129,13 +128,13 @@ private void restoreVolumesOfDestroyedVMs(Map<String, String> volumePaths, Strin
129128 }
130129
131130 private void restoreVolume (String backupPath , String backupRepoType , String backupRepoAddress , String volumePath ,
132- String diskType , String volumeUUID , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
131+ String diskType , String backupFile , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
133132 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
134- Pair < String , String > bkpPathAndVolUuid ;
133+ String bkpPath ;
135134 try {
136- bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , volumeUUID );
137- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid . first () )) {
138- throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid . second () ));
135+ bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
136+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
137+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
139138 }
140139 if (VirtualMachine .State .Running .equals (vmNameAndState .second ())) {
141140 if (!attachVolumeToVm (vmNameAndState .first (), volumePath )) {
@@ -191,13 +190,11 @@ private void deleteTemporaryDirectory(String backupDirectory) {
191190 }
192191 }
193192
194- private Pair < String , String > getBackupPath (String mountDirectory , String volumePath , String backupPath , String diskType , String volumeUuid ) {
193+ private String getBackupPath (String mountDirectory , String backupPath , String backupFile , String diskType ) {
195194 String bkpPath = String .format (FILE_PATH_PLACEHOLDER , mountDirectory , backupPath );
196- int lastIndex = volumePath .lastIndexOf (File .separator );
197- String volUuid = Objects .isNull (volumeUuid ) ? volumePath .substring (lastIndex + 1 ) : volumeUuid ;
198- String backupFileName = String .format ("%s.%s.qcow2" , diskType .toLowerCase (Locale .ROOT ), volUuid );
195+ String backupFileName = String .format ("%s.%s.qcow2" , diskType .toLowerCase (Locale .ROOT ), backupFile );
199196 bkpPath = String .format (FILE_PATH_PLACEHOLDER , bkpPath , backupFileName );
200- return new Pair <>( bkpPath , volUuid ) ;
197+ return bkpPath ;
201198 }
202199
203200 private boolean replaceVolumeWithBackup (String volumePath , String backupPath ) {
0 commit comments