@@ -16,7 +16,7 @@ if [ $(docker ps | grep -c influxdb) -gt 0 ]; then
16
16
fi
17
17
18
18
# setup variables
19
- logfile=./backups/log .txt
19
+ logfile=./backups/log_local .txt
20
20
backupfile=" backup-$( date +" %Y-%m-%d_%H%M" ) .tar.gz"
21
21
22
22
# compress the backups folders to archive
@@ -29,9 +29,10 @@ sudo tar -czf \
29
29
30
30
rm list.txt
31
31
32
+ # set permission for backup files
32
33
sudo chown pi:pi ./backups/backup*
33
34
34
- # create log file and add the backup file to it
35
+ # create local logfile and append the latest backup file to it
35
36
echo " backup saved to ./backups/$backupfile "
36
37
sudo touch $logfile
37
38
sudo chown pi:pi $logfile
@@ -40,19 +41,60 @@ echo $backupfile >>$logfile
40
41
# show size of archive file
41
42
du -h ./backups/$backupfile
42
43
43
- # upload to cloud
44
+ # remove older local backup files
45
+ # to change backups retained, change below +8 to whatever you want (days retained +1)
46
+ ls -t1 ./backups/backup* | tail -n +8 | sudo xargs rm -f
47
+ echo " last seven local backup files are saved in ~/IOTstack/backups"
48
+
49
+
50
+
51
+ # cloud related - dropbox
44
52
if [ -f ./backups/dropbox ]; then
53
+
54
+ # setup variables
55
+ dropboxfolder=/IOTstackBU
56
+ dropboxuploader=~ /Dropbox-Uploader/dropbox_uploader.sh
57
+ dropboxlog=./backups/log_dropbox.txt
58
+
59
+ # upload new backup to dropbox
45
60
echo " uploading to dropbox"
46
- ~ /Dropbox-Uploader/dropbox_uploader.sh upload ./backups/$backupfile /IOTstackBU/
61
+ $dropboxuploader upload ./backups/$backupfile $dropboxfolder
62
+
63
+ # list older files to be deleted from cloud (exludes last 7)
64
+ # to change dropbox backups retained, change below -7 to whatever you want
65
+ echo " checking for old backups on dropbox"
66
+ files=$( $dropboxuploader list $dropboxfolder | awk {' print $3 ' } | tail -n +2 | head -n -7)
67
+
68
+ # write files to be deleted to dropbox logfile
69
+ sudo touch $dropboxlog
70
+ sudo chown pi:pi $dropboxlog
71
+ echo $files | tr " " " \n" > $dropboxlog
72
+
73
+ # delete files from dropbox as per logfile
74
+ echo " deleting old backups from dropbox if they exist - last 7 files are kept"
75
+
76
+ # check older files exist on dropbox, if yes then delete them
77
+ if [ $( echo " $files " | grep -c " backup" ) -ne 0 ] ; then
78
+ input=$dropboxlog
79
+ while IFS= read -r file
80
+ do
81
+ $dropboxuploader delete $dropboxfolder /$file
82
+ done < " $input "
83
+ fi
84
+
85
+ echo " backups deleted from dropbox" >> $dropboxlog
86
+
47
87
fi
48
88
89
+
90
+ # cloud related - google drive
49
91
if [ -f ./backups/rclone ]; then
50
- echo " uploading to Google Drive"
51
- rclone -P copy ./backups/$backupfile gdrive:/IOTstackBU/
92
+ echo " synching to Google Drive"
93
+ echo " latest 7 backup files are kept"
94
+ # sync local backups to gdrive (older gdrive copies will be deleted)
95
+ rclone sync -P ./backups --include " /backup*" gdrive:/IOTstackBU/
96
+ echo " synch with Google Drive complete"
52
97
fi
53
98
54
- # remove older backup files
55
- ls -t1 ./backups/backup* | tail -n +6 | xargs rm -f
56
- echo " last five backup files are saved in ~/IOTstack/backups"
57
99
58
100
popd
0 commit comments