Actualiser Backup_OpenStack_Vms_And_Volumes.sh
Ajout de la gestion de la réplication inter-région
This commit is contained in:
parent
0e1d8873e4
commit
0f87d1c896
1 changed files with 98 additions and 20 deletions
|
@ -1,48 +1,57 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
#source openrc.sh
|
#source openrc.sh
|
||||||
export OS_AUTH_TYPE=v3applicationcredential
|
#export OS_AUTH_TYPE=v3applicationcredential
|
||||||
export OS_AUTH_URL=https://auth.cloud.ovh.net/
|
#export OS_AUTH_URL=https://auth.cloud.ovh.net/
|
||||||
export OS_IDENTITY_API_VERSION=3
|
#export OS_IDENTITY_API_VERSION=3
|
||||||
export OS_INTERFACE=public
|
#export OS_INTERFACE=public
|
||||||
export OS_REGION_NAME="GRA11"
|
#export OS_REGION_NAME="GRA11"
|
||||||
export OS_APPLICATION_CREDENTIAL_ID=
|
#export OS_APPLICATION_CREDENTIAL_ID=
|
||||||
export OS_APPLICATION_CREDENTIAL_SECRET=
|
#export OS_APPLICATION_CREDENTIAL_SECRET=
|
||||||
export PROJECT_ID=
|
#export PROJECT_ID=
|
||||||
|
|
||||||
#Daily Retention (Nb of months)
|
#Daily Retention (Nb of months)
|
||||||
export DAILY_RETENTION=20
|
#export DAILY_RETENTION=20
|
||||||
|
|
||||||
#Monthly Retention (Nb of months)
|
#Monthly Retention (Nb of months)
|
||||||
# 0 = disable monthly backup
|
# 0 = disable monthly backup
|
||||||
export MONTHLY_RETENTION=12
|
#export MONTHLY_RETENTION=12
|
||||||
# DAY OF MOUNTH TO EXECUTE MONTHLY BACKUP
|
# DAY OF MOUNTH TO EXECUTE MONTHLY BACKUP
|
||||||
# 01 ... 30 31
|
# 01 ... 30 31
|
||||||
export DAY_MONTHLY_BACKUP=01
|
#export DAY_MONTHLY_BACKUP=01
|
||||||
|
|
||||||
#Yearly Retention (Nb of year)
|
#Yearly Retention (Nb of year)
|
||||||
# 0 = disable Yearly backup
|
# 0 = disable Yearly backup
|
||||||
export YEARLY_RETENTION=0
|
#export YEARLY_RETENTION=0
|
||||||
# DAY OF MOUNTH TO EXECUTE YEARLY BACKUP
|
# DAY OF MOUNTH TO EXECUTE YEARLY BACKUP
|
||||||
# 01 ... 30 31
|
# 01 ... 30 31
|
||||||
export DAY_OF_YEARLY_BACKUP=01
|
#export DAY_OF_YEARLY_BACKUP=01
|
||||||
# MOUNTH TO EXECUTE YEARLY BACKUP
|
# MOUNTH TO EXECUTE YEARLY BACKUP
|
||||||
# 01:january... 12:December
|
# 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
|
# Number of backup instances to keep
|
||||||
export MAX_KEEP_BACKUP_INSTANCES=100
|
export MAX_KEEP_BACKUP_INSTANCES=100
|
||||||
|
|
||||||
#Set TimeZone
|
#Set TimeZone
|
||||||
export TZ=Europe/Paris
|
#export TZ=Europe/Paris
|
||||||
|
|
||||||
|
ORIGIN_REGION=${OS_REGION_NAME}
|
||||||
TODAY=$(date +"%Y-%m-%d_%HH%M")
|
TODAY=$(date +"%Y-%m-%d_%HH%M")
|
||||||
DAY_OF_MONTH=$(date +"%d")
|
DAY_OF_MONTH=$(date +"%d")
|
||||||
MONTH_OF_YEAR=$(date +"%m")
|
MONTH_OF_YEAR=$(date +"%m")
|
||||||
|
|
||||||
#Set Binary
|
#Set Binary
|
||||||
OPENSTACK=/usr/bin/openstack
|
OPENSTACK=/usr/local/bin/openstack
|
||||||
JQ=jq
|
JQ=jq
|
||||||
|
RCLONE=rclone
|
||||||
GREP=grep
|
GREP=grep
|
||||||
SORT=sort
|
SORT=sort
|
||||||
|
|
||||||
|
@ -89,6 +98,16 @@ function check_prerequistes(){
|
||||||
exit $retVal
|
exit $retVal
|
||||||
fi
|
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"
|
_info " ---> Show grep version"
|
||||||
${GREP} --version
|
${GREP} --version
|
||||||
retVal=$?
|
retVal=$?
|
||||||
|
@ -157,7 +176,7 @@ function backup(){
|
||||||
BCK_VOLUME_ATTACHED_NAME=${PREFIX}_${VOLUME_ATTACHED_NAME}_${TODAY}
|
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}"
|
_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
|
${OPENSTACK} volume backup create --name ${BCK_VOLUME_ATTACHED_NAME} ${VOLUME_ATTACHED_NAME} --force
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -169,13 +188,66 @@ function backup(){
|
||||||
BCK_SERVER=${PREFIX}_${SERVER}_${TODAY}
|
BCK_SERVER=${PREFIX}_${SERVER}_${TODAY}
|
||||||
|
|
||||||
_info_action " ------> [Instance : ${SERVER}] ---> Launching the Snapshot of the instance ${SERVER} to ${BCK_SERVER}"
|
_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}
|
${OPENSTACK} server backup create --rotate ${MAX_KEEP_BACKUP_INSTANCES} --name ${BCK_SERVER} ${SERVER}
|
||||||
|
|
||||||
_info_blue " ------> [Instance : ${SERVER}] === === End of the backup === ==="
|
_info_blue " ------> [Instance : ${SERVER}] === === End of the backup === ==="
|
||||||
done
|
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(){
|
function check_backup(){
|
||||||
echo ""
|
echo ""
|
||||||
_info "================================================================================"
|
_info "================================================================================"
|
||||||
|
@ -200,7 +272,7 @@ function check_backup(){
|
||||||
exit -1
|
exit -1
|
||||||
fi
|
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}
|
sleep ${CHECK_FREQUENCY}
|
||||||
done
|
done
|
||||||
_info_action " Backup Volume ${BCK_VOLUME_TODAY} is OK"
|
_info_action " Backup Volume ${BCK_VOLUME_TODAY} is OK"
|
||||||
|
@ -412,6 +484,12 @@ function main() {
|
||||||
backup
|
backup
|
||||||
check_backup
|
check_backup
|
||||||
manage_retention
|
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 "$@"
|
main "$@"
|
||||||
|
|
Loading…
Reference in a new issue