@@ -62,25 +62,16 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
6262 String restoreVolumeUuid = command .getRestoreVolumeUUID ();
6363
6464 String newVolumeId = null ;
65- try {
66- if (Objects .isNull (vmExists )) {
67- String volumePath = volumePaths .get (0 );
68- int lastIndex = volumePath .lastIndexOf ("/" );
69- newVolumeId = volumePath .substring (lastIndex + 1 );
70- restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
71- new Pair <>(vmName , command .getVmState ()), mountOptions );
72- } else if (Boolean .TRUE .equals (vmExists )) {
73- restoreVolumesOfExistingVM (volumePaths , backupPath , backupRepoType , backupRepoAddress , mountOptions );
74- } else {
75- restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
76- }
77- } catch (CloudRuntimeException e ) {
78- String errorMessage = "Failed to restore backup for VM: " + vmName + "." ;
79- if (e .getMessage () != null && !e .getMessage ().isEmpty ()) {
80- errorMessage += " Details: " + e .getMessage ();
81- }
82- logger .error (errorMessage );
83- return new BackupAnswer (command , false , errorMessage );
65+ if (Objects .isNull (vmExists )) {
66+ String volumePath = volumePaths .get (0 );
67+ int lastIndex = volumePath .lastIndexOf ("/" );
68+ newVolumeId = volumePath .substring (lastIndex + 1 );
69+ restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
70+ new Pair <>(vmName , command .getVmState ()), mountOptions );
71+ } else if (Boolean .TRUE .equals (vmExists )) {
72+ restoreVolumesOfExistingVM (volumePaths , backupPath , backupRepoType , backupRepoAddress , mountOptions );
73+ } else {
74+ restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
8475 }
8576
8677 return new BackupAnswer (command , true , newVolumeId );
@@ -95,8 +86,10 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
9586 String volumePath = volumePaths .get (idx );
9687 Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
9788 diskType = "datadisk" ;
98- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
99- throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
89+ try {
90+ replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ());
91+ } catch (IOException e ) {
92+ throw new CloudRuntimeException (String .format ("Unable to revert backup for volume [%s] due to [%s]." , bkpPathAndVolUuid .second (), e .getMessage ()), e );
10093 }
10194 }
10295 } finally {
@@ -115,8 +108,10 @@ private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmNam
115108 String volumePath = volumePaths .get (i );
116109 Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
117110 diskType = "datadisk" ;
118- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
119- throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
111+ try {
112+ replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ());
113+ } catch (IOException e ) {
114+ throw new CloudRuntimeException (String .format ("Unable to revert backup for volume [%s] due to [%s]." , bkpPathAndVolUuid .second (), e .getMessage ()), e );
120115 }
121116 }
122117 } finally {
@@ -131,13 +126,15 @@ private void restoreVolume(String backupPath, String backupRepoType, String back
131126 Pair <String , String > bkpPathAndVolUuid ;
132127 try {
133128 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 () ));
136- }
137- if (VirtualMachine . State . Running . equals (vmNameAndState .second () )) {
138- if (! attachVolumeToVm ( vmNameAndState .first (), volumePath )) {
139- throw new CloudRuntimeException ( String . format ( "Failed to attach volume to VM: %s" , vmNameAndState . first ()));
129+ try {
130+ replaceVolumeWithBackup ( volumePath , bkpPathAndVolUuid .first ( ));
131+ if ( VirtualMachine . State . Running . equals ( vmNameAndState . second ())) {
132+ if (! attachVolumeToVm (vmNameAndState .first (), volumePath )) {
133+ throw new CloudRuntimeException ( String . format ( "Failed to attach volume to VM: %s" , vmNameAndState .first ()));
134+ }
140135 }
136+ } catch (IOException e ) {
137+ throw new CloudRuntimeException (String .format ("Unable to revert backup for volume [%s] due to [%s]." , bkpPathAndVolUuid .second (), e .getMessage ()), e );
141138 }
142139 } catch (Exception e ) {
143140 throw new CloudRuntimeException ("Failed to restore volume" , e );
@@ -197,9 +194,8 @@ private Pair<String, String> getBackupPath(String mountDirectory, String volumeP
197194 return new Pair <>(bkpPath , volUuid );
198195 }
199196
200- private boolean replaceVolumeWithBackup (String volumePath , String backupPath ) {
201- int exitValue = Script .runSimpleBashScriptForExitValue (String .format (RSYNC_COMMAND , backupPath , volumePath ));
202- return exitValue == 0 ;
197+ private void replaceVolumeWithBackup (String volumePath , String backupPath ) throws IOException {
198+ Script .runSimpleBashScript (String .format (RSYNC_COMMAND , backupPath , volumePath ));
203199 }
204200
205201 private boolean attachVolumeToVm (String vmName , String volumePath ) {
0 commit comments