Different Backups from Crontab to network

All kinds of 'OS' scripts

Moderator: leecollings

astrapowerrr
Posts: 141
Joined: Tuesday 31 January 2017 20:34
Target OS: -
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by astrapowerrr »

Hi all,

Before messing things up I don’t understand I ask the question first.. maybe very simple answer..
I’m using this script for a while now and recently I messed up some Domoticz settings.
Wanted to backup the dB but they were not fine.. so I went to investigate.
Seems that’s about a year ago the file size got smaller. From about 600kb to 47kB. How is that possible..
The 47 kB file is empty too if I unpack..

When making manual backup it works fine.
But when using the script from te wiki to make backup to nas it fails. But without errors. So it makes a file of backup .db.gz. And copies is nicely to my nas.


You guys know how to fix this?
Well i found my problem...
i bought a new router last year and this came with a minor ip nr difference.
strange but true that the file got written to my server with a wrong ip?(but thats not the issue here.)
anyhow changed the ip numbers to the correct ones and up and running
plenkske
Posts: 5
Joined: Saturday 01 February 2020 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by plenkske »

DAVIZINHO wrote: Saturday 19 May 2018 16:56 hello.
I use this method and works great. i make a backup of various folders in my nas, every day

The problem for me is in the part of script to delete de files with XX days:

# get directory listing from remote source
echo "
cd $putdir
ls -l
"|$ftpsite >dirlist

the return to me:
Host key verification failed.
Couldn't read packet: Connection reset by peer

any idea?
I have the exact same problem.
Tried some things but could not find a solution. Someone here that can help ?
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Different Backups from Crontab to network

Post by Derik »

Perhaps some one has a backup to google drive working?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by dressie »

Hi all! Hope you are having a great weekend!
Past week I moved my Domoticz from a Pi to Synology after years. I had a nice upgrade for my Synology, so it can handle the extra domoticz "load".
Migration went very well, some scripts had to be changed but overal no mayor issues.

One thing that I can't get back to work is this automatic back up. Have been searching the forum for it, but is there a solution for the database not returning with 0kB? The scripts works perfect, "www" and "scripts" are being put in a file. Database too, but with 0kB!

Anyone found a solution for this issue?

Thanks!
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Different Backups from Crontab to network

Post by Derik »

Your pad in the nas is different as the pi.
So make sure you find the good pad with a ftp [ winscp ] and repleace the old path with the new
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by dressie »

Derik wrote: Sunday 02 February 2020 11:28 Your pad in the nas is different as the pi.
So make sure you find the good pad with a ftp [ winscp ] and repleace the old path with the new
So I have tried some more things in the mean time. Through the website itself, the backup comes back with 0kB. I do have the auto-backup on, and they are okay...at least not 0kB.
I fixed the path in the script, thinking that was the issue, but it's completly something els.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Different Backups from Crontab to network

Post by Derik »

more info to see..
What is your script?
Crontab changed?
what if pad:./your.sh
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by dressie »

Derik wrote: Sunday 02 February 2020 14:38 more info to see..
What is your script?
Crontab changed?
what if pad:./your.sh
Script:
#!/bin/bash
DOMO_IP="127.0.0.1" # Domoticz IP
DOMO_PORT="8084" # Domoticz port
### END OF USER CONFIGURABLE PARAMETERS
TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
BACKUPFILE="domoticzbackup_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
BACKUPFILEGZ="$BACKUPFILE".gz
#Create backup and make tar archives
/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /volume1/Software/Backup/Domoticz/database/$BACKUPFILE
tar -zcvf /volume1/Software/Backup/Domoticz/scripts/domoticz_scripts_$TIMESTAMP.tar.gz /volume1/@appstore/domoticz/var/scripts/
tar -zcvf /volume1/Software/Backup/Domoticz/www/domoticz_wwwfolder_$TIMESTAMP.tar.gz /volume1/@appstore/domoticz/www/
#Delete backups older than 31 days
/usr/bin/find /volume1/Software/Backup/Domoticz/database/ -name '*.db' -mtime +31 -delete
/usr/bin/find /volume1/Software/Backup/Domoticz/scripts/ -name '*.tar.gz' -mtime +31 -delete
/usr/bin/find /volume1/Software/Backup/Domoticz/www/ -name '*.tar.gz' -mtime +31 -delete

No crontab, I use the taskmanager from Synology. Using already for other scripts and work fine.
Path to my .sh file: /volume1/@appstore/domoticz/var/scripts

The backupdatabase.php is not working, always comes back with 0kB. Like I mentioned before, a manual backup within settings is not working either.
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by dressie »

After a reboot of the NAS, it's working again.....looks like the "stop" and "start" option in the package center doesn't stop and restart everything.
Marcus22
Posts: 5
Joined: Sunday 06 November 2016 9:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by Marcus22 »

I am working on a Domoticz backup to my Synology NAS using this example. The scripts runs fine after some problem solving. When I run it, there are three files in the Backup folder on my NAS. The ony problem is that the database backup file is always only 47 bytes in size.

Knipsel.PNG
Knipsel.PNG (7.81 KiB) Viewed 4856 times

The scripts uses backupdatabase.php, the same way as making a backup from the settings screens works.

Code: Select all

 /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/$BACKUPFILE
Unfortunately clicking the Backup database button results in a frozen Domoticz service and I have to do a restart. I suspect this is the same problem.

I am running Domoticz 2020.2. And yes, getting this backup to work is due to the upgrade! ;)
The Raspbian is Buster:

Code: Select all

Linux raspberrypi 4.19.118-v7+ #1311 SMP Mon Apr 27 14:21:24 BST 2020 armv7l
Additional information:
- The backup button did work when I was running 4.9.
- The backup button did not work when I was running 4.10
- I now have installed SQLite3
- The database itself is around 540Mb and working just fine.

Code: Select all

-rw-r--r--   1 pi   pi   540295168 Jun  3 21:20 domoticz.db
- I cannot find backupdatabase.php anywhere on my Pi or in the Domoticz Github. As I understand from this post by waaren, the file does not exist, it is a command inside an html file that calls an internal sqlite3 command.
- I use https to access the Domoticz webpage
- Domoticz on my Pi:
Spoiler: show

Code: Select all

pi@raspberrypi:~/domoticz $ ls -l
total 544060
drwxr-xr--   5 pi   pi        4096 Dec  8 12:00 backups
drwxr-xr-x 106 pi   pi        4096 May 28 22:06 Config
-rw-r-----   1 root root       236 Jun  3 17:38 domocookie.txt
-rwxr-xr-x   1 pi   pi    13861696 Apr 26 13:48 domoticz
-rw-------   1 root root     25917 Mar 27 22:01 domoticz_crash.log
-rw-r--r--   1 pi   pi   540295168 Jun  3 21:20 domoticz.db
-rw-r--r--   1 pi   pi       32768 Jun  3 21:22 domoticz.db-shm
-rw-r--r--   1 pi   pi     2690392 Jun  3 21:22 domoticz.db-wal
-rw-r--r--   1 pi   pi        4433 Apr  2 10:06 domoticz.sh
drwxr-xr-x   4 pi   pi        4096 Apr  2 10:06 dzVents
-rw-r--r--   1 pi   pi       97954 Apr 26 13:27 History.txt
-rw-r--r--   1 pi   pi       35147 Apr  2 10:06 License.txt
-rw-r-----   1 root root       226 Dec 26 17:14 ozwcp.poll.XXXXXX.xml
-rw-r-----   1 root root        63 Dec 26 17:13 ozwcp.scenes.XXXXXX
-rw-r-----   1 root root      2131 Dec 26 17:13 ozwcp.stat.XXXXXX
-rw-r-----   1 root root        49 Dec 26 17:13 ozwcp.topo.XXXXXX
drwxr-xr-x   5 pi   pi        4096 Apr  2 10:06 plugins
drwxr-xr-x   9 pi   pi        4096 Jun  3 17:42 scripts
-rw-r--r--   1 pi   pi        3414 Apr  2 10:06 server_cert.pem
-rwxr-xr-x   1 pi   pi         610 Apr  2 10:06 updatebeta
-rwxr-xr-x   1 pi   pi         627 Apr  2 10:06 updaterelease
drwxr-xr-x  13 pi   pi        4096 Apr 26 09:14 www
- I can open the database in SQLite3

What else can I check to get the backupdatabase.php to return a valid backup of the Domoticz database?
Home automation as a hobby
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by roblom »

I have the same problem as above. For some reason using the backupdatabase.php from a script causes my Domoticz becomes unresponsive and eventually crashes.
But I was thinking, if Domoticz automatically created backups in the hourly, daily and monthly folders, and these do work and doesn't cause problems, why is it needed to use the backupdatabase.php again? Isn't it easier to just sync the content of the "/home/pi/domoticz/backups" folder to the NAS?
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by MikeyMan »

Been fiddling around with the scripts as well.

Seems to be working ok.

Domoticz*.tar.gz (197mb)
Domoticz*.db.gz (3,3mb)

get written to my Synology without any issues. Hoewever; it feels kind of double...?

Just the db.gz would be good enough for daily backups wouldn't it?

Also; cant get the removal of old dbs to work properly yet. WIP.

Seems to be hanging at:

< 226 Transfer complete.
* Connection #0 to host 10.0.0.4 left intact
removing files older than Jul 31
ssh: connect to host 10.0.0.4 port 221: Connection timed out
Connection closed
./backup.sh: 67: ./backup.sh: Syntax error: "(" unexpected

When i use shellcheck it does come up with some errors, but not at line 67...

Mostly:
Use $(...) notation instead of legacy backticked `...`
Double quote to prevent globbing and word splitting.
Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.
expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

as you can see; scripting is not my cup of tea...
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by MikeyMan »

Cleaned it up with spellcheck, now looking as follows:

Code: Select all

#!/bin/bash

    ## LOCAL/FTP/SCP/MAIL PARAMETERS
    SERVER="10.0.0.4"         # IP of Network disk, used for: ftp mail scp
    PORT="211"			#Port of ftp server
    USERNAME="Domoticz"         # FTP username of Network disk used for: ftp mail scp
    PASSWORD="Backup123!"               # FTP password of Network disk used for: ftp mail scp
    #DESTDIR="/opt/backup"      # used for: local
    DESTDIRNAS="/Domoticz/"
    DOMO_IP="10.0.0.30"      # Domoticz IP used for all
    DOMO_PORT="8080"         # Domoticz port used for all
    ## END OF USER CONFIGURABLE PARAMETERS

    ##Backup DBase##
    TIMESTAMP=$(/bin/date +%Y%m%d%H%M%S)
    BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
    BACKUPFILEGZ="$BACKUPFILE".gz
    ### Stop Domoticz, create backup, ZIP it and start Domoticz again
    #sudo service domoticz.sh stop
    /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/"$BACKUPFILE"
    #sudo service domoticz.sh start
    gzip -9 /tmp/"$BACKUPFILE"
    ### Send to Network disk through FTP
    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEGZ" -u"$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS/"				
    ### Remove temp backup file
    /bin/rm /tmp/"$BACKUPFILEGZ"
    ### Done!

    ##Backup Files##
#    TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
#    BACKUPFILEDIR="domoticz_$TIMESTAMP.tar.gz" # Change the xxx to yours
 
    ### Create backup and ZIP it
#    tar -zcvf /tmp/$BACKUPFILEDIR /home/pi/domoticz/backups   # Change the xxx to yours    # Or try /home/pi/domoticz/
    
    ### Send to Network disk through FTP
#    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEDIR" -u "$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS"  # Change the ftp to yours !!!
                                                    
    ### Remove temp backup file
#    /bin/rm /tmp/$BACKUPFILEDIR

    ### Done!
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="sftp -b-  -oPort=211  $USERNAME@$SERVER"
putdir=$DESTDIRNAS

ndays=19

# 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"
echo $putdir
echo $ndays

# 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
I'm down to the next errors:

Code: Select all

ssh_exchange_identification: Connection closed by remote host
Connection closed
backup.sh: 70: backup.sh: Syntax error: Bad for loop variable

Something is going wrong with the "Get directory listing" part.
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by MikeyMan »

Rewritten to curl, as in the first piece of code:

Code: Select all

#!/bin/bash

    ## LOCAL/FTP/SCP/MAIL PARAMETERS
    SERVER="10.0.0.4"         # IP of Network disk, used for: ftp mail scp
    PORT="211"			#Port of ftp server
    USERNAME="Domoticz"         # FTP username of Network disk used for: ftp mail scp
    PASSWORD="Backup123!"               # FTP password of Network disk used for: ftp mail scp
    #DESTDIR="/opt/backup"      # used for: local
    DESTDIRNAS="/Domoticz/"
    DOMO_IP="10.0.0.30"      # Domoticz IP used for all
    DOMO_PORT="8080"         # Domoticz port used for all
    ## END OF USER CONFIGURABLE PARAMETERS

    ##Backup DBase##
    TIMESTAMP=$(/bin/date +%Y%m%d%H%M%S)
    BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
    BACKUPFILEGZ="$BACKUPFILE".gz
    ### Stop Domoticz, create backup, ZIP it and start Domoticz again
    #sudo service domoticz.sh stop
    /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/"$BACKUPFILE"
    #sudo service domoticz.sh start
    gzip -9 /tmp/"$BACKUPFILE"
    ### Send to Network disk through FTP
    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEGZ" -u"$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS/"				
    ### Remove temp backup file
    /bin/rm /tmp/"$BACKUPFILEGZ"
    ### Done!

    ##Backup Files##
#    TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
#    BACKUPFILEDIR="domoticz_$TIMESTAMP.tar.gz" # Change the xxx to yours
 
    ### Create backup and ZIP it
#    tar -zcvf /tmp/$BACKUPFILEDIR /home/pi/domoticz/backups   # Change the xxx to yours    # Or try /home/pi/domoticz/
    
    ### Send to Network disk through FTP
#    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEDIR" -u "$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS"  # Change the ftp to yours !!!
                                                    
    ### Remove temp backup file
#    /bin/rm /tmp/$BACKUPFILEDIR

    ### Done!
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="curl -u "$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS""
#putdir=$DESTDIRNAS

ndays=19

# 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

echo "1"

# skip first three and last line, ftp command echo
listing=$(tail -n+0 dirlist|head -n-1)

echo "2"
echo "$listing"
echo "3"

lista="$listing"
echo "lista"
echo "4"

# 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
Now i get a full list back from $listing:

Code: Select all

-rwxrwxrwx   1 Domoticz users         2945480 Jun 17 10:47 domoticz_20200617104649.db.gz
-rwxrwxrwx   1 Domoticz users       180524349 Jun 17 10:49 domoticz_20200617104707.tar.gz
-rwxrwxrwx   1 Domoticz users         2941513 Jul 20 14:10 domoticz_20200720141038.db.gz
-rwxrwxrwx   1 Domoticz users       184107439 Jul 20 14:13 domoticz_20200720141057.tar.gz
-rwxrwxrwx   1 Domoticz users         3445758 Aug 19 15:01 domoticz_20200819150047.db.gz
-rwxrwxrwx   1 Domoticz users         3445361 Aug 19 15:04 domoticz_20200819150346.db.gz
-rwxrwxrwx   1 Domoticz users         3446379 Aug 19 15:06 domoticz_20200819150533.db.gz
-rwxrwxrwx   1 Domoticz users         3446315 Aug 19 15:07 domoticz_20200819150704.db.gz
-rwxrwxrwx   1 Domoticz users         3446321 Aug 19 15:10 domoticz_20200819150955.db.gz
-rwxrwxrwx   1 Domoticz users         3446069 Aug 19 15:22 domoticz_20200819152207.db.gz
-rwxrwxrwx   1 Domoticz users         3446224 Aug 19 15:23 domoticz_20200819152316.db.gz
-rwxrwxrwx   1 Domoticz users         3446254 Aug 19 15:25 domoticz_20200819152444.db.gz
-rwxrwxrwx   1 Domoticz users         3447300 Aug 19 15:28 domoticz_20200819152736.db.gz
-rwxrwxrwx   1 Domoticz users         3447302 Aug 19 15:29 domoticz_20200819152844.db.gz
-rwxrwxrwx   1 Domoticz users         3446817 Aug 19 15:31 domoticz_20200819153108.db.gz
-rwxrwxrwx   1 Domoticz users         3446852 Aug 19 15:33 domoticz_20200819153245.db.gz
-rwxrwxrwx   1 Domoticz users         3446937 Aug 19 15:34 domoticz_20200819153411.db.gz
-rwxrwxrwx   1 Domoticz users         3449900 Aug 19 16:07 domoticz_20200819160701.db.gz
-rwxrwxrwx   1 Domoticz users         3449912 Aug 19 16:09 domoticz_20200819160859.db.gz
No clue how to have those files removed now i must say... But safe to say there is some misalignment on the first and second part of the script on the wiki.
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Different Backups from Crontab to network

Post by MikeyMan »

Rewritten to curl, as in the first piece of code:

Code: Select all

#!/bin/bash

    ## LOCAL/FTP/SCP/MAIL PARAMETERS
    SERVER="10.0.0.4"         # IP of Network disk, used for: ftp mail scp
    PORT="211"			#Port of ftp server
    USERNAME="Domoticz"         # FTP username of Network disk used for: ftp mail scp
    PASSWORD="Backup123!"               # FTP password of Network disk used for: ftp mail scp
    #DESTDIR="/opt/backup"      # used for: local
    DESTDIRNAS="/Domoticz/"
    DOMO_IP="10.0.0.30"      # Domoticz IP used for all
    DOMO_PORT="8080"         # Domoticz port used for all
    ## END OF USER CONFIGURABLE PARAMETERS

    ##Backup DBase##
    TIMESTAMP=$(/bin/date +%Y%m%d%H%M%S)
    BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
    BACKUPFILEGZ="$BACKUPFILE".gz
    ### Stop Domoticz, create backup, ZIP it and start Domoticz again
    #sudo service domoticz.sh stop
    /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/"$BACKUPFILE"
    #sudo service domoticz.sh start
    gzip -9 /tmp/"$BACKUPFILE"
    ### Send to Network disk through FTP
    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEGZ" -u"$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS/"				
    ### Remove temp backup file
    /bin/rm /tmp/"$BACKUPFILEGZ"
    ### Done!

    ##Backup Files##
#    TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
#    BACKUPFILEDIR="domoticz_$TIMESTAMP.tar.gz" # Change the xxx to yours
 
    ### Create backup and ZIP it
#    tar -zcvf /tmp/$BACKUPFILEDIR /home/pi/domoticz/backups   # Change the xxx to yours    # Or try /home/pi/domoticz/
    
    ### Send to Network disk through FTP
#    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEDIR" -u "$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS"  # Change the ftp to yours !!!
                                                    
    ### Remove temp backup file
#    /bin/rm /tmp/$BACKUPFILEDIR

    ### Done!
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="curl -u "$USERNAME:$PASSWORD" "ftp://$SERVER:$PORT/$DESTDIRNAS""
#putdir=$DESTDIRNAS

ndays=19

# 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

echo "1"

# skip first three and last line, ftp command echo
listing=$(tail -n+0 dirlist|head -n-1)

echo "2"
echo "$listing"
echo "3"

lista="$listing"
echo "lista"
echo "4"

# 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
Now i get a full list back from $listing:

Code: Select all

-rwxrwxrwx   1 Domoticz users         2945480 Jun 17 10:47 domoticz_20200617104649.db.gz
-rwxrwxrwx   1 Domoticz users       180524349 Jun 17 10:49 domoticz_20200617104707.tar.gz
-rwxrwxrwx   1 Domoticz users         2941513 Jul 20 14:10 domoticz_20200720141038.db.gz
-rwxrwxrwx   1 Domoticz users       184107439 Jul 20 14:13 domoticz_20200720141057.tar.gz
-rwxrwxrwx   1 Domoticz users         3445758 Aug 19 15:01 domoticz_20200819150047.db.gz
-rwxrwxrwx   1 Domoticz users         3445361 Aug 19 15:04 domoticz_20200819150346.db.gz
-rwxrwxrwx   1 Domoticz users         3446379 Aug 19 15:06 domoticz_20200819150533.db.gz
-rwxrwxrwx   1 Domoticz users         3446315 Aug 19 15:07 domoticz_20200819150704.db.gz
-rwxrwxrwx   1 Domoticz users         3446321 Aug 19 15:10 domoticz_20200819150955.db.gz
-rwxrwxrwx   1 Domoticz users         3446069 Aug 19 15:22 domoticz_20200819152207.db.gz
-rwxrwxrwx   1 Domoticz users         3446224 Aug 19 15:23 domoticz_20200819152316.db.gz
-rwxrwxrwx   1 Domoticz users         3446254 Aug 19 15:25 domoticz_20200819152444.db.gz
-rwxrwxrwx   1 Domoticz users         3447300 Aug 19 15:28 domoticz_20200819152736.db.gz
-rwxrwxrwx   1 Domoticz users         3447302 Aug 19 15:29 domoticz_20200819152844.db.gz
-rwxrwxrwx   1 Domoticz users         3446817 Aug 19 15:31 domoticz_20200819153108.db.gz
-rwxrwxrwx   1 Domoticz users         3446852 Aug 19 15:33 domoticz_20200819153245.db.gz
-rwxrwxrwx   1 Domoticz users         3446937 Aug 19 15:34 domoticz_20200819153411.db.gz
-rwxrwxrwx   1 Domoticz users         3449900 Aug 19 16:07 domoticz_20200819160701.db.gz
-rwxrwxrwx   1 Domoticz users         3449912 Aug 19 16:09 domoticz_20200819160859.db.gz
No clue how to have those files removed now i must say... But safe to say there is some misalignment on the first and second part of the script on the wiki.
hendrikenrenny
Posts: 18
Joined: Monday 20 April 2015 21:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Different Backups from Crontab to network

Post by hendrikenrenny »

Hi,

I have been using the scripts (1st one) from the wiki (https://www.domoticz.com/wiki/Daily_bac ... ternal_hdd) successfully for the last years. However, recently the scripts fails. It only sends 1 kb files to the back-up destination, while the domoticz database is around 35 Mb.

With trial and error, I found out that it seems that the "backupdatabase.php" thing doesnot do what it is supposed to do. (Sorry, I'm not a programmer....). The FTP connection between source (RPI) and destination (nas) seems to be working fine.

Any idea how to solve it?

Hendrik

Using Domoticz 2020.2
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Different Backups from Crontab to network

Post by waaren »

hendrikenrenny wrote: Thursday 12 November 2020 20:43 I have been using the scripts (1st one) from the wiki (https://www.domoticz.com/wiki/Daily_bac ... ternal_hdd) successfully for the last years. However, recently the scripts fails. It only sends 1 kb files to the back-up destination, while the domoticz database is around 35 Mb.
Any idea how to solve it?
Try removing the lines

Code: Select all

service domoticz stop 
service domoticz start
The backup command

Code: Select all

/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/"$BACKUPFILE"
will not work when domoticz is down. (I also updated the wiki page accordingly)
Reason that it could work before is probably that the domoticz was not effectively stopped yet when the backup command via curl came in.

Also have a look at this post where the use of sqlite3 .backup command is mentioned.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
hendrikenrenny
Posts: 18
Joined: Monday 20 April 2015 21:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Different Backups from Crontab to network

Post by hendrikenrenny »

Hi,

Removing the stop/start of Domoticz helped.

Thanks Waaren!

Hendrik
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests