├── README.md └── create_snapshot.sh /README.md: -------------------------------------------------------------------------------- 1 | # openstack-nova-snapshot 2 | Easy OpenStack Nova snapshots from inside instances 3 | 4 | https://raymii.org/s/tutorials/OpenStack_Quick_and_automatic_instance_snapshot_backups.html 5 | -------------------------------------------------------------------------------- /create_snapshot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # License: GNU GPLv3 3 | # Author: Remy van Elst, https://raymii.org 4 | # Script to create snapshot of Nova Instance (to glance) 5 | # Place the computerc file in: /root/.openstack_snapshotrc 6 | 7 | # To restore to a new server: 8 | # nova boot --image "SNAPSHOT_NAME" --poll --flavor "Standard 1" --availability-zone NL1 --nic net-id=00000000-0000-0000-0000-000000000000 --key "SSH_KEY" "VM_NAME" 9 | # To restore to this server (keep public IP) 10 | # nova rebuild --poll "THIS_INSTANCE_UUID" "SNAPSHOT_IMAGE_UUID" 11 | 12 | # OpenStack Command Line tools required: 13 | # apt-get install python-novaclient 14 | # apt-get install python-keystoneclient 15 | # apt-get install python-glanceclient 16 | 17 | # Or for older/other distributions: 18 | # apt-get install python-pip || yum install python-pip 19 | # pip install python-novaclient 20 | # pip install python-keystoneclient 21 | # pip install python-glanceclient 22 | 23 | # To create a snapshot before an apt-get upgrade: 24 | # Place the following in /etc/apt/apt.conf.d/00glancesnapshot 25 | # DPKG::Pre-Invoke {"/bin/bash /usr/local/bin/glance-image-create.sh";}; 26 | 27 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 28 | 29 | # First we check if all the commands we need are installed. 30 | command_exists() { 31 | command -v "$1" >/dev/null 2>&1 32 | if [[ $? -ne 0 ]]; then 33 | echo "I require $1 but it's not installed. Aborting." 34 | exit 1 35 | fi 36 | } 37 | 38 | for COMMAND in "nova" "glance" "dmidecode" "tr"; do 39 | command_exists "${COMMAND}" 40 | done 41 | 42 | # Check if the computerc file exists. If so, assume it has the credentials. 43 | if [[ ! -f "/root/.openstack_snapshotrc" ]]; then 44 | echo "/root/.openstack_snapshotrc file required." 45 | exit 1 46 | else 47 | source "/root/.openstack_snapshotrc" 48 | fi 49 | 50 | # backup_type 51 | BACKUP_TYPE="${1}" 52 | 53 | if [[ -z "${BACKUP_TYPE}" ]]; then 54 | BACKUP_TYPE="manual" 55 | fi 56 | 57 | # rotation of snapshots 58 | ROTATION="${2}" 59 | 60 | if [[ -z "${ROTATION}" ]]; then 61 | ROTATION="7" 62 | fi 63 | 64 | # The nova UUID is accessible via dmidecode, but it's all caps. 65 | THIS_INSTANCE_UUID="$(dmidecode --string system-uuid | tr '[:upper:]' '[:lower:]')" 66 | 67 | # snapshot names will sort by date, hostname and UUID. 68 | SNAPSHOT_NAME="backup-snapshot-$(date "+%Y%m%d-%H:%M")-${BACKUP_TYPE}-$(hostname)-${THIS_INSTANCE_UUID}" 69 | 70 | echo "INFO: Start OpenStack snapshot creation." 71 | 72 | nova backup "${THIS_INSTANCE_UUID}" "${SNAPSHOT_NAME}" "${BACKUP_TYPE}" "${ROTATION}" 73 | if [[ "$?" != 0 ]]; then 74 | echo "ERROR: nova image-create \"${THIS_INSTANCE_UUID}\" \"${SNAPSHOT_NAME}\" \"${BACKUP_TYPE}\" \"${ROTATION}\" failed." 75 | exit 1 76 | else 77 | echo "SUCCESS: Backup image created and pending upload." 78 | fi 79 | --------------------------------------------------------------------------------