-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha-backup-cleanup.bash
executable file
·45 lines (45 loc) · 1.06 KB
/
ha-backup-cleanup.bash
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
#! /bin/bash
#
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
BACKUP_HOME="/data/homeassistant/backups"
ECHO="/bin/echo"
RM="/bin/rm"
PING="/usr/bin/ping"
#
NFSHOST="nazgul.net.home"
BCKDIR="/dl/scratch"
#
shopt -s nullglob
logfiles=($BACKUP_HOME/*)
N_FILES=$($ECHO ${#logfiles[@]})
#
MIN_FILES=30
MAX_AGE="+30"
#
if [ $N_FILES -ge $MIN_FILES ]
then
find $BACKUP_HOME -maxdepth 1 -type f -mtime $MAX_AGE | while read -r file; do
$ECHO "Removing backup file $file"
sudo $RM -f $file
done
else
$ECHO ""
$ECHO "Only $N_FILES backup files exsist. None removed."
$ECHO ""
fi
#
$PING -W 2 -c 1 $NFSHOST > /dev/null
if [ "$?" == "0" ]
then
$ECHO ""
$ECHO "Host $NFSHOST is online. Copying HA backup files"
$ECHO ""
sudo mount -t nfs $NFSHOST:$BCKDIR $BCKDIR
sudo rsync -vrt --delete $BACKUP_HOME/ $BCKDIR/backup/HA
sudo find $BCKDIR/backup/HA -type f -exec sudo chmod 640 {} \;
sudo umount $BCKDIR
else
$ECHO ""
$ECHO "Host $NFSHOST if offline. HA backupfiles not saved to external storage"
$ECHO ""
fi