Script to backup to a Synology NAS

All kinds of 'OS' scripts

Moderator: leecollings

desertdog
Posts: 84
Joined: Sunday 14 August 2016 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Script to backup to a Synology NAS

Post by desertdog »

I have changed all the credentials to
SERVER="192.168.1.100" # IP of Synology NAS, used for ftp
USERNAME="RPiBackup" # FTP username of Network disk used for ftp
PASSWORD="XXXXXX" # FTP password of Network disk used for ftp
DESTDIR="/opt/backup" # used for temorarily storage
DESTDIRNAS="/domoticz/" # Path to your Synology NAS backup folder
DOMO_IP="192.168.1.104" # Domoticz IP
DOMO_PORT="8080" # Domoticz port

but I still can't seem to make it work. when I read the logs, the errors I get are

Code: Select all

Connected to 192.168.1.100 (192.168.1.100) port 21 (#0)
< 220 SERVERNAME FTP server ready.
> USER RPiBackup
< 331 Password required for RPiBackup.
> PASS fXXXXXX
< 230 User RPiBackup logged in.
> PWD
< 257 "/" is current directory.
* Entry path is '/'
> CWD /
* ftp_perform ends with SECONDARY: 0
< 250 CWD command successful.
> CWD domoticz
< 550 No such file or directory.
* Server denied you to change to the given directory
On my Synology, there is a folder domoticz in the root of the ftp shared folder. (chmodded 777)
I also tried removing the directory from the config file and just leaving '/', but i then get the error:

Code: Select all

> TYPE I
< 200 Type set to I.
> STOR telegram_scripts_20210406140401.tar.gz
< 553 telegram_scripts_20210406140401.tar.gz: Permission denied.
* Failed FTP upload: 553
* Remembering we are in dir "/"
Michel13
Posts: 69
Joined: Thursday 07 January 2016 19:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: France
Contact:

Re: Script to backup to a Synology NAS

Post by Michel13 »

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 :

Code: Select all

which ftp
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest