Add OpenStack Backup Script
This commit is contained in:
parent
7b16d09719
commit
0fae301077
1 changed files with 121 additions and 0 deletions
121
Backup_OpenStack_Vms_And_Volumes.sh
Normal file
121
Backup_OpenStack_Vms_And_Volumes.sh
Normal file
|
@ -0,0 +1,121 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
export OS_AUTH_TYPE=xxxx
|
||||
export OS_AUTH_URL=xxxx
|
||||
export OS_IDENTITY_API_VERSION=xxx
|
||||
export OS_REGION_NAME="xxxx"
|
||||
export OS_INTERFACE=public
|
||||
export OS_APPLICATION_CREDENTIAL_ID=xxxx
|
||||
export OS_APPLICATION_CREDENTIAL_SECRET=xxxx
|
||||
|
||||
TODAY=$(date +"%Y-%m-%d_%HH%M")
|
||||
DAILY_RETENTION=7
|
||||
|
||||
PROJECT_DIR=/opt/backup
|
||||
LOG_FILE=${PROJECT_DIR}/log/backup_${TODAY}.log
|
||||
|
||||
BCK_VOLUMES_LIST=()
|
||||
BCK_SERVERS_LIST=()
|
||||
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
date +"[%Y-%m-%d_%HH%M] - Listing instance to be backed up" >>${LOG_FILE} 2>&1
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
|
||||
if SERVERS=$(/usr/bin/openstack server list -f json| jq -r '.[].Name'); then
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ---> Server(s) Found" >>${LOG_FILE} 2>&1
|
||||
for SERVER in $SERVERS
|
||||
do
|
||||
SNAPSHOT_SERVER=${SERVER}_${TODAY}
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] === === Starting Backup === ===" >>${LOG_FILE} 2>&1
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] ---> Listing volumes attached" >>${LOG_FILE} 2>&1
|
||||
|
||||
if VOLUMES_ATTACHED=$(/usr/bin/openstack server volume list ${SERVER} -f json | jq -r '.[]."Volume ID"'); then
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] ------> Found volumes attached" >>${LOG_FILE} 2>&1
|
||||
|
||||
for VOLUME_ATTACHED_ID in $VOLUMES_ATTACHED
|
||||
do
|
||||
VOLUME_ATTACHED_NAME=$(/usr/bin/openstack volume show ${VOLUME_ATTACHED_ID} -f json | jq -r '.name')
|
||||
BCK_VOLUME_ATTACHED_NAME=Backup_${VOLUME_ATTACHED_NAME}_${TODAY}
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] ------> Starting Backup volume from ${VOLUME_ATTACHED_NAME} to ${BCK_VOLUME_ATTACHED_NAME}" >>${LOG_FILE} 2>&1
|
||||
BCK_VOLUMES_LIST+=(${BCK_VOLUME_ATTACHED_NAME})
|
||||
/usr/bin/openstack volume backup create --name ${BCK_VOLUME_ATTACHED_NAME} ${VOLUME_ATTACHED_NAME} --force >>${LOG_FILE} 2>&1
|
||||
done
|
||||
|
||||
else
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] ------> No volumes attached" >>${LOG_FILE} 2>&1
|
||||
|
||||
fi
|
||||
|
||||
BCK_SERVER=Backup_${SERVER}_${TODAY}
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] ---> Starting Snapshot from ${SERVER} to ${BCK_SERVER}" >>${LOG_FILE} 2>&1
|
||||
BCK_SERVERS_LIST+=(${BCK_SERVER})
|
||||
/usr/bin/openstack server backup create --rotate ${DAILY_RETENTION} --name ${BCK_SERVER} ${SERVER} >>${LOG_FILE} 2>&1
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ------> [Srv : ${SERVER}] === === End of Backup === ===" >>${LOG_FILE} 2>&1
|
||||
done
|
||||
else
|
||||
echo "No Server found" >>${LOG_FILE} 2>&1
|
||||
fi
|
||||
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
date +"[%Y-%m-%d_%HH%M] - Confirm that's all backup is OK" >>${LOG_FILE} 2>&1
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] => Checking Backup Volumes" >>${LOG_FILE} 2>&1
|
||||
|
||||
for CHECK_BCK_VOLUME in ${BCK_VOLUMES_LIST}
|
||||
do
|
||||
##TODO faire une variable qui recup tous les instances sauvegardées précédements et checker
|
||||
done
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] => Checking Backup Instance" >>${LOG_FILE} 2>&1
|
||||
for CHECK_BCK_SERVER in ${BCK_SERVERS_LIST}
|
||||
do
|
||||
##TODO faire une variable qui recup tous les instances sauvegardées précédements et checker
|
||||
done
|
||||
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
date +"[%Y-%m-%d_%HH%M] - Manage Retention - keep last ${DAILY_RETENTION} Backup Volumes" >>${LOG_FILE} 2>&1
|
||||
echo "======================================================" >>${LOG_FILE} 2>&1
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] => Manage Retention for Backup Volumes" >>${LOG_FILE} 2>&1
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ---> List all Backup Volumes" >>${LOG_FILE} 2>&1
|
||||
/usr/bin/openstack volume backup list -f json | jq '.[].Name' >>${LOG_FILE} 2>&1
|
||||
|
||||
if OLD_VOLUMES=$(/usr/bin/openstack volume backup list -f json | jq -r '.[].Name' | sed "1,${DAILY_RETENTION}d"); then
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ---> Backup Volume(s) to be removed Found" >>${LOG_FILE} 2>&1
|
||||
for OLD_VOLUME in $OLD_VOLUMES
|
||||
do
|
||||
date +"[%Y-%m-%d_%HH%M] ------> Remove Backup Volume ${OLD_VOLUME}" >>${LOG_FILE} 2>&1
|
||||
/usr/local/bin/openstack volume backup delete ${OLD_VOLUME} >>${LOG_FILE} 2>&1
|
||||
|
||||
done
|
||||
else
|
||||
date +"[%Y-%m-%d_%HH%M] No Old Backup Volume found" >>${LOG_FILE} 2>&1
|
||||
fi
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] => Manage Retention for Backup Instances" >>${LOG_FILE} 2>&1
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ---> List all Backup Instances" >>${LOG_FILE} 2>&1
|
||||
/usr/bin/openstack image list -f json| jq -r '.[].Name|select(. | startswith("Backup_"))' >>${LOG_FILE} 2>&1
|
||||
|
||||
if OLD_INSTANCES=$(/usr/bin/openstack image list -f json| jq -r '.[].Name|select(. | startswith("Backup_"))' | sed "1,${DAILY_RETENTION}d"); then
|
||||
|
||||
date +"[%Y-%m-%d_%HH%M] ---> Backup Instance(s) to be removed Found" >>${LOG_FILE} 2>&1
|
||||
for OLD_INSTANCE in $INSTANCES
|
||||
do
|
||||
date +"[%Y-%m-%d_%HH%M] ------> Remove Backup Instance ${OLD_INSTANCE}" >>${LOG_FILE} 2>&1
|
||||
/usr/local/bin/openstack image delete ${OLD_INSTANCE} >>${LOG_FILE} 2>&1
|
||||
|
||||
done
|
||||
else
|
||||
date +"[%Y-%m-%d_%HH%M] No Old Backup Instances found" >>${LOG_FILE} 2>&1
|
||||
fi
|
Loading…
Reference in a new issue