Skip to content

Commit e094e14

Browse files
committed
Nas backup provider changes
1 parent 35ed72c commit e094e14

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

core/src/main/java/org/apache/cloudstack/backup/RestoreBackupCommand.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class RestoreBackupCommand extends Command {
3030
private String backupPath;
3131
private String backupRepoType;
3232
private String backupRepoAddress;
33-
private List<String> volumePaths;
33+
private List<String> backupVolumesUUIDs;
34+
private List<String> restoreVolumePaths;
3435
private String diskType;
3536
private Boolean vmExists;
3637
private String restoreVolumeUUID;
@@ -72,12 +73,12 @@ public void setBackupRepoAddress(String backupRepoAddress) {
7273
this.backupRepoAddress = backupRepoAddress;
7374
}
7475

75-
public List<String> getVolumePaths() {
76-
return volumePaths;
76+
public List<String> getRestoreVolumePaths() {
77+
return restoreVolumePaths;
7778
}
7879

79-
public void setVolumePaths(List<String> volumePaths) {
80-
this.volumePaths = volumePaths;
80+
public void setRestoreVolumePaths(List<String> restoreVolumePaths) {
81+
this.restoreVolumePaths = restoreVolumePaths;
8182
}
8283

8384
public Boolean isVmExists() {
@@ -127,4 +128,12 @@ public void setVmState(VirtualMachine.State vmState) {
127128
public boolean executeInSequence() {
128129
return true;
129130
}
131+
132+
public List<String> getBackupVolumesUUIDs() {
133+
return backupVolumesUUIDs;
134+
}
135+
136+
public void setBackupVolumesUUIDs(List<String> backupVolumesUUIDs) {
137+
this.backupVolumesUUIDs = backupVolumesUUIDs;
138+
}
130139
}

plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,20 @@ private BackupVO createBackupObject(VirtualMachine vm, String backupPath) {
208208
return backupDao.persist(backup);
209209
}
210210

211+
@Override
212+
public boolean restoreBackupToVM(VirtualMachine vm, Backup backup) {
213+
return restoreVMBackup(vm, backup);
214+
}
215+
211216
@Override
212217
public boolean restoreVMFromBackup(VirtualMachine vm, Backup backup) {
218+
return restoreVMBackup(vm, backup);
219+
}
220+
221+
private boolean restoreVMBackup(VirtualMachine vm, Backup backup) {
213222
List<Backup.VolumeInfo> backedVolumes = backup.getBackedUpVolumes();
214-
List<VolumeVO> volumes = backedVolumes.stream().map(volume -> volumeDao.findByUuid(volume.getUuid())).collect(Collectors.toList());
223+
List<String> backedVolumesUUIDs = backedVolumes.stream().map(volume -> volume.getUuid()).collect(Collectors.toList());
224+
List<VolumeVO> restoreVolumes = volumeDao.findByInstance(vm.getId());
215225

216226
LOG.debug("Restoring vm {} from backup {} on the NAS Backup Provider", vm.getUuid(), backup.getUuid());
217227
BackupRepository backupRepository = getBackupRepository(vm, backup);
@@ -222,7 +232,8 @@ public boolean restoreVMFromBackup(VirtualMachine vm, Backup backup) {
222232
restoreCommand.setBackupRepoType(backupRepository.getType());
223233
restoreCommand.setBackupRepoAddress(backupRepository.getAddress());
224234
restoreCommand.setVmName(vm.getName());
225-
restoreCommand.setVolumePaths(getVolumePaths(volumes));
235+
restoreCommand.setBackupVolumesUUIDs(backedVolumesUUIDs);
236+
restoreCommand.setRestoreVolumePaths(getVolumePaths(restoreVolumes));
226237
restoreCommand.setVmExists(vm.getRemoved() == null);
227238
restoreCommand.setVmState(vm.getState());
228239

@@ -287,7 +298,7 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, String volumeU
287298
restoreCommand.setBackupRepoType(backupRepository.getType());
288299
restoreCommand.setBackupRepoAddress(backupRepository.getAddress());
289300
restoreCommand.setVmName(vmNameAndState.first());
290-
restoreCommand.setVolumePaths(Collections.singletonList(String.format("%s/%s", dataStore.getLocalPath(), volumeUUID)));
301+
restoreCommand.setRestoreVolumePaths(Collections.singletonList(String.format("%s/%s", dataStore.getLocalPath(), volumeUUID)));
291302
restoreCommand.setDiskType(volume.getVolumeType().name().toLowerCase(Locale.ROOT));
292303
restoreCommand.setVmExists(null);
293304
restoreCommand.setVmState(vmNameAndState.second());

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,38 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
5858
String mountOptions = command.getMountOptions();
5959
Boolean vmExists = command.isVmExists();
6060
String diskType = command.getDiskType();
61-
List<String> volumePaths = command.getVolumePaths();
61+
List<String> backedVolumeUUIDs = command.getBackupVolumesUUIDs();
62+
List<String> restoreVolumePaths = command.getRestoreVolumePaths();
6263
String restoreVolumeUuid = command.getRestoreVolumeUUID();
6364

6465
String newVolumeId = null;
6566
if (Objects.isNull(vmExists)) {
66-
String volumePath = volumePaths.get(0);
67+
String volumePath = restoreVolumePaths.get(0);
6768
int lastIndex = volumePath.lastIndexOf("/");
6869
newVolumeId = volumePath.substring(lastIndex + 1);
6970
restoreVolume(backupPath, backupRepoType, backupRepoAddress, volumePath, diskType, restoreVolumeUuid,
7071
new Pair<>(vmName, command.getVmState()));
7172
} else if (Boolean.TRUE.equals(vmExists)) {
72-
restoreVolumesOfExistingVM(volumePaths, backupPath, backupRepoType, backupRepoAddress, mountOptions);
73+
restoreVolumesOfExistingVM(restoreVolumePaths, backedVolumeUUIDs, backupPath, backupRepoType, backupRepoAddress, mountOptions);
7374
} else {
74-
restoreVolumesOfDestroyedVMs(volumePaths, vmName, backupPath, backupRepoType, backupRepoAddress, mountOptions);
75+
restoreVolumesOfDestroyedVMs(restoreVolumePaths, vmName, backupPath, backupRepoType, backupRepoAddress, mountOptions);
7576
}
7677

7778
return new BackupAnswer(command, true, newVolumeId);
7879
}
7980

80-
private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupPath,
81-
String backupRepoType, String backupRepoAddress, String mountOptions) {
81+
private void restoreVolumesOfExistingVM(List<String> restoreVolumePaths, List<String> backedVolumesUUIDs, String backupPath,
82+
String backupRepoType, String backupRepoAddress, String mountOptions) {
8283
String diskType = "root";
8384
String mountDirectory = mountBackupDirectory(backupRepoAddress, backupRepoType);
8485
try {
85-
for (int idx = 0; idx < volumePaths.size(); idx++) {
86-
String volumePath = volumePaths.get(idx);
87-
Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, null);
86+
for (int idx = 0; idx < restoreVolumePaths.size(); idx++) {
87+
String restoreVolumePath = restoreVolumePaths.get(idx);
88+
String backupVolumeUuid = backedVolumesUUIDs.get(idx);
89+
Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, null, backupPath, diskType, backupVolumeUuid);
8890
diskType = "datadisk";
8991
try {
90-
replaceVolumeWithBackup(volumePath, bkpPathAndVolUuid.first());
92+
replaceVolumeWithBackup(restoreVolumePath, bkpPathAndVolUuid.first());
9193
} catch (IOException e) {
9294
throw new CloudRuntimeException(String.format("Unable to revert backup for volume [%s] due to [%s].", bkpPathAndVolUuid.second(), e.getMessage()), e);
9395
}
@@ -96,7 +98,6 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
9698
unmountBackupDirectory(mountDirectory);
9799
deleteTemporaryDirectory(mountDirectory);
98100
}
99-
100101
}
101102

102103
private void restoreVolumesOfDestroyedVMs(List<String> volumePaths, String vmName, String backupPath,
@@ -177,8 +178,7 @@ private void deleteTemporaryDirectory(String backupDirectory) {
177178

178179
private Pair<String, String> getBackupPath(String mountDirectory, String volumePath, String backupPath, String diskType, String volumeUuid) {
179180
String bkpPath = String.format(FILE_PATH_PLACEHOLDER, mountDirectory, backupPath);
180-
int lastIndex = volumePath.lastIndexOf(File.separator);
181-
String volUuid = Objects.isNull(volumeUuid) ? volumePath.substring(lastIndex + 1) : volumeUuid;
181+
String volUuid = Objects.isNull(volumeUuid) ? volumePath.substring(volumePath.lastIndexOf(File.separator) + 1) : volumeUuid;
182182
String backupFileName = String.format("%s.%s.qcow2", diskType.toLowerCase(Locale.ROOT), volUuid);
183183
bkpPath = String.format(FILE_PATH_PLACEHOLDER, bkpPath, backupFileName);
184184
return new Pair<>(bkpPath, volUuid);

0 commit comments

Comments
 (0)