Skip to content

Commit

Permalink
improvement(restore): script for restoring from backup file
Browse files Browse the repository at this point in the history
Longhorn 1521

Signed-off-by: Ray Chang <ray.chang@suse.com>
weizhe0422 authored and innobead committed Oct 20, 2022
1 parent 53c9c40 commit 704f651
Showing 2 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/restore_to_file.yaml.template
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ spec:
--output-file '/tmp/restore/<OUTPUT_FILE>'
--output-format <OUTPUT_FORMAT>
# the version of longhorn engine should be v0.4.1 or higher
image: rancher/longhorn-engine:v0.4.1
image: longhornio/longhorn-engine:<LONGHORN_VERSION>
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
103 changes: 103 additions & 0 deletions scripts/restore-backup-to-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash

export RED='\x1b[0;31m'
export NO_COLOR='\x1b[0m'

usage () {
echo "USAGE: $0 --aws-access-key <your_aws_access_key> \ "
echo " --aws-secret-access-key <your_aws_secret_access_key> \ "
echo " --backup-url s3://backupbucket@ap-northeast-1/backupstore?backup=<backup_name>&volume=<volume_name> \ "
echo " --target-directory /tmp/restore/volume.raw \ "
echo " --output-format raw \ "
echo " --backing-file <backing_file_path>"
echo "Restore a Longhorn backup to a raw image or a qcow2 image."
echo ""
echo " -u, --backup-url (Required) Backups S3/NFS URL. e.g., s3://backupbucket@us-east-1/backupstore?backup=backup-bd326da2c4414b02&volume=volumeexamplename"
echo " -o, --output-file (Required) Output file, e.g., /tmp/restore/volume.raw"
echo " -f, --output-format (Required) Output file format, e.g., raw or qcow2"
echo " -v, --version (Required) Longhorn version, e.g., v1.3.2"
echo " --aws-access-key (Optional) AWS credentials access key"
echo " --aws-secret-access-key (Optional) AWS credentials access secret key"
echo " -b, --backing-file (Optional) backing image. e.g., /tmp/backingfile.qcow2"
echo " -h, --help Usage message"
}

error_invalid_params() {
echo -e "${RED}[ERROR]Invalid params. Check the required params.${NO_COLOR}"
usage
exit 1
}

while [[ "$#" -gt 0 ]]; do
key="$1"
case $key in
--aws-access-key)
aws_access_key="$2"
shift # past argument
shift # past value
;;
--aws-secret-access-key)
aws_secret_access_key="$2"
shift # past argument
shift # past value
;;
-u|--backup-url)
backup_url="$2"
shift # past argument
shift # past value
;;
-o|--output-file)
output_file="$2"
shift # past argument
shift # past value
;;
-f|--output-format)
output_format="$2"
shift # past argument
shift # past value
;;
-b|--backing-file)
backing_file="$2"
shift # past argument
shift # past value
;;
-v|--version)
version="$2"
shift # past argument
shift # past value
;;
-h|--help)
usage
exit 0
shift
;;
*)
error_invalid_params
;;
esac
done

# Check the required parameters exits
if [ -z "${backup_url}" ] || [ -z "${output_file}" ] || [ -z "${output_format}" ] || [ -z "${version}" ]; then
error_invalid_params
fi
if [[ "${backup_url}" =~ ^[Ss]3 ]]; then
if [ -z "${aws_access_key}" ] || [ -z "${aws_secret_access_key}" ]; then
error_invalid_params
fi
fi

# Compose the docker arguments
if [[ "${backup_url}" =~ ^[Ss]3 ]]; then
CUSTOMIZED_ARGS="-e AWS_ACCESS_KEY_ID="${aws_access_key}" -e AWS_SECRET_ACCESS_KEY="${aws_secret_access_key}" "
else
CUSTOMIZED_ARGS="--cap-add SYS_ADMIN --security-opt apparmor:unconfined"
fi

# Start restoring a backup to an image file.
docker run ${CUSTOMIZED_ARGS} -v /tmp/restore:/tmp/restore \
longhornio/longhorn-engine:"${version}" longhorn backup \
restore-to-file ""${backup_url}"" \
--output-file "/tmp/restore/${output_file}" \
--output-format "${output_format}" \
--backing-file "${backing_file}"

0 comments on commit 704f651

Please sign in to comment.