Skip to content

Instantly share code, notes, and snippets.

@misterion
Created November 10, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterion/b8f3731dd635017044c91ae34410164d to your computer and use it in GitHub Desktop.
Save misterion/b8f3731dd635017044c91ae34410164d to your computer and use it in GitHub Desktop.
Selectel private cloud backup script
#!/bin/bash
export OS_AUTH_URL="https://api.selvpc.ru/identity/v3"
export OS_IDENTITY_API_VERSION="3"
export OS_VOLUME_API_VERSION="2"
export OS_PROJECT_DOMAIN_NAME='PUT YOUR DATA HERE'
export OS_PROJECT_ID='PUT YOUR DATA HERE'
export OS_TENANT_ID='PUT YOUR DATA HERE'
export OS_REGION_NAME='ru-1'
export OS_USER_DOMAIN_NAME='PUT YOUR DATA HERE'
export OS_USERNAME='PUT YOUR DATA HERE'
export OS_ESCAPED_USERNAME=$(echo -n 'backups' | python -c "import sys,json; sys.stdout.write(json.dumps(sys.stdin.read()))")
export OS_PASSWORD='PUT YOUR DATA HERE'
#Set the field separator to new line
IFS=$'\n'
function backup {
volumes=$(/usr/local/bin/cinder list --status in-use)
while read -r line; do
if [[ $line =~ ([[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+) ]]; then
snapId=${BASH_REMATCH[1]}
echo "Backup volumt Id $snapId"
/usr/local/bin/cinder snapshot-create --force True "$snapId"
fi
done <<< "$volumes"
}
function cleanOldSnapshots {
echo "Clean uo snapshots"
index=0
maxIndex=20
snapshots=$(/usr/local/bin/cinder snapshot-list --sort created_at)
while read -r line; do
if [[ $line =~ ([[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+-[[:alnum:]]+) ]]; then
snapId=${BASH_REMATCH[1]}
echo "$index: Keep snapshot Id $snapId"
((index++))
if (( index > maxIndex )); then
echo "$index: Should be removed $snapId"
/usr/local/bin/cinder snapshot-delete "$snapId"
fi
fi
done <<< "$snapshots"
}
cleanOldSnapshots
backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment