-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfluxDB_backup.sh
63 lines (62 loc) · 2.6 KB
/
InfluxDB_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
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
#Author Hubert Witkowski github.com/huczas twitter: @huczas
#This script is for full backup InfluxDB and send tarred archive to NAS disc.
#For recovery database first untar last archive and
#restore all databases found within the backup directory:
#influxd restore -portable path-to-backup
#
#SETUP MAIL only if your system is capable to sending mails.
MAIL='[email protected]'
BDIR=/home/pi/InfluxDB_backup #Temporary backup directory
MDIR=/home/pi/backup_drive #Mounting point directory
BFILE=InfluxDB_backup_$(date +%Y%m%d_%H%M%S) #Name your backup file with date
DAYS=10 #How many days backup files should be keept
echo -e "Starting InfluxDB backup process!"
echo -e "Unmounting NAS drive"
sudo umount ${MDIR} | echo -n "\e[OK]"
echo -e "Mounting NAS drive"
sudo mount ${MDIR} | echo -n "\e[OK]"
# Check if backup directory exists
if [ ! -d "$BDIR" ];
then
echo -e "Backup directory $BDIR doesn't exist, creating it now!"
mkdir $BDIR
echo -n "\e[OK]"
fi
# Begin the backup process
echo -e "Backing up InfluxDB to $BDIR."
echo -e "This will take some time depending on your disc performance. Please wait..."
influxd backup -portable $BDIR > /dev/null
# Wait for backup process to finish and catch result
RESULT=$?
# If command has completed successfully, delete previous backups and exit
if [ $RESULT = 0 ];
then
echo -e "Delete old local backup file"
sudo rm -f ~/InfluxDB_backup_*.tar.gz
sudo find ${MDIR} -maxdepth 1 -type f -ctime ${DAYS} -not -name '._*' -print -delete
echo -n "\e[OK]"
echo -e "Backup is being tarred. Please wait..."
tar zcfP ~/$BFILE.tar.gz $BDIR
echo -e "Moving backup to NAS"
sudo rsync -a ~/$BFILE.tar.gz $MDIR/
echo -n "\e[OK]"
echo -e "Tarred files being deleted..."
rm -rf $BDIR/$(date +%Y%m%d)*
echo -n "\e[OK]"
echo -e "InfluxDB backup process completed! FILE: ${BFILE}.tar.gz"#| mail -s "InfluxDB backup successful" ${MAIL}
echo -e "Unmounting NAS drive"
sudo umount ${MDIR} | echo -n "[OK]"
exit 0
# Else remove attempted backup file
else
echo -e "Backup failed! Previous backup files untouched."
echo -e "Please check there is sufficient space on the HDD."
echo -e "Tarred files being deleted..."
rm -rf $BDIR
echo -n "\e[OK]"
echo -e "InfluxDB backup process failed!"| mail -s "InfluxDB backup failed!" ${MAIL}
echo -e "Unmounting NAS drive"
sudo umount ${MDIR} | echo -e "\e[OK]"
exit 1
fi