Skip to content

Instantly share code, notes, and snippets.

@asvignesh
Created March 8, 2020 14:51

Revisions

  1. asvignesh created this gist Mar 8, 2020.
    17 changes: 17 additions & 0 deletions IAM Role.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Sid": "1",
    "Effect": "Allow",
    "Action": [
    "ec2:CreateSnapshot",
    "ec2:CreateTags",
    "ec2:DescribeInstanceAttribute"
    ],
    "Resource": [
    "*"
    ]
    }
    ]
    }
    96 changes: 96 additions & 0 deletions ec2-consistentbackup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #!/bin/bash

    # ec2-consistentbackup --description='My First backup' --tags="user=asvignesh;"
    # This script freezes all EXT4 and XFS Filesystem running on Linux EC2 Instances

    description=""
    tags=""

    for i in "$@"
    do
    case $i in
    --description=*) description="${i#*=}" ;;
    --tags=*) tags="${i#*=}" ;;
    *) ;;
    esac
    done

    if [[ -z $tags ]]; then
    tags_spec=""
    else
    # Input format is name=value;name2=value2
    # Output format is {Key=name,Value=value},{Key=name2,Value=value2}
    new_tags=$( echo $tags | perl -p -e 's/(.*?)=(.*?)(;|$)/{Key=\1,Value=\2},/g; s/,$//' )
    tags_spec="ResourceType=snapshot,Tags=[$new_tags]"
    fi


    # Unfreeze file systems
    function unfreeze() {
    for target in $(findmnt -nlo TARGET -t ext4,xfs); {
    if [[ "$target" != "/" ]]; then
    fsfreeze --unfreeze "$target";
    fi
    }
    }

    function warn_and_unfreeze() {
    # If there was any problem, make sure to unfreeze the filesystems before quitting!
    errorCode=$?
    if [ $errorCode -ne 0 ]; then
    unfreeze
    echo -e "ec2-consistent-snapshot exited with an error: $snapshot_output\n$vol_output"
    exit $errorCode
    fi
    }

    # Make sure to unfreeze if we are done, but but also if there was an error or if we are interrupted
    trap warn_and_unfreeze SIGINT ERR

    # Sync data to disk
    sync

    {
    # Find the volumes for the instance
    instance=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    # Get the Availability Zone, like "us-east-1b"
    az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
    # Trim off the last character of the AZ, so "us-east-1b" becomes "us-east-1"
    region=${az::-1}

    volumes=$(
    aws ec2 describe-instance-attribute --instance-id $instance \
    --attribute blockDeviceMapping \
    --output text \
    --query BlockDeviceMappings[*].Ebs.VolumeId \
    --region $region
    )


    # Find the mounted EXT4 and XFS partitions and freeze them
    # To avoid hanging the machine in unrecoverable way, don't freeze "/".
    for target in $(findmnt -nlo TARGET -t ext4,xfs); {
    if [[ "$target" != "/" ]]; then
    fsfreeze --freeze "$target";
    fi
    }

    snapshot_output=""

    # Create snapshots for the current instance
    # Be sure you have an Role attached to the instance
    # and that the role has permission to make snapshots.
    for volume in $(echo $volumes | tr " " "\n"); {
    vol_output=$(
    aws ec2 create-snapshot --volume-id $volume \
    --description "$description" \
    --region $region \
    --tag-specifications "$tags_spec" 2>&1
    )
    snapshot_output="$snapshot_output\n$vol_output";
    }

    unfreeze
    }

    exit 0;