Hello,
I have setup a FTP daily backup and a cleanup of the older files on my Synology NAS based on the wiki :
https://wiki.domoticz.com/Daily_backup_to_external_hdd
First I have created in the NAS a dedicated user for this task with only the right to use the FTP and with an access limited to the shared folder I use for the backups and images saving of my Rasps. Like this, I reduce the chance to spoil anything else than this folder.
I have used FTP exclusively for the cleansup because SFTP requires access with SSH keys and as my use is exclusively on my private and secured network, I don't need to add more security to the exchanges Raspberry to/from NAS.
FTP was not available on my Raspberry and I had to install it.
To check if it is avaliable :
If FTP is not avalable on your Raspberry, you have to install it :
Code: Select all
sudo apt update
sudo apt install ftp
I didn't skip first three and last lines in the cleaning process as in the wiki because I have not seen a valid reason to do so
Telegram stuffs are also disabled as I don't have that.
Here is my script :
Code: Select all
#!/bin/bash
# LOCAL/FTP/SCP/MAIL PARAMETERS
SERVER="192.168.x.y" # IP of Synology NAS, used for ftp
USERNAME="<MyExclusiveFTPUser>" # FTP username of Network disk used for ftp
PASSWORD="<Password of MyExclusiveFTPUser>" # FTP password of Network disk used for ftp
DESTDIR="/opt/backup" # used for temporarily storage
DESTDIRNAS="<Path to the shared folder/Sub-Folder/Other Sub-Folder/etc/>" # Path to your Synology NAS backup folder
DOMO_IP="192.168.x.z" # Domoticz IP
DOMO_PORT="8080" # Domoticz port
### END OF USER CONFIGURABLE PARAMETERS
TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
BACKUPFILEGZ="$BACKUPFILE".gz
### Create backup and ZIP it
/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/$BACKUPFILE
gzip -9 /tmp/$BACKUPFILE
tar -zcvf /tmp/domoticz_scripts_$TIMESTAMP.tar.gz /home/pi/domoticz/scripts/
### tar -zcvf /tmp/telegram_scripts_$TIMESTAMP.tar.gz /home/pi/tg/scripts/
### Send to Network disk through FTP
curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEGZ" -u"$USERNAME:$PASSWORD" "ftp://$SERVER/$DESTDIRNAS"
curl -s --disable-epsv -v -T"/tmp/domoticz_scripts_$TIMESTAMP.tar.gz" -u"$USERNAME:$PASSWORD" "ftp://$SERVER/$DESTDIRNAS"
### curl -s --disable-epsv -v -T"/tmp/telegram_scripts_$TIMESTAMP.tar.gz" -u"$USERNAME:$PASSWORD" "ftp://$SERVER/$DESTDIRNAS"
### Remove temp backup file
/bin/rm /tmp/$BACKUPFILEGZ
/bin/rm /tmp/domoticz_scripts_$TIMESTAMP.tar.gz
### /bin/rm /tmp/telegram_scripts_$TIMESTAMP.tar.gz
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="ftp -inv $SERVER"
putdir=$DESTDIRNAS
ndays=30
# 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
# Collect file list
ftp -inv $SERVER <<EOF > dirlist
user $USERNAME $PASSWORD
passive
cd $DESTDIRNAS
ls -l
bye
EOF
# We look after the lines that are files only
grep '^-' dirlist | while read -r line; do
# Sort out the fields : month day hour/year file_name
set -- $line
month=$6
day=$7
hour_ou_year=$8
file_name=$9
# Build a date from the fields
fdate="$month $day $hour_or_year"
sdate=$(date --date="$fdate" +%s 2>/dev/null)
if [ -z "$sdate" ]; then
echo "⚠️ invalid date : $fdate – file ignored"
continue
fi
# Comparaison
if [ $sdate -lt $TT ]; then
echo "$MM $DD: Delete $file_name (from $fdate)"
ftp -inv $SERVER <<EOF
user $USERNAME $PASSWORD
passive
cd $putdir
delete $file_name
bye
EOF
fi
done
Cleanup is set at 30 days. You can adjust it at your wish.
Obviously, I have chmod the script and add a line in the crontab for a daily running. All that is explained in the wiki.
I'm not an expert in batch scripts, and this one was partially created using ChatGPT. So please be kind, and if you see any improvements you can make, please let me know.
It works perfectly for me, and I hope it can help others.