-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
55 lines (36 loc) · 1.38 KB
/
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
# ***** SQLite backup sript *****
APP=sonarr
# Only start running if db file is found
FILE=/config/$APP.db
sleep 100
until test -f $FILE; do echo "SQLite Backup Script [Info] No database file detected yet, sleeping for 5 minutes.." >> /proc/1/fd/1 && sleep 300; done
# Loop forever and sleep 24 hours after each backup
while true
do
# Set variables like source, destination and name the backup files
DATE=$(date +"%d-%m-%Y")
SOURCE="/config/${APP}.db"
DESTINATION=/backups
NO_BACKUPS=$(ls $DESTINATION | grep .db | wc -l)
BACKUPFILE="${APP}-backup_$DATE.db"
DATETIME=$(date +"%d-%m-%Y %H:%M:%S")
# Variables on where to put the outputfiles
OUTPUTFILE=$DESTINATION/$BACKUPFILE
# Backup actual db file locally in /backups
if test -f "$OUTPUTFILE"; then
echo "$DATETIME SQLite Backup Script [Info] File exists already, skipping backup.." >> /proc/1/fd/1
else
sqlite3 $SOURCE ".backup $DESTINATION/$BACKUPFILE"
echo "$DATETIME SQLite Backup Script [Info] Backup made successfully.." >> /proc/1/fd/1
fi
# Keep last 30 backups
if [ $NO_BACKUPS -gt 30 ]; then
REMOVEFILE=$(ls -t $DESTINATION | tail -1)
rm -f $DESTINATION$REMOVEFILE
echo "$DATETIME SQLite Backup Script [Info] Removed backups from more than 30 days ago.." >> /proc/1/fd/1
else
echo "$DATETIME SQLite Backup Script [Info] We now have $NO_BACKUPS backups so nothing to remove!" >> /proc/1/fd/1
fi
sleep 86400
done