forked from Paraphraser/IOTstackBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiotstack_backup
More file actions
executable file
·143 lines (122 loc) · 4.86 KB
/
iotstack_backup
File metadata and controls
executable file
·143 lines (122 loc) · 4.86 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# Ver 0.1 forked from Paraphraser IOTBACKUP do not use SCP and RSYNC
# 0.2 added combine to one tar file with datetime
# 0.3 + upload to Dropbox
# 0.4 c only upload new files
# 0.5 + delete backups
# 0.6 + encryption
# 0.7 + move archives to old to copy only last to dropbox
# 0.9 + retain last 8 backups only
<<<<<<< HEAD
# 0.10 c temporarily dropbox deactivated, remove .tar file before mv to archive so .tar file does not count in number of retained files
# 0.11 c fetch backups from Blinki using scp
=======
>>>>>>> 983416d502fd21b9364f78012e3abd308414c19f
# should not run as root
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
# support user renaming of script
SCRIPT=$(basename "$0")
# define the purpose of the optional argument, with appropriate default
RUNTAG=${1:-$(date +"%Y-%m-%d_%H%M").$HOSTNAME}
if [ "$#" -gt 1 ]; then
echo "Usage: $SCRIPT {runtag}"
exit -1
fi
# define your backup host - note that if SCPPATH is within a Dropbox
# folder on SCPHOST then you will get Dropbox sync for free.
SCPHOST="myhost.mydomain.com"
SCPUSER="myuser"
SCPPATH="/path/to/backup/directory/on/myhost"
# choose a backup method. SCP saves the results of THIS backup run.
# RSYNC synchronises the RPi backup folder with SCPHOST.
# The essential difference is that RSYNC will result in old backups
# being removed from the target whereas, with SCP, old backups will be
# preserved until you take some action to remove them.
BACKUP_METHOD="DROPB"
# assumptions
IOTSTACK="$HOME/IOTstack"
COMPOSENAME="docker-compose.yml"
COMPOSE="$IOTSTACK/$COMPOSENAME"
BACKUPS="$IOTSTACK/backups"
LOGNAME="backup-log"
LOGFILE="$RUNTAG.$LOGNAME.txt"
GENERALNAME="general-backup"
INFLUXNAME="influx-backup"
# check the key assumptions
if ! [ -d "$IOTSTACK" -a -e "$COMPOSE" ] ; then
echo "Error: One of the following does not exist:"
echo " $IOTSTACK"
echo " $COMPOSE"
echo "This may indicate a problem with your installation."
exit -1
fi
# make sure the backups directory exists, has correct ownership & mode
[ -d "$BACKUPS" ] || mkdir -m 755 -p "$BACKUPS"
[ $(stat -c "%U:%G" "$BACKUPS") = "$USER:$USER" ] || sudo chown $USER:$USER "$BACKUPS"
[ $(stat -c "%a" "$BACKUPS") = "755" ] || sudo chmod 755 "$BACKUPS"
# move into the backups directory
cd "$BACKUPS"
# ensure that the log exists and redirect to it
touch "$LOGFILE"
exec >> "$LOGFILE"
exec 2>> "$LOGFILE"
echo "----- Starting $SCRIPT at $(date) -----"
#clean directories and save old backups
mv -f ~/IOTstack/backups/archive/* /home/pi/IOTstack/backups/archive_old/
rm ~/IOTstack/backups/*backup*
# perform the general backup
~/IOTstackBackup/iotstack_backup_general "$BACKUPS" "$RUNTAG"
# perform the influx backup
~/IOTstackBackup/iotstack_backup_influxdb "$BACKUPS" "$RUNTAG"
<<<<<<< HEAD
echo "------- End Backup scripts"
=======
>>>>>>> 983416d502fd21b9364f78012e3abd308414c19f
# copy the files (keep in mind that log entries written after the
# log is copied to the remote will only be in the local log).
case "$BACKUP_METHOD" in
"SCP")
<<<<<<< HEAD
echo "SCP Backup"
scp "$RUNTAG".* $SCPUSER@$SCPHOST:$SCPPATH
=======
#scp "$RUNTAG".* $SCPUSER@$SCPHOST:$SCPPATH
>>>>>>> 983416d502fd21b9364f78012e3abd308414c19f
;;
"RSYNC")
# note that the slash after "$BACKUPS" is required!
rsync -vrt --delete --exclude=influxdb "$BACKUPS"/ $SCPUSER@$SCPHOST:$SCPPATH
;;
"DROPB")
# all files in one tar archive with date time
echo "Dropbox Backup"
cd ~/IOTstack/backups
tar --exclude='./archive' --exclude='./archive_old' --exclude='./influxdb' -cf ./archive/$RUNTAG.FullBackup.tar .
echo "encrypt archive"
cd ~/IOTstack/backups/archive
gpg -c --passphrase-file /home/pi/user/pass --pinentry-mode loopback $RUNTAG.FullBackup.tar
<<<<<<< HEAD
# ~/Dropbox-Uploader/dropbox_uploader.sh delete *.gpg
# ~/Dropbox-Uploader/dropbox_uploader.sh -s upload *.gpg /
rm $RUNTAG.FullBackup.tar
#to change backups retained, change below +8 to whatever you want (days retained +1)
cd ~/IOTstack/backups/archive_old
ls -t1 ~/IOTstack/backups/archive_old* | tail -n +9 | xargs rm
=======
~/Dropbox-Uploader/dropbox_uploader.sh -s upload * /
mv -f ~/IOTstack/backups/archive/* /home/pi/IOTstack/backups/archive_old/
rm ~/IOTstack/backups/*backup*
#to change backups retained, change below +8 to whatever you want (days retained +1)
ls -t1 ~/IOTstack/backups/archive_old* | tail -n +2 | sudo xargs rm -f
>>>>>>> 983416d502fd21b9364f78012e3abd308414c19f
;;
*)
echo "Warning: $BACKUP_METHOD backup method is not supported"
echo "Warning: The only backup files are the ones in $BACKUPS"
;;
esac
# cleanup
ls -t1 *."$GENERALNAME".* 2>/dev/null | tail -n +8 | xargs rm -f
ls -t1 *."$INFLUXNAME".* 2>/dev/null | tail -n +8 | xargs rm -f
ls -t1 *."$LOGNAME".* 2>/dev/null | tail -n +8 | xargs rm -f
echo "----- Finished $SCRIPT at $(date) -----"