@@ -16,7 +16,7 @@ if [ $(docker ps | grep -c influxdb) -gt 0 ]; then
1616fi
1717
1818# setup variables
19- logfile=./backups/log .txt
19+ logfile=./backups/log_local .txt
2020backupfile=" backup-$( date +" %Y-%m-%d_%H%M" ) .tar.gz"
2121
2222# compress the backups folders to archive
@@ -29,9 +29,10 @@ sudo tar -czf \
2929
3030rm list.txt
3131
32+ # set permission for backup files
3233sudo chown pi:pi ./backups/backup*
3334
34- # create log file and add the backup file to it
35+ # create local logfile and append the latest backup file to it
3536echo " backup saved to ./backups/$backupfile "
3637sudo touch $logfile
3738sudo chown pi:pi $logfile
@@ -40,19 +41,60 @@ echo $backupfile >>$logfile
4041# show size of archive file
4142du -h ./backups/$backupfile
4243
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
4452if [ -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
4560 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+
4787fi
4888
89+
90+ # cloud related - google drive
4991if [ -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"
5297fi
5398
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"
5799
58100popd
0 commit comments