@@ -62,16 +62,25 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
6262 String restoreVolumeUuid = command .getRestoreVolumeUUID ();
6363
6464 String newVolumeId = null ;
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 );
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 );
7584 }
7685
7786 return new BackupAnswer (command , true , newVolumeId );
@@ -86,10 +95,8 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
8695 String volumePath = volumePaths .get (idx );
8796 Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
8897 diskType = "datadisk" ;
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 );
98+ if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
99+ throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
93100 }
94101 }
95102 } finally {
@@ -108,10 +115,8 @@ private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmNam
108115 String volumePath = volumePaths .get (i );
109116 Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
110117 diskType = "datadisk" ;
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 );
118+ if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
119+ throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
115120 }
116121 }
117122 } finally {
@@ -126,15 +131,13 @@ private void restoreVolume(String backupPath, String backupRepoType, String back
126131 Pair <String , String > bkpPathAndVolUuid ;
127132 try {
128133 bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , volumeUUID );
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- }
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 ()));
135140 }
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 );
138141 }
139142 } catch (Exception e ) {
140143 throw new CloudRuntimeException ("Failed to restore volume" , e );
@@ -194,8 +197,9 @@ private Pair<String, String> getBackupPath(String mountDirectory, String volumeP
194197 return new Pair <>(bkpPath , volUuid );
195198 }
196199
197- private void replaceVolumeWithBackup (String volumePath , String backupPath ) throws IOException {
198- Script .runSimpleBashScript (String .format (RSYNC_COMMAND , backupPath , volumePath ));
200+ private boolean replaceVolumeWithBackup (String volumePath , String backupPath ) {
201+ int exitValue = Script .runSimpleBashScriptForExitValue (String .format (RSYNC_COMMAND , backupPath , volumePath ));
202+ return exitValue == 0 ;
199203 }
200204
201205 private boolean attachVolumeToVm (String vmName , String volumePath ) {
0 commit comments