Code: Select all
#!/bin/bash
# Sript to automatic backup some stuff from Domoticz
# Run script every day from crontab.
# Example: sudo crontab -e
# Place this line below in your crontab document:
# 0 1 * * * sudo ~/domoticz/scripts/domoticz_backup.sh
# Backup wil start every night at 1:00 hour past midnight
#
# LOCAL/FTP/SCP/MAIL/FILE PARAMETERS
SERVER="191.168.2.11" # IP of NAS, used for ftp
USERNAME="xxxxx" # FTP username of Network disk used for ftp
PASSWORD="xxxxxxxxx" # FTP password of Network disk used for ftp
DESTDIR="/tmp" # used for temorarily storage
DESTDIRNAS="Backups/Domoticz" # Path to your NAS backup folder
DOMO_IP="192.168.2.26" # Domoticz IP
DOMO_PORT="8086" # Domoticz port
DOMO_PATH="/home/pi/domoticz" # Path to your Domoticz folder on raspberry
TIMESTAMP=`/bin/date +%d-%m-%Y`
BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
BACKUPFILEGZ="$BACKUPFILE".gz
### END OF USER CONFIGURABLE PARAMETERS
### First remove old backups
sudo /bin/rm $DOMO_PATH/backups/*
### Create backup and ZIP it
sudo /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > $DESTDIR/$BACKUPFILE
sudo gzip -1 $DESTDIR/$BACKUPFILE
sudo tar -zcvf $DESTDIR/domoticz_$TIMESTAMP.tar.gz $DOMO_PATH
### Send to Network disk through FTP
sudo curl -u "$USERNAME:$PASSWORD" --ftp-create-dirs "ftp://192.168.2.11/$DESTDIRNAS/$TIMESTAMP/"
sudo curl -s --disable-epsv -v -T "$DESTDIR/$BACKUPFILEGZ" -u "$USERNAME:$PASSWORD" "ftp://192.168.2.11/$DESTDIRNAS/$TIMESTAMP/"
sudo curl -s --disable-epsv -v -T "$DESTDIR/domoticz_$TIMESTAMP.tar.gz" -u "$USERNAME:$PASSWORD" "ftp://192.168.2.11/$DESTDIRNAS/$TIMESTAMP/"
### Remove temp backup file
sudo /bin/rm $DESTDIR/$BACKUPFILEGZ
sudo /bin/rm $DESTDIR/domoticz_$TIMESTAMP.tar.gz
## --------------------------------------------------------------------------------------------------- ##
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="sftp -b- -oPort=22 $USERNAME@$SERVER"
putdir=$DESTDIRNAS
ndays=14
# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
TT=`date --date="$ndays days ago" +%s`
echo removing files older than $MM $DD
# get directory listing from remote source
echo "
cd $putdir
ls -l
"|$ftpsite >dirlist
# skip first three and last line, ftp command echo
listing="`tail -n+4 dirlist|head -n-1`"
lista=( $listing )
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
# echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
fdate="${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} ${lista[`expr $FNO+7`]}"
sdate=`date --date="$fdate" +%s`
# check the date stamp
if [ $sdate -lt $TT ]
then
# Remove this file
echo "$MM $DD: Removing ${lista[`expr $FNO+5`]} / ${lista[`expr $FNO+6`]} / ${lista[`expr $FNO+8`]}"
$ftpsite <<EOMYF2
cd $putdir
rm ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
done
The script gets stuck on the next part.
Code: Select all
# get directory listing from remote source
echo"
cd $putdir
ls -l
"|$ftpsite > directory
Code: Select all
* We are completely uploaded and fine
* Remembering we are in dir "Backups/Domoticz/07-12-2022/"
< 226 Transfer complete
* Connection #0 to host 192.168.2.11 left intact
removing files older than nov 23
Perhaps there is an easier way to delete older content folders than I highly recommend.