Skip to content

Commit

Permalink
backup: simple NAS backup plugin for KVM (apache#9451)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Pearl Dsilva <[email protected]>
Co-authored-by: Abhishek Kumar <[email protected]>
Co-authored-by: Suresh Kumar Anaparti <[email protected]>
  • Loading branch information
4 people authored Sep 5, 2024
1 parent c3f0d14 commit 85765c3
Show file tree
Hide file tree
Showing 59 changed files with 2,735 additions and 67 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/vm/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ public boolean isUsedBySystem() {
*/
Date getCreated();

Date getRemoved();

long getServiceOfferingId();

Long getBackupOfferingId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ public class ApiConstants {
public static final String WEBHOOK_NAME = "webhookname";

public static final String NFS_MOUNT_OPTIONS = "nfsmountopts";
public static final String MOUNT_OPTIONS = "mountopts";

public static final String SHAREDFSVM_MIN_CPU_COUNT = "sharedfsvmmincpucount";
public static final String SHAREDFSVM_MIN_RAM_SIZE = "sharedfsvmminramsize";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.util.Set;

import org.apache.cloudstack.api.response.BackupRepositoryResponse;
import org.apache.cloudstack.backup.BackupRepository;
import org.apache.cloudstack.storage.object.Bucket;
import org.apache.cloudstack.affinity.AffinityGroup;
import org.apache.cloudstack.affinity.AffinityGroupResponse;
Expand Down Expand Up @@ -554,5 +556,7 @@ List<TemplateResponse> createTemplateResponses(ResponseView view, VirtualMachine

BucketResponse createBucketResponse(Bucket bucket);

BackupRepositoryResponse createBackupRepositoryResponse(BackupRepository repository);

SharedFSResponse createSharedFSResponse(ResponseView view, SharedFS sharedFS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.inject.Inject;

import com.amazonaws.util.CollectionUtils;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
Expand All @@ -27,6 +28,7 @@
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.BackupScheduleResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.backup.BackupManager;
import org.apache.cloudstack.backup.BackupSchedule;
Expand All @@ -39,6 +41,9 @@
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.utils.exception.CloudRuntimeException;

import java.util.ArrayList;
import java.util.List;

@APICommand(name = "listBackupSchedule",
description = "List backup schedule of a VM",
responseObject = BackupScheduleResponse.class, since = "4.14.0",
Expand Down Expand Up @@ -74,9 +79,14 @@ public Long getVmId() {
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
try{
BackupSchedule schedule = backupManager.listBackupSchedule(getVmId());
if (schedule != null) {
BackupScheduleResponse response = _responseGenerator.createBackupScheduleResponse(schedule);
List<BackupSchedule> schedules = backupManager.listBackupSchedule(getVmId());
ListResponse<BackupScheduleResponse> response = new ListResponse<>();
List<BackupScheduleResponse> scheduleResponses = new ArrayList<>();
if (CollectionUtils.isNullOrEmpty(schedules)) {
for (BackupSchedule schedule : schedules) {
scheduleResponses.add(_responseGenerator.createBackupScheduleResponse(schedule));
}
response.setResponses(scheduleResponses, schedules.size());
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.api.command.user.backup.repository;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.BackupRepositoryResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.backup.BackupRepository;
import org.apache.cloudstack.backup.BackupRepositoryService;
import org.apache.cloudstack.context.CallContext;

import javax.inject.Inject;

@APICommand(name = "addBackupRepository",
description = "Adds a backup repository to store NAS backups",
responseObject = BackupRepositoryResponse.class, since = "4.20.0",
authorized = {RoleType.Admin})
public class AddBackupRepositoryCmd extends BaseCmd {

@Inject
private BackupRepositoryService backupRepositoryService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "name of the backup repository")
private String name;

@Parameter(name = ApiConstants.ADDRESS, type = CommandType.STRING, required = true, description = "address of the backup repository")
private String address;

@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true, description = "type of the backup repository storage. Supported values: nfs, cephfs, cifs")
private String type;

@Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING, description = "backup repository provider")
private String provider;

@Parameter(name = ApiConstants.MOUNT_OPTIONS, type = CommandType.STRING, description = "shared storage mount options")
private String mountOptions;

@Parameter(name = ApiConstants.ZONE_ID,
type = CommandType.UUID,
entityType = ZoneResponse.class,
required = true,
description = "ID of the zone where the backup repository is to be added")
private Long zoneId;

@Parameter(name = ApiConstants.CAPACITY_BYTES, type = CommandType.LONG, description = "capacity of this backup repository")
private Long capacityBytes;


/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public BackupRepositoryService getBackupRepositoryService() {
return backupRepositoryService;
}

public String getName() {
return name;
}

public String getType() {
if ("cephfs".equalsIgnoreCase(type)) {
return "ceph";
}
return type.toLowerCase();
}

public String getAddress() {
return address;
}

public String getProvider() {
return provider;
}

public String getMountOptions() {
return mountOptions == null ? "" : mountOptions;
}

public Long getZoneId() {
return zoneId;
}

public Long getCapacityBytes() {
return capacityBytes;
}

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

@Override
public void execute() {
try {
BackupRepository result = backupRepositoryService.addBackupRepository(this);
if (result != null) {
BackupRepositoryResponse response = _responseGenerator.createBackupRepositoryResponse(result);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add backup repository");
}
} catch (Exception ex4) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex4.getMessage());
}

}

@Override
public long getEntityOwnerId() {
return CallContext.current().getCallingAccount().getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.api.command.user.backup.repository;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.BackupRepositoryResponse;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.backup.BackupRepositoryService;

import javax.inject.Inject;

@APICommand(name = "deleteBackupRepository",
description = "delete a backup repository",
responseObject = SuccessResponse.class, since = "4.20.0",
authorized = {RoleType.Admin})
public class DeleteBackupRepositoryCmd extends BaseCmd {

@Inject
BackupRepositoryService backupRepositoryService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.ID,
type = CommandType.UUID,
entityType = BackupRepositoryResponse.class,
required = true,
description = "ID of the backup repository to be deleted")
private Long id;


/////////////////////////////////////////////////////
//////////////// Accessors //////////////////////////
/////////////////////////////////////////////////////

public Long getId() {
return id;
}

@Override
public void execute() {
boolean result = backupRepositoryService.deleteBackupRepository(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete backup repository");
}
}

@Override
public long getEntityOwnerId() {
return 0;
}
}
Loading

0 comments on commit 85765c3

Please sign in to comment.