AWS SSH

From SWKLS WIKI
Revision as of 18:30, 26 March 2020 by Unknown user (talk) (Created page with "==Working with AWS through Shell Sessions== ===MySQL EVENT Scheduler=== Enable event scheduler (etc/my.cnf) to run this event: <syntaxhighlight lang="bash"> CREATE EVENT clea...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Working with AWS through Shell Sessions

MySQL EVENT Scheduler

Enable event scheduler (etc/my.cnf) to run this event:

CREATE EVENT clear_router ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY DO TRUNCATE ft_form_21;

This should truncate (clear) table ft_form_21 on Sunday.

Start / Stop / Restart Services

Login via ssh

sudo service <service_name> [start | stop | restart | status]

Example services:

  • mysqld
  • httpd

Example usage:

sudo service mysqld restart

LetsEncrypt Scheduled Task

sudo -i
crontab -e

Add the following (press to insert new lines) 0 4 1,15 * * /home/ec2-user/certbot-auto renew

Snapshot Script

Reference:https://github.com/JozefJarosciak/Lightsail-Backup-and-Clean-Up-Shell-Script/blob/master/lightsail-backup-and-cleanup.sh

 NOTE: Requires both JQ and AWS CLI are already installed !!!

Retrieve or create AWS Access Key & Secret Key from the account that has permissions on the LightSail instance.

Configure the instance via CLI

aws configure

Supply Access & Secret Key, region (us-east-1), output (json)

Test

aws lightsail get-instance-snapshots

Create File

Create new file 'lightsail.backup.sh' in ec2-user home directory (or sub-directory) and add code below

#!/bin/bash

######################################
##   CREATE A NEW SNAPSHOT BACKUP   ##
######################################
## This script takes in three arguments the name of your instance, the region, and the number of snapshots to keep.

NameOfYourInstance=$1
NameOfYourBackup=$NameOfYourInstance
Region=$2

aws lightsail create-instance-snapshot --instance-snapshot-name ${NameOfYourBackup}-$(date +%Y-%m-%d_%H.%M) --instance-name $NameOfYourInstance --region $Region

## Delay before initiating clean up of old snapshots
sleep 30

###############################################
##   DELETE OLD SNAPSHOTS + RETAIN SNAPSHOTS ##
###############################################
# Set number of snapshots you'd like to keep in your account
snapshotsToKeep=$3
echo "Number of Instance Snapshots to keep: ${snapshotsToKeep}"

# get the total number of available Lightsail snapshots
numberOfSnapshots=$(aws lightsail get-instance-snapshots | jq '[.[]  | select(.[].fromInstanceName == "'${NameOfYourInstance}'") ]| length')
echo "Number of instance snapshots: ${numberOfSnapshots}"

# get the names of all snapshots sorted from old to new
SnapshotNames=$(aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | map(select(.fromInstanceName == "'${NameOfYourInstance}'")) | .[].name')

# loop through all snapshots
while IFS= read -r line
do
let "i++"

    # delete old snapshots condition
    if (($i <= $numberOfSnapshots-$snapshotsToKeep))
    then
        snapshotToDelete=$(echo "$line" | tr -d '"')

        # delete snapshot command
        aws lightsail delete-instance-snapshot --instance-snapshot-name $snapshotToDelete
    fi

done <<< "$SnapshotNames"

exit 1

Make file executable

sudo chmod 755 lightsail.backup.sh

Add Crontab entry

sudo crontab -e

Add new line containing:

0 0 * * * /home/ec2-user/lightsail.backup.sh <instance_name> <region> <number_of_snapshots>