4141import org .apache .commons .lang3 .StringUtils ;
4242import org .libvirt .LibvirtException ;
4343
44- import java .io .File ;
4544import java .io .IOException ;
4645import java .nio .file .Files ;
4746import java .nio .file .Paths ;
@@ -77,21 +76,23 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
7776 int timeout = command .getWait ();
7877 String cacheMode = command .getCacheMode ();
7978 KVMStoragePoolManager storagePoolMgr = serverResource .getStoragePoolMgr ();
79+ List <String > volumePaths = command .getVolumePaths ();
80+ List <String > backupFiles = command .getBackupFiles ();
8081
8182 String newVolumeId = null ;
8283 try {
8384 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions , mountTimeout );
8485 if (Objects .isNull (vmExists )) {
85- PrimaryDataStoreTO volumePool = restoreVolumePools .get (0 );
86- String volumePath = restoreVolumePaths .get (0 );
86+ String volumePath = volumePaths .get (0 );
87+ String backupFile = backupFiles .get (0 );
8788 int lastIndex = volumePath .lastIndexOf ("/" );
8889 newVolumeId = volumePath .substring (lastIndex + 1 );
89- restoreVolume (storagePoolMgr , backupPath , volumePool , volumePath , diskType , restoreVolumeUuid ,
90- new Pair <>(vmName , command .getVmState ()), mountDirectory , timeout , cacheMode );
90+ restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , backupFile ,
91+ new Pair <>(vmName , command .getVmState ()), mountOptions );
9192 } else if (Boolean .TRUE .equals (vmExists )) {
92- restoreVolumesOfExistingVM (storagePoolMgr , restoreVolumePools , restoreVolumePaths , backedVolumeUUIDs , backupPath , mountDirectory , timeout );
93+ restoreVolumesOfExistingVM (volumePaths , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
9394 } else {
94- restoreVolumesOfDestroyedVMs (storagePoolMgr , restoreVolumePools , restoreVolumePaths , vmName , backupPath , mountDirectory , timeout );
95+ restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupFiles , backupRepoType , backupRepoAddress , mountOptions );
9596 }
9697 } catch (CloudRuntimeException e ) {
9798 String errorMessage = e .getMessage () != null ? e .getMessage () : "" ;
@@ -101,28 +102,17 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
101102 return new BackupAnswer (command , true , newVolumeId );
102103 }
103104
104- private void verifyBackupFile (String backupPath , String volUuid ) {
105- if (!checkBackupPathExists (backupPath )) {
106- throw new CloudRuntimeException (String .format ("Backup file for the volume [%s] does not exist." , volUuid ));
107- }
108- if (!checkBackupFileImage (backupPath )) {
109- throw new CloudRuntimeException (String .format ("Backup qcow2 file for the volume [%s] is corrupt." , volUuid ));
110- }
111- }
112-
113- private void restoreVolumesOfExistingVM (KVMStoragePoolManager storagePoolMgr , List <PrimaryDataStoreTO > restoreVolumePools , List <String > restoreVolumePaths , List <String > backedVolumesUUIDs ,
114- String backupPath , String mountDirectory , int timeout ) {
105+ private void restoreVolumesOfExistingVM (List <String > volumePaths , String backupPath , List <String > backupFiles ,
106+ String backupRepoType , String backupRepoAddress , String mountOptions ) {
115107 String diskType = "root" ;
116108 try {
117- for (int idx = 0 ; idx < restoreVolumePaths .size (); idx ++) {
118- PrimaryDataStoreTO restoreVolumePool = restoreVolumePools .get (idx );
119- String restoreVolumePath = restoreVolumePaths .get (idx );
120- String backupVolumeUuid = backedVolumesUUIDs .get (idx );
121- Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , null , backupPath , diskType , backupVolumeUuid );
109+ for (int idx = 0 ; idx < volumePaths .size (); idx ++) {
110+ String volumePath = volumePaths .get (idx );
111+ String backupFile = backupFiles .get (idx );
112+ String bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
122113 diskType = "datadisk" ;
123- verifyBackupFile (bkpPathAndVolUuid .first (), bkpPathAndVolUuid .second ());
124- if (!replaceVolumeWithBackup (storagePoolMgr , restoreVolumePool , restoreVolumePath , bkpPathAndVolUuid .first (), timeout )) {
125- throw new CloudRuntimeException (String .format ("Unable to restore contents from the backup volume [%s]." , bkpPathAndVolUuid .second ()));
114+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
115+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
126116 }
127117 }
128118 } finally {
@@ -131,17 +121,18 @@ private void restoreVolumesOfExistingVM(KVMStoragePoolManager storagePoolMgr, Li
131121 }
132122 }
133123
134- private void restoreVolumesOfDestroyedVMs (KVMStoragePoolManager storagePoolMgr , List <PrimaryDataStoreTO > volumePools , List <String > volumePaths , String vmName , String backupPath , String mountDirectory , int timeout ) {
124+ private void restoreVolumesOfDestroyedVMs (List <String > volumePaths , String vmName , String backupPath , List <String > backupFiles ,
125+ String backupRepoType , String backupRepoAddress , String mountOptions ) {
126+ String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
135127 String diskType = "root" ;
136128 try {
137- for (int i = 0 ; i < volumePaths .size (); i ++) {
138- PrimaryDataStoreTO volumePool = volumePools .get (i );
139- String volumePath = volumePaths .get (i );
140- Pair < String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
129+ for (int idx = 0 ; idx < volumePaths .size (); idx ++) {
130+ String volumePath = volumePaths .get (idx );
131+ String backupFile = backupFiles .get (idx );
132+ String bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
141133 diskType = "datadisk" ;
142- verifyBackupFile (bkpPathAndVolUuid .first (), bkpPathAndVolUuid .second ());
143- if (!replaceVolumeWithBackup (storagePoolMgr , volumePool , volumePath , bkpPathAndVolUuid .first (), timeout )) {
144- throw new CloudRuntimeException (String .format ("Unable to restore contents from the backup volume [%s]." , bkpPathAndVolUuid .second ()));
134+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
135+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
145136 }
146137 }
147138 } finally {
@@ -150,14 +141,14 @@ private void restoreVolumesOfDestroyedVMs(KVMStoragePoolManager storagePoolMgr,
150141 }
151142 }
152143
153- private void restoreVolume (KVMStoragePoolManager storagePoolMgr , String backupPath , PrimaryDataStoreTO volumePool , String volumePath , String diskType , String volumeUUID ,
154- Pair <String , VirtualMachine .State > vmNameAndState , String mountDirectory , int timeout , String cacheMode ) {
155- Pair <String , String > bkpPathAndVolUuid ;
144+ private void restoreVolume (String backupPath , String backupRepoType , String backupRepoAddress , String volumePath ,
145+ String diskType , String backupFile , Pair <String , VirtualMachine .State > vmNameAndState , String mountOptions ) {
146+ String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
147+ String bkpPath ;
156148 try {
157- bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , volumeUUID );
158- verifyBackupFile (bkpPathAndVolUuid .first (), bkpPathAndVolUuid .second ());
159- if (!replaceVolumeWithBackup (storagePoolMgr , volumePool , volumePath , bkpPathAndVolUuid .first (), timeout , true )) {
160- throw new CloudRuntimeException (String .format ("Unable to restore contents from the backup volume [%s]." , bkpPathAndVolUuid .second ()));
149+ bkpPath = getBackupPath (mountDirectory , backupPath , backupFile , diskType );
150+ if (!replaceVolumeWithBackup (volumePath , bkpPath )) {
151+ throw new CloudRuntimeException (String .format ("Unable to restore backup from volume [%s]." , volumePath ));
161152 }
162153 if (VirtualMachine .State .Running .equals (vmNameAndState .second ())) {
163154 if (!attachVolumeToVm (storagePoolMgr , vmNameAndState .first (), volumePool , volumePath , cacheMode )) {
@@ -220,12 +211,11 @@ private void deleteTemporaryDirectory(String backupDirectory) {
220211 }
221212 }
222213
223- private Pair < String , String > getBackupPath (String mountDirectory , String volumePath , String backupPath , String diskType , String volumeUuid ) {
214+ private String getBackupPath (String mountDirectory , String backupPath , String backupFile , String diskType ) {
224215 String bkpPath = String .format (FILE_PATH_PLACEHOLDER , mountDirectory , backupPath );
225- String volUuid = Objects .isNull (volumeUuid ) ? volumePath .substring (volumePath .lastIndexOf (File .separator ) + 1 ) : volumeUuid ;
226- String backupFileName = String .format ("%s.%s.qcow2" , diskType .toLowerCase (Locale .ROOT ), volUuid );
216+ String backupFileName = String .format ("%s.%s.qcow2" , diskType .toLowerCase (Locale .ROOT ), backupFile );
227217 bkpPath = String .format (FILE_PATH_PLACEHOLDER , bkpPath , backupFileName );
228- return new Pair <>( bkpPath , volUuid ) ;
218+ return bkpPath ;
229219 }
230220
231221 private boolean checkBackupFileImage (String backupPath ) {
0 commit comments