Last active
March 28, 2018 11:45
-
-
Save whi-tw/af221239ae93e65396fb49f4710e1af2 to your computer and use it in GitHub Desktop.
Memset Automatic Snapshot with retention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logfile=/var/log/memset-snapshot | |
apikey="**APIKEY**" | |
server="testyaa1" | |
memstore="mstestyaa1" | |
snap_type="tar" | |
retention=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#tnw | |
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
cd $mydir | |
if [ -f /opt/scripts/snapshot.conf ]; | |
then | |
. /opt/scripts/snapshot.conf | |
else | |
echo "Config is missing - Please check this." | |
echo "Creating dummy config in snapshot.conf" | |
echo 'logfile=/var/log/memset-snapshot | |
apikey="" | |
server="" | |
memstore="" | |
snap_type="tar" | |
retention='>snapshot.conf | |
exit 1 | |
fi | |
cleanup_only=False | |
if [ "$1" == "cleanup" ] ; then | |
cleanup_only=True | |
fi | |
#initial log entry | |
echo "$(date +"%Y-%m-%d %T") Snapshot Script Started" >> $logfile | |
list_test=$(curl -s --user $apikey:x https://api.memset.com/v1/json/server.snapshot_list/?storage_name=$memstore) | |
if [[ $list_test == *"error_type"* ]] | |
then | |
error=$(echo $list_test | jq -r '.error') | |
error_code=$(echo $list_test | jq -r '.error_code') | |
echo "$(date +"%Y-%m-%d %T") ERROR: ($error_code) $error." >> $logfile | |
echo "$(date +"%Y-%m-%d %T") Snapshot script Finished" >> $logfile | |
exit 1 | |
fi | |
#check if this server is the right one (disabled on this server due to funny hostname) | |
if [[ $(hostname -s) == $server ]] ; then | |
echo "$(date +"%Y-%m-%d %T") Config file matches this server. Continuing." >> $logfile | |
else | |
echo "$(date +"%Y-%m-%d %T") Config mismatch. Terminating early" >> $logfile | |
exit 1 | |
fi | |
if [ "$cleanup_only" != True ]; then | |
#create a new one and get job ID | |
snapresult=$(curl -s --user $apikey:x https://api.memset.com/v1/json/server.snapshot/$server/?storage_name=$memstore\&image_type=$snap_type) | |
snapjobid=$(echo $snapresult | jq -r '.id') | |
#log job id | |
echo "$(date +"%Y-%m-%d %T") Snapshot started with job ID: $snapjobid" >> $logfile | |
#progress loop | |
delay=5 | |
snapfinished=false | |
while [ "$snapfinished" == "false" ] | |
do | |
snapstate=$(curl -s --user $apikey:x https://api.memset.com/v1/json/job.status?id=$snapjobid) | |
snapfinished=$(echo $snapstate | jq -r '.finished') | |
snaperror=$(echo $snapstate | jq -r '.error') | |
if [ "$snaperror" == "true" ] | |
then | |
echo "$(date +"%Y-%m-%d %T") Snapshot job failed, due to an error. Exiting" >> $logfile | |
exit 1 | |
fi | |
if [ "$snapfinished" == "false" ] | |
then | |
sleep $delay | |
else | |
break | |
fi | |
done | |
echo "$(date +"%Y-%m-%d %T") Snapshot Completed" >> $logfile | |
fi | |
snapshots=$(curl -s --user $apikey:x https://api.memset.com/v1/json/server.snapshot_list/?storage_name=$memstore | grep snapshot_path | grep $server | awk '{print $2}' | sed 's/,//' | sed 's/\"//g') | |
no_snaps=$(echo "$snapshots" | wc -l) | |
#find snapshot to delete | |
while [ $no_snaps -gt $retention ] | |
do | |
todel=$(echo "$snapshots" | head -n1) | |
echo "$(date +"%Y-%m-%d %T") $no_snaps snapshots currently exist" >> $logfile | |
echo "$(date +"%Y-%m-%d %T") Old snapshot to be deleted: $todel" >> $logfile | |
#delete it | |
delresult=$(curl -s --user $apikey:x https://api.memset.com/v1/json/server.snapshot_delete/?storage_name=$memstore\&snapshot_path="$todel") | |
deljobid=$(echo $delresult | jq -r '.id') | |
echo "$(date +"%Y-%m-%d %T") Old snapshot queued for deletion with job ID: $deljobid" >> $logfile | |
delfinished=false | |
while [ "$delfinished" == "false" ] | |
do | |
delstate=$(curl -s --user $apikey:x https://api.memset.com/v1/json/job.status?id=$deljobid) | |
delfinished=$(echo $delstate | jq -r '.finished') | |
delerror=$(echo $delstate | jq -r '.error') | |
if [ "$snaperror" == "true" ] | |
then | |
echo "$(date +"%Y-%m-%d %T") Delete job failed, due to an error. Exiting" >> $logfile | |
exit 1 | |
fi | |
if [ "$delfinished" == "false" ] | |
then | |
sleep $delay | |
else | |
break | |
fi | |
done | |
snapshots=$(curl -s --user $apikey:x https://api.memset.com/v1/json/server.snapshot_list/?storage_name=$memstore | grep snapshot_path | grep $server | awk '{print $2}' | sed 's/,//' | sed 's/\"//g') | |
no_snaps=$(echo "$snapshots" | wc -l) | |
done | |
echo "$(date +"%Y-%m-%d %T") $no_snaps snapshots currently exist" >> $logfile | |
echo "$(date +"%Y-%m-%d %T") Snapshot script Finished" >> $logfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment