Skip to content

Commit 85765c3

Browse files
rohityadavcloudPearl1594shwstpprsureshanaparti
authored
backup: simple NAS backup plugin for KVM (#9451)
This is a simple NAS backup plugin for KVM which may be later expanded for other hypervisors. This backup plugin aims to use shared NAS storage on KVM hosts such as NFS (or CephFS and others in future), which is used to backup fully cloned VMs for backup & restore operations. This may NOT be as efficient and performant as some of the other B&R providers, but maybe useful for some KVM environments who are okay to only have full-instance backups and limited functionality. Design & Implementation follows the `networker` B&R plugin, which is simply: - Implement B&R plugin interfaces - Use cmd-answer pattern to execute backup and restore operations on KVM host when VM is running (or needs to be restored) - instead of a B&R API client, relies on answers from KVM agent which executes the operations - Backups are full VM domain snapshots, copied to a VM-specific folders on a NAS target (NFS) along with a domain XML - Backup uses libvirt feature: https://libvirt.org/kbase/live_full_disk_backup.html orchestrated via virsh/bash script (nasbackup.sh) as the libvirt-java lacks the bindings - Supported instance volume storage for restore operations: NFS & local storage Refer the doc PR for feature limitations and usage details: apache/cloudstack-documentation#429 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> Co-authored-by: Pearl Dsilva <pearl1594@gmail.com> Co-authored-by: Abhishek Kumar <abhishek.mrt22@gmail.com> Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
1 parent c3f0d14 commit 85765c3

File tree

59 files changed

+2735
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2735
-67
lines changed

api/src/main/java/com/cloud/vm/VirtualMachine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ public boolean isUsedBySystem() {
333333
*/
334334
Date getCreated();
335335

336+
Date getRemoved();
337+
336338
long getServiceOfferingId();
337339

338340
Long getBackupOfferingId();

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ public class ApiConstants {
11451145
public static final String WEBHOOK_NAME = "webhookname";
11461146

11471147
public static final String NFS_MOUNT_OPTIONS = "nfsmountopts";
1148+
public static final String MOUNT_OPTIONS = "mountopts";
11481149

11491150
public static final String SHAREDFSVM_MIN_CPU_COUNT = "sharedfsvmmincpucount";
11501151
public static final String SHAREDFSVM_MIN_RAM_SIZE = "sharedfsvmminramsize";

api/src/main/java/org/apache/cloudstack/api/ResponseGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import org.apache.cloudstack.api.response.BackupRepositoryResponse;
26+
import org.apache.cloudstack.backup.BackupRepository;
2527
import org.apache.cloudstack.storage.object.Bucket;
2628
import org.apache.cloudstack.affinity.AffinityGroup;
2729
import org.apache.cloudstack.affinity.AffinityGroupResponse;
@@ -554,5 +556,7 @@ List<TemplateResponse> createTemplateResponses(ResponseView view, VirtualMachine
554556

555557
BucketResponse createBucketResponse(Bucket bucket);
556558

559+
BackupRepositoryResponse createBackupRepositoryResponse(BackupRepository repository);
560+
557561
SharedFSResponse createSharedFSResponse(ResponseView view, SharedFS sharedFS);
558562
}

api/src/main/java/org/apache/cloudstack/api/command/user/backup/ListBackupScheduleCmd.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import javax.inject.Inject;
2121

22+
import com.amazonaws.util.CollectionUtils;
2223
import org.apache.cloudstack.acl.RoleType;
2324
import org.apache.cloudstack.api.APICommand;
2425
import org.apache.cloudstack.api.ApiConstants;
@@ -27,6 +28,7 @@
2728
import org.apache.cloudstack.api.Parameter;
2829
import org.apache.cloudstack.api.ServerApiException;
2930
import org.apache.cloudstack.api.response.BackupScheduleResponse;
31+
import org.apache.cloudstack.api.response.ListResponse;
3032
import org.apache.cloudstack.api.response.UserVmResponse;
3133
import org.apache.cloudstack.backup.BackupManager;
3234
import org.apache.cloudstack.backup.BackupSchedule;
@@ -39,6 +41,9 @@
3941
import com.cloud.exception.ResourceUnavailableException;
4042
import com.cloud.utils.exception.CloudRuntimeException;
4143

44+
import java.util.ArrayList;
45+
import java.util.List;
46+
4247
@APICommand(name = "listBackupSchedule",
4348
description = "List backup schedule of a VM",
4449
responseObject = BackupScheduleResponse.class, since = "4.14.0",
@@ -74,9 +79,14 @@ public Long getVmId() {
7479
@Override
7580
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
7681
try{
77-
BackupSchedule schedule = backupManager.listBackupSchedule(getVmId());
78-
if (schedule != null) {
79-
BackupScheduleResponse response = _responseGenerator.createBackupScheduleResponse(schedule);
82+
List<BackupSchedule> schedules = backupManager.listBackupSchedule(getVmId());
83+
ListResponse<BackupScheduleResponse> response = new ListResponse<>();
84+
List<BackupScheduleResponse> scheduleResponses = new ArrayList<>();
85+
if (CollectionUtils.isNullOrEmpty(schedules)) {
86+
for (BackupSchedule schedule : schedules) {
87+
scheduleResponses.add(_responseGenerator.createBackupScheduleResponse(schedule));
88+
}
89+
response.setResponses(scheduleResponses, schedules.size());
8090
response.setResponseName(getCommandName());
8191
setResponseObject(response);
8292
} else {
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.api.command.user.backup.repository;
19+
20+
import org.apache.cloudstack.acl.RoleType;
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.BaseCmd;
25+
import org.apache.cloudstack.api.Parameter;
26+
import org.apache.cloudstack.api.ServerApiException;
27+
import org.apache.cloudstack.api.response.BackupRepositoryResponse;
28+
import org.apache.cloudstack.api.response.ZoneResponse;
29+
import org.apache.cloudstack.backup.BackupRepository;
30+
import org.apache.cloudstack.backup.BackupRepositoryService;
31+
import org.apache.cloudstack.context.CallContext;
32+
33+
import javax.inject.Inject;
34+
35+
@APICommand(name = "addBackupRepository",
36+
description = "Adds a backup repository to store NAS backups",
37+
responseObject = BackupRepositoryResponse.class, since = "4.20.0",
38+
authorized = {RoleType.Admin})
39+
public class AddBackupRepositoryCmd extends BaseCmd {
40+
41+
@Inject
42+
private BackupRepositoryService backupRepositoryService;
43+
44+
/////////////////////////////////////////////////////
45+
//////////////// API parameters /////////////////////
46+
/////////////////////////////////////////////////////
47+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the backup repository")
48+
private String name;
49+
50+
@Parameter(name = ApiConstants.ADDRESS, type = CommandType.STRING, required = true, description = "address of the backup repository")
51+
private String address;
52+
53+
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true, description = "type of the backup repository storage. Supported values: nfs, cephfs, cifs")
54+
private String type;
55+
56+
@Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING, description = "backup repository provider")
57+
private String provider;
58+
59+
@Parameter(name = ApiConstants.MOUNT_OPTIONS, type = CommandType.STRING, description = "shared storage mount options")
60+
private String mountOptions;
61+
62+
@Parameter(name = ApiConstants.ZONE_ID,
63+
type = CommandType.UUID,
64+
entityType = ZoneResponse.class,
65+
required = true,
66+
description = "ID of the zone where the backup repository is to be added")
67+
private Long zoneId;
68+
69+
@Parameter(name = ApiConstants.CAPACITY_BYTES, type = CommandType.LONG, description = "capacity of this backup repository")
70+
private Long capacityBytes;
71+
72+
73+
/////////////////////////////////////////////////////
74+
/////////////////// Accessors ///////////////////////
75+
/////////////////////////////////////////////////////
76+
77+
public BackupRepositoryService getBackupRepositoryService() {
78+
return backupRepositoryService;
79+
}
80+
81+
public String getName() {
82+
return name;
83+
}
84+
85+
public String getType() {
86+
if ("cephfs".equalsIgnoreCase(type)) {
87+
return "ceph";
88+
}
89+
return type.toLowerCase();
90+
}
91+
92+
public String getAddress() {
93+
return address;
94+
}
95+
96+
public String getProvider() {
97+
return provider;
98+
}
99+
100+
public String getMountOptions() {
101+
return mountOptions == null ? "" : mountOptions;
102+
}
103+
104+
public Long getZoneId() {
105+
return zoneId;
106+
}
107+
108+
public Long getCapacityBytes() {
109+
return capacityBytes;
110+
}
111+
112+
/////////////////////////////////////////////////////
113+
/////////////////// Accessors ///////////////////////
114+
/////////////////////////////////////////////////////
115+
116+
@Override
117+
public void execute() {
118+
try {
119+
BackupRepository result = backupRepositoryService.addBackupRepository(this);
120+
if (result != null) {
121+
BackupRepositoryResponse response = _responseGenerator.createBackupRepositoryResponse(result);
122+
response.setResponseName(getCommandName());
123+
this.setResponseObject(response);
124+
} else {
125+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add backup repository");
126+
}
127+
} catch (Exception ex4) {
128+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex4.getMessage());
129+
}
130+
131+
}
132+
133+
@Override
134+
public long getEntityOwnerId() {
135+
return CallContext.current().getCallingAccount().getId();
136+
}
137+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.api.command.user.backup.repository;
19+
20+
import org.apache.cloudstack.acl.RoleType;
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.BaseCmd;
25+
import org.apache.cloudstack.api.Parameter;
26+
import org.apache.cloudstack.api.ServerApiException;
27+
import org.apache.cloudstack.api.response.BackupRepositoryResponse;
28+
import org.apache.cloudstack.api.response.SuccessResponse;
29+
import org.apache.cloudstack.backup.BackupRepositoryService;
30+
31+
import javax.inject.Inject;
32+
33+
@APICommand(name = "deleteBackupRepository",
34+
description = "delete a backup repository",
35+
responseObject = SuccessResponse.class, since = "4.20.0",
36+
authorized = {RoleType.Admin})
37+
public class DeleteBackupRepositoryCmd extends BaseCmd {
38+
39+
@Inject
40+
BackupRepositoryService backupRepositoryService;
41+
42+
/////////////////////////////////////////////////////
43+
//////////////// API parameters /////////////////////
44+
/////////////////////////////////////////////////////
45+
@Parameter(name = ApiConstants.ID,
46+
type = CommandType.UUID,
47+
entityType = BackupRepositoryResponse.class,
48+
required = true,
49+
description = "ID of the backup repository to be deleted")
50+
private Long id;
51+
52+
53+
/////////////////////////////////////////////////////
54+
//////////////// Accessors //////////////////////////
55+
/////////////////////////////////////////////////////
56+
57+
public Long getId() {
58+
return id;
59+
}
60+
61+
@Override
62+
public void execute() {
63+
boolean result = backupRepositoryService.deleteBackupRepository(this);
64+
if (result) {
65+
SuccessResponse response = new SuccessResponse(getCommandName());
66+
this.setResponseObject(response);
67+
} else {
68+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete backup repository");
69+
}
70+
}
71+
72+
@Override
73+
public long getEntityOwnerId() {
74+
return 0;
75+
}
76+
}

0 commit comments

Comments
 (0)