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 ;
@@ -59,20 +58,21 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
5958 Boolean vmExists = command .isVmExists ();
6059 String diskType = command .getDiskType ();
6160 List <String > volumePaths = command .getVolumePaths ();
62- String restoreVolumeUuid = command .getRestoreVolumeUUID ();
61+ List < String > backupFiles = command .getBackupFiles ();
6362
6463 String newVolumeId = null ;
6564 try {
6665 if (Objects .isNull (vmExists )) {
6766 String volumePath = volumePaths .get (0 );
67+ String backupFile = backupFiles .get (0 );
6868 int lastIndex = volumePath .lastIndexOf ("/" );
6969 newVolumeId = volumePath .substring (lastIndex + 1 );
70- restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
70+ restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , backupFile ,
7171 new Pair <>(vmName , command .getVmState ()), mountOptions );
7272 } else if (Boolean .TRUE .equals (vmExists )) {
73- restoreVolumesOfExistingVM (volumePaths , backupPath , backupRepoType , backupRepoAddress , mountOptions );
73+ restoreVolumesOfExistingVM (volumePaths , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
7474 } else {
75- restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
75+ restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
7676 }
7777 } catch (CloudRuntimeException e ) {
7878 String errorMessage = "Failed to restore backup for VM: " + vmName + "." ;
@@ -86,17 +86,18 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
8686 return new BackupAnswer (command , true , newVolumeId );
8787 }
8888
89- private void restoreVolumesOfExistingVM (List <String > volumePaths , String backupPath ,
89+ private void restoreVolumesOfExistingVM (List <String > volumePaths , String backupPath , List < String > backupFiles ,
9090 String backupRepoType , String backupRepoAddress , String mountOptions ) {
9191 String diskType = "root" ;
9292 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
9393 try {
9494 for (int idx = 0 ; idx < volumePaths .size (); idx ++) {
9595 String volumePath = volumePaths .get (idx );
96- Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
96+ String backupFile = backupFiles .get (idx );
97+ String bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
9798 diskType = "datadisk" ;
98- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid . first () )) {
99- 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 ));
100101 }
101102 }
102103 } finally {
@@ -106,17 +107,18 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
106107
107108 }
108109
109- private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath ,
110+ private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath , List < String > backupFiles ,
110111 String backupRepoType , String backupRepoAddress , String mountOptions ) {
111112 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
112113 String diskType = "root" ;
113114 try {
114- for (int i = 0 ; i < volumePaths .size (); i ++) {
115- String volumePath = volumePaths .get (i );
116- Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , 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 );
117119 diskType = "datadisk" ;
118- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid . first () )) {
119- 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 ));
120122 }
121123 }
122124 } finally {
@@ -126,13 +128,13 @@ private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmNam
126128 }
127129
128130 private void restoreVolume (String backupPath , String backupRepoType , String backupRepoAddress , String volumePath ,
129- String diskType , String volumeUUID , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
131+ String diskType , String backupFile , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
130132 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
131- Pair < String , String > bkpPathAndVolUuid ;
133+ String bkpPath ;
132134 try {
133- bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , volumeUUID );
134- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid . first () )) {
135- 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 ));
136138 }
137139 if (VirtualMachine .State .Running .equals (vmNameAndState .second ())) {
138140 if (!attachVolumeToVm (vmNameAndState .first (), volumePath )) {
@@ -188,13 +190,11 @@ private void deleteTemporaryDirectory(String backupDirectory) {
188190 }
189191 }
190192
191- 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 ) {
192194 String bkpPath = String .format (FILE_PATH_PLACEHOLDER , mountDirectory , backupPath );
193- int lastIndex = volumePath .lastIndexOf (File .separator );
194- String volUuid = Objects .isNull (volumeUuid ) ? volumePath .substring (lastIndex + 1 ) : volumeUuid ;
195- 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 );
196196 bkpPath = String .format (FILE_PATH_PLACEHOLDER , bkpPath , backupFileName );
197- return new Pair <>( bkpPath , volUuid ) ;
197+ return bkpPath ;
198198 }
199199
200200 private boolean replaceVolumeWithBackup (String volumePath , String backupPath ) {
0 commit comments