forked from mcci-catena/docker-iot-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafana_backup.sh
executable file
·55 lines (47 loc) · 2.38 KB
/
grafana_backup.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
#!/bin/bash
#The Shell script will be used for taking backup and send it to S3 bucket.
DATE1=$(date +%Y%m%d%H%M)
DATE=$(date +%d-%m-%y_%H-%M)
mkdir -p /var/lib/backup/grafana
grafana_src='/grafana'
if [ ! -d $grafana_src ]; then
echo "DATE:" "$DATE" > /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "DESCRIPTION: ${SOURCE_NAME}_Grafana backup" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "STATUS: Grafana backup failed" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "The source backup directory: grafana_src is not available" >> /tmp/grafana.txt
< /tmp/grafana.txt mail -s "${SOURCE_NAME}: Grafana Data Backup" "${CRON_BACKUP_MAIL}"
exit
else
tar cvzf /var/lib/backup/grafana/"${SOURCE_NAME}"_grafana_data_backup_"${DATE1}".tgz ${grafana_src}/
fi
# Moving the backup to S3 bucket
if s3cmd put -r --no-mime-magic /var/lib/backup/grafana/ s3://"${S3_BUCKET_GRAFANA}"/;
then
echo "DATE:" "$DATE" > /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "DESCRIPTION: ${SOURCE_NAME}_Grafana backup" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "STATUS: Grafana backup succeeded." >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "******* Grafana Data Backup ****************" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
s3cmd ls --no-mime-magic s3://"${S3_BUCKET_GRAFANA}"/ --human-readable | grep -i "${SOURCE_NAME}"_grafana_data | cut -d' ' -f3- | tac | head -10 | sed "s,s3:\/\/${S3_BUCKET_GRAFANA}\/,,g" &>> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "************** END **************************" >> /tmp/grafana.txt
else
echo "DATE:" "$DATE" > /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "DESCRIPTION: ${SOURCE_NAME}_Grafana backup" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "STATUS: Grafana backup failed" >> /tmp/grafana.txt
echo "" >> /tmp/grafana.txt
echo "Something went wrong, please check it" >> /tmp/grafana.txt
< /tmp/grafana.txt mail -s "${SOURCE_NAME}: Grafana Data Backup" "${CRON_BACKUP_MAIL}"
fi
< /tmp/grafana.txt mail -s "${SOURCE_NAME}: Grafana Data Backup" "${CRON_BACKUP_MAIL}"
# Remove the old backup data in local directory to avoid excessive storage use
find /var/lib/backup/grafana/ -type f -exec rm {} \;
exit