AWS LightSail Delete Snapshots

Here’s my script to delete AWS LightSail snapshots. It can delete daily or weekly backups and scheduled via crontab. If you want longer or shorter retention, adjust the expired value. It’s in seconds. 604800 is 1 week. 2592000 is 1 month.

#!/bin/bash
 
current=$(date +%s)
 
if [ $# -eq 0 ]; then
  exit 1
fi
 
if [[ $1 = 'daily' ]]; then
  prefix='daily'
  expired=$(($current-604800))
elif [[ $1 = 'weekly' ]]; then
  prefix='weekly'
  expired=$(($current-2592000))
else
  exit 1
fi
 
snaps='/root/snapshots/snapshots.json'
names='/root/snapshots/names.txt'
parse='/root/snapshots/parse.txt'
logfile='/root/snapshots/snapshots.log'
 
/usr/local/bin/aws lightsail get-instance-snapshots > $snaps
cat $snaps | jq -r '.instanceSnapshots[] | .name' > $names
cat $names | grep $prefix > $parse
 
while read -r line; do
 
  snapshot=$(echo $line | cut -d_ -f3)
  snapshotname=$line
 
  if [ `expr $snapshot + 1 2> /dev/null` ] ; then
 
    if [ $snapshot -le $expired ]; then
      echo 'Deleted: '$snapshotname >> $logfile
      /usr/local/bin/aws lightsail delete-instance-snapshot \
      --instance-snapshot-name $snapshotname
    else
      echo 'Nothing: '$snapshotname >> $logfile
    fi
 
  else
    echo $snapshot is not numeric > /dev/null
  fi
 
done < $parse
 
echo 'Current time: '$current >> $logfile
echo 'Expired time: '$expired >> $logfile
echo '-----------------------------------' >> $logfile

Schedule deletes via crontab.

# run daily at 5am
0 5 * * * /bin/bash /root/snapshots/delete-snapshot.sh daily 2>&1
# run weekly every sunday at 6am
0 6 * * 0 /bin/bash /root/snapshots/delete-snapshot.sh weekly 2>&1

AWS LightSail Create Snapshots

Here’s my script to create snapshots of a LightSail instance. Create daily or weekly snapshots.

#!/bin/bash
if [ $1 != '' ]; then 
  prefix=$1
  timestamp=$(date +%s)
  snapshotname=$prefix'_web-server_'$timestamp
  /usr/local/bin/aws lightsail create-instance-snapshot \
  --instance-snapshot-name $snapshotname \
  --instance-name web-server
else
  echo 'Need one argument. e.g. daily or weekly'
fi

Crontab. Job can be scheduled daily or weekly.

# run daily at 4am
0 4 * * * /bin/bash /root/snapshots/create-snapshot.sh daily 2>&1
# run weekly every sunday at 5am
0 5 * * 0 /bin/bash /root/snapshots/create-snapshot.sh weekly 2>&1

Terraform Launch EC2 Instance

Here’s the template for launching an EC2 instance via Terraform.

provider "aws" {
  region                  = "us-east-1"
  shared_credentials_file = "/home/username/.aws/credentials"
  profile                 = "default"
}
resource "aws_security_group" "hostname-sg" {
	name = "allow ssh"
	vpc_id = ""
	ingress {
		cidr_blocks = [ "10.0.0.0/8" ]
		from_port = 22
		to_port = 22
		protocol = "tcp"
	}
	egress {
		from_port = 0
		to_port = 0
		protocol = "-1"
		cidr_blocks = [ "0.0.0.0/0" ]
	}
}
resource "aws_instance" "hostname" {
	ami = "ami-xxxxxxxxxx"
	key_name = "your-key"
	instance_type = "t2.large"
	subnet_id = "subnet-xxxxxxxx"
	security_groups	= ["${aws_security_group.hostname-sg.id}"]
	tags {
		Name = "hostname"
		Environment = "development"
	}
}

AWS EFS List on AWS Backup

Here’s how to get a list of EFS on AWS Backup. Displays a list and looks for a specific tag.

#! /bin/bash
 
tmpfil="temp.txt"
output="efs-list.txt"
 
> $output
> $tmpfil
 
declare -a account=("default" "account1" "account2" "account3")
declare -a region=("us-east-1" "us-east-2" "us-west-1" "us-west-2")
 
for i in "${account[@]}"; do
  echo "===================" >> $output
  echo $i >> $output
  echo "===================" >> $output
  for j in "${region[@]}"; do
    echo $j >> $output
    echo "-------------------" >> $output
    aws efs describe-file-systems \
    --query 'FileSystems[*].[FileSystemId]' \
    --profile $i --region $j --output text >> $tmpfil
    while read -r efs; do
      echo $efs >> $output
      sleep 1s
      aws efs describe-tags --file-system-id $efs \
      --query 'Tags[?Key==`aws-backup`].{Name:Key,Value:Value}' \
      --profile $i --region $j --output text >> $output
      echo "..................." >> $output
    done < $tmpfil
    > $tmpfil
  done
done
rm $tmpfil

Output file is efs-list.txt

NFS Fails to Mount on Bootup

Check if rpcbind is running.

service rpcbind status|start|stop|restart

Or you can also clean the NFS cache.

service rpcbind stop
service nfslock stop
rm -rf /var/lib/nfs/statd/sm/*
rm -rf /var/lib/nfs/statd/sm.bak/*
service rpcbind start
service nfslock start