-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots_volumes_purge.sh
executable file
·105 lines (87 loc) · 2.8 KB
/
snapshots_volumes_purge.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#################################################################################
# Ident : snapshots_volumes_purge.sh - 1.0
# Auteur : J.Behuet
#
# Description : Automatisation de la suppression des snapshots ayantle tag
# AutoCreate à true
#
# Usage : ./snapshots_volumes_purge.sh
# Remarque(s) : Ce script nécessite l'installation des Amazon EC2 API Tools
# http://aws.amazon.com/developertools/351
#
# Versions :
# V | Date | Auteur | Description des modifications
# -----|----------|------------------|------------------------------------------
# 1.0 |21-06-2013| J.Behuet | Initial
#
#
#################################################################################
export JAVA_HOME=/usr/
export EC2_HOME=/usr/local/ec2/
export EC2_URL=https://ec2.eu-west-1.amazonaws.com
export PATH=$PATH:$EC2_HOME/bin
export AWS_ACCESS_KEY=NEED_YOUR_KEY
export AWS_SECRET_KEY=NEED_YOUR_KEY
SCRIPTNAME=`basename $0`
VERSION="1.0"
DESCRIPTION="Automatisation de la suppression des snapshot ayant le tag AutoCreate à true"
function print_usage() {
echo -e "Usage\t: ./snapshots_volumes_purge.sh"
echo -e "ARGS"
echo -e "\t-h : Print help"
}
function print_help() {
print_version
echo ""
print_usage
echo $DESCRIPTION
}
function print_version() {
echo -e "Ident\t: $SCRIPTNAME version $VERSION"
echo -e "Auteur\t: J.Behuet"
}
while getopts :h OPT
do
case $OPT in
h)
print_help
exit 0
;;
\?)
echo -e "$SCRIPTNAME : Option incorrecte : $OPTARG"
print_usage
exit 0
;;
esac
done
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') -- Snapshots purge [ START ] --"
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') Get snapshots list"
SNAP_LIST=(`ec2-describe-snapshots --filter "tag:AutoCreated=true" | grep SNAPSHOT | awk '{ print $2";"$5";"$9; }'`)
if [ "$SNAP_LIST" = "0" ]; then
echo "[ERROR] $(date +'%d/%m/%Y %H:%M:%S') Get snapshots list"
exit 1
fi
#Modifier cette valeur pour augmenter ou diminuer la rention maximale d'un snapshot
MAX_RETENTION=7
DELETED=0
TOTAL=0
for v in "${SNAP_LIST[@]}"; do
SNAP_INFO=(`echo $v | tr ';' ' '`)
TODAY=$(date -d "$date" "+%s")
SNAP_DATE=$(date -d "${SNAP_INFO[1]}" "+%s")
SEC_IN_DAY=86400
DATE_DIFF=`echo \($TODAY - $SNAP_DATE\) / $SEC_IN_DAY | bc`
# Test si la date est suppérieur à Xjours alors suppresion
if [ $DATE_DIFF -gt $MAX_RETENTION ]; then
ec2-delete-snapshot ${SNAP_INFO[0]}
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') ${SNAP_INFO[2]} (${SNAP_INFO[0]}) [ DELETED ]"
((DELETED++))
else
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') ${SNAP_INFO[2]} (${SNAP_INFO[0]}) [ KEEP ]"
fi
((TOTAL++))
done
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') -- Result : $DELETED/$TOTAL Snapshot(s) deleted --"
echo "[INFO] $(date +'%d/%m/%Y %H:%M:%S') -- Snapshots purge [ END ] --"
exit 0