diff --git a/Backup_OpenStack_Vms_And_Volumes.sh b/Backup_OpenStack_Vms_And_Volumes.sh index 30993bf..f1bba2d 100644 --- a/Backup_OpenStack_Vms_And_Volumes.sh +++ b/Backup_OpenStack_Vms_And_Volumes.sh @@ -1,48 +1,57 @@ #!/usr/bin/env bash #source openrc.sh -export OS_AUTH_TYPE=v3applicationcredential -export OS_AUTH_URL=https://auth.cloud.ovh.net/ -export OS_IDENTITY_API_VERSION=3 -export OS_INTERFACE=public -export OS_REGION_NAME="GRA11" -export OS_APPLICATION_CREDENTIAL_ID= -export OS_APPLICATION_CREDENTIAL_SECRET= -export PROJECT_ID= +#export OS_AUTH_TYPE=v3applicationcredential +#export OS_AUTH_URL=https://auth.cloud.ovh.net/ +#export OS_IDENTITY_API_VERSION=3 +#export OS_INTERFACE=public +#export OS_REGION_NAME="GRA11" +#export OS_APPLICATION_CREDENTIAL_ID= +#export OS_APPLICATION_CREDENTIAL_SECRET= +#export PROJECT_ID= #Daily Retention (Nb of months) -export DAILY_RETENTION=20 +#export DAILY_RETENTION=20 #Monthly Retention (Nb of months) # 0 = disable monthly backup -export MONTHLY_RETENTION=12 +#export MONTHLY_RETENTION=12 # DAY OF MOUNTH TO EXECUTE MONTHLY BACKUP # 01 ... 30 31 -export DAY_MONTHLY_BACKUP=01 +#export DAY_MONTHLY_BACKUP=01 #Yearly Retention (Nb of year) # 0 = disable Yearly backup -export YEARLY_RETENTION=0 +#export YEARLY_RETENTION=0 # DAY OF MOUNTH TO EXECUTE YEARLY BACKUP # 01 ... 30 31 -export DAY_OF_YEARLY_BACKUP=01 +#export DAY_OF_YEARLY_BACKUP=01 # MOUNTH TO EXECUTE YEARLY BACKUP # 01:january... 12:December -export MONTH_OF_YEARLY_BACKUP=01 +#export MONTH_OF_YEARLY_BACKUP=01 + +# BACKUP_REPLICATE_REGION +# "" = disable replication +#export BCK_REPLICATE_REGION="" +#export BCK_RCLONE_SRC_ALIAS="" +#export BCK_RCLONE_DST_ALIAS="" +export BCK_REPLICATE_TMP_DIR=/opt/backup/tmp # Number of backup instances to keep export MAX_KEEP_BACKUP_INSTANCES=100 #Set TimeZone -export TZ=Europe/Paris +#export TZ=Europe/Paris +ORIGIN_REGION=${OS_REGION_NAME} TODAY=$(date +"%Y-%m-%d_%HH%M") DAY_OF_MONTH=$(date +"%d") MONTH_OF_YEAR=$(date +"%m") #Set Binary -OPENSTACK=/usr/bin/openstack +OPENSTACK=/usr/local/bin/openstack JQ=jq +RCLONE=rclone GREP=grep SORT=sort @@ -89,6 +98,16 @@ function check_prerequistes(){ exit $retVal fi + _info " ---> Show rclone version" + ${RCLONE} --version + retVal=$? + if [ $retVal -ne 0 ]; then + _err " ---> with rclone" + ${RCLONE} --version 2>&1 + exit $retVal + fi + + _info " ---> Show grep version" ${GREP} --version retVal=$? @@ -157,7 +176,7 @@ function backup(){ BCK_VOLUME_ATTACHED_NAME=${PREFIX}_${VOLUME_ATTACHED_NAME}_${TODAY} _info_action " ------> [Instance : ${SERVER}] ------> Launching the backup of the volume ${VOLUME_ATTACHED_NAME} to ${BCK_VOLUME_ATTACHED_NAME}" - BCK_VOLUMES_LIST+=(${BCK_VOLUME_ATTACHED_NAME}) + BCK_VOLUMES_LIST+=(${PREFIX}_${VOLUME_ATTACHED_NAME}) ${OPENSTACK} volume backup create --name ${BCK_VOLUME_ATTACHED_NAME} ${VOLUME_ATTACHED_NAME} --force done @@ -169,13 +188,66 @@ function backup(){ BCK_SERVER=${PREFIX}_${SERVER}_${TODAY} _info_action " ------> [Instance : ${SERVER}] ---> Launching the Snapshot of the instance ${SERVER} to ${BCK_SERVER}" - BCK_INSTANCES_LIST+=(${BCK_SERVER}) + BCK_INSTANCES_LIST+=(${PREFIX}_${SERVER}) ${OPENSTACK} server backup create --rotate ${MAX_KEEP_BACKUP_INSTANCES} --name ${BCK_SERVER} ${SERVER} _info_blue " ------> [Instance : ${SERVER}] === === End of the backup === ===" done } +function replicate_backup(){ + echo "" + _info "================================================================================" + _info " - Start Backup Replication" + _info "================================================================================" + + echo "" + _info " => Starting Backup Instance Replication" + for BCK_INSTANCE in "${BCK_INSTANCES_LIST[@]}" + do + BCK_INSTANCE_TODAY=${BCK_INSTANCE}_${TODAY} + + export OS_REGION_NAME=${ORIGIN_REGION} + _info_action " Local recording backup Instance ${BCK_INSTANCE_TODAY}" + BCK_INSTANCE_ID=$(${OPENSTACK} image show ${BCK_INSTANCE_TODAY} -c id -f value) + ${OPENSTACK} image save --file ${BCK_REPLICATE_TMP_DIR}/${BCK_INSTANCE_TODAY}.qcow ${BCK_INSTANCE_ID} + + export OS_REGION_NAME=${BCK_REPLICATE_REGION} + _info_action " Replicating backup Instance ${BCK_INSTANCE_TODAY}" + ${OPENSTACK} image create --disk-format qcow2 --container-format bare --file ${BCK_REPLICATE_TMP_DIR}/${BCK_INSTANCE_TODAY}.qcow ${BCK_INSTANCE_TODAY} + + _info_action "Remove local image ${BCK_REPLICATE_TMP_DIR}/${BCK_INSTANCE_TODAY}.qcow" + rm ${BCK_REPLICATE_TMP_DIR}/${BCK_INSTANCE_TODAY}.qcow + + _info_action " Backup Replication of ${BCK_INSTANCE_TODAY} is OK" + done + + export OS_REGION_NAME=${ORIGIN_REGION} + echo "" + _info " => Starting Backup Volume Replication" + + _info_action " Syncing S3 volumebackups" + ${RCLONE} sync --fast-list --transfers=30 --checkers=12 ${BCK_RCLONE_SRC_ALIAS}:volumebackups/ ${BCK_RCLONE_DST_ALIAS}:volumebackups/ + + for BCK_VOLUME in "${BCK_VOLUMES_LIST[@]}" + do + BCK_VOLUME_TODAY=${BCK_VOLUME}_${TODAY} + + export OS_REGION_NAME=${ORIGIN_REGION} + _info_action " Exporting Metadata for backup volume ${BCK_VOLUME_TODAY}" + BCK_VOLUME_ID=$(${OPENSTACK} volume backup list --name ${BCK_VOLUME_TODAY} -c ID -f value) + BCK_VOLUME_JSON_EXPORT=$(${OPENSTACK} volume backup record export -f json ${BCK_VOLUME_ID}) + BCK_VOLUME_EXPORT_SVC=$(echo $BCK_VOLUME_JSON_EXPORT | ${JQ} -r '.backup_service') + BCK_VOLUME_EXPORT_URL=$(echo $BCK_VOLUME_JSON_EXPORT | ${JQ} -r '.backup_url') + + export OS_REGION_NAME=${BCK_REPLICATE_REGION} + _info_action " Importing Metadata for backup volume ${BCK_VOLUME_TODAY}" + ${OPENSTACK} volume backup record import ${BCK_VOLUME_EXPORT_SVC} ${BCK_VOLUME_EXPORT_URL} + done + + export OS_REGION_NAME=${ORIGIN_REGION} +} + function check_backup(){ echo "" _info "================================================================================" @@ -200,7 +272,7 @@ function check_backup(){ exit -1 fi - RESULT=$(${OPENSTACK} volume backup list -f json | jq -c '.[] | select(.Name | contains("'${BCK_VOLUME_TODAY}'")) | .Status == "available"') + RESULT=$(${OPENSTACK} volume backup list -f json | ${JQ} -c '.[] | select(.Name | contains("'${BCK_VOLUME_TODAY}'")) | .Status == "available" ') sleep ${CHECK_FREQUENCY} done _info_action " Backup Volume ${BCK_VOLUME_TODAY} is OK" @@ -412,8 +484,14 @@ function main() { backup check_backup manage_retention + if [ -n "$BCK_REPLICATE_REGION" ]; then + replicate_backup + export OS_REGION_NAME=${BCK_REPLICATE_REGION} + manage_retention + export OS_REGION_NAME=${ORIGIN_REGION} + fi } main "$@" -exit ${EXIT} +exit ${EXIT} \ No newline at end of file