Automate dietpi-backup

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Automate dietpi-backup

Post by johnvick » Fri May 18, 2018 4:04 am

Is it possible to do this from script/cron job?

John

User avatar
MichaIng
Legend
Posts: 542
Joined: Sat Nov 18, 2017 5:21 pm

Re: Automate dietpi-backup

Post by MichaIng » Fri May 18, 2018 10:45 am

Hi @johnvick,

yes this should be possible via dietpi-backup 1, which takes the settings chosen via dietpi-backup before. However we do some automated check for interactive shell, which appears to work fine for cron and systemd units, but in case the command hangs, please add G_USER_INPUT=0 to force recognition of non-interactive shell. Full command would be then:
G_USER_UNPUT=0 dietpi-backup 1

@Fourdee
As for G_WHIP_YESNO in non-interactive shells "yes" is chosen, right now insufficient free space is ignored. We should change this. Possible:
- Always ask G_WHIP_YESNO in a way, that "yes" (return 0) is the safest/most compatible answer.
- As by default "no" is selected, do it the other way round and chose "no" if non-interactive.
- Actively chose default answer/input in code and use this as non-interactive result as well.

User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Automate dietpi-backup

Post by johnvick » Fri May 18, 2018 11:12 am

Hi @MichaIng

Thanks for the help - dietpi-backup 1 works from the command line but putting it in a simple script fails:

Code: Select all

#!/bin/bash
#
dietpi-backup 1
Gives an error message

./backup.sh: line 3: dietpi-backup: command not found.

The reason for trying to set this up is that my backup is never recent enough :)

John

User avatar
MichaIng
Legend
Posts: 542
Joined: Sat Nov 18, 2017 5:21 pm

Re: Automate dietpi-backup

Post by MichaIng » Fri May 18, 2018 12:15 pm

@johnvick
Ah sorry, our aliases are not present within scripts, use
/DietPi/dietpi/dietpi-backup 1

User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Automate dietpi-backup

Post by johnvick » Sat May 19, 2018 1:25 am

This works nicely

Code: Select all

#!/bin/bash
#
cd /mnt/usb_2
cp -aR /mnt/usb_2/dietpi-backup "/mnt/usb_2/dietpi-backup-$(date +"%m-%d-%y-%r")"
/DietPi/dietpi/dietpi-backup 1
:

Copies the old backup with the current date appended (maybe not the best solution but it allows you to have multiple backups) and then creates a new one.

John

User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Automate dietpi-backup

Post by johnvick » Sat May 19, 2018 1:51 am

Better solution:

Code: Select all

#!/bin/bash
#
cd /mnt/usb_2
mv /mnt/usb_2/dietpi-backup "/mnt/usb_2/dietpi-backup-$(date +"%m-%d-%y-%r")"
/DietPi/dietpi/dietpi-backup 1

User avatar
MichaIng
Legend
Posts: 542
Joined: Sat Nov 18, 2017 5:21 pm

Re: Automate dietpi-backup

Post by MichaIng » Sat May 19, 2018 2:09 am

@johnvick
Nice one :D. I do it a bid similar for my MariaDB dumps, although I don't like that it grows unlimited until you clean up by yourself.
Another way is adding a count number to file name, always removing a defined max number and incrementally increase the earliers files count before doing fresh backup:

Code: Select all

MAX_BACKUPS=10
cd /mnt/usb_2
if [[ -d dietpi-backup ]]; then
  rm dietpi-backup-$MAX_BACKUPS
  for ((i=$MAX_BACKUPS-1;i>1;i--); do
    mv dietpi-backup-$i dietpi-backup-$( (( $i+1 )) )
  done
  mv dietpi-backup dietpi-backup-2
fi
/DietPi/dietpi/dietpi-backup 1
Thought about implementing something like into dietpi-backups itself.

User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Automate dietpi-backup

Post by johnvick » Sat May 19, 2018 2:14 am

Nice one @MichaIng I'll adopt your method.

Thanks, John

User avatar
johnvick
Legend
Posts: 563
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Automate dietpi-backup

Post by johnvick » Sat May 19, 2018 6:06 am

I get this error - any ideas? @MichaIng

I'm not very good at scripts or coding!

Code: Select all

[email protected]:/home/john/scripts# ./backup.sh
./backup.sh: line 5: syntax error near `;'
./backup.sh: line 5: `  for ((i=$MAX_BACKUPS-1;i>1;i--); do'

User avatar
MichaIng
Legend
Posts: 542
Joined: Sat Nov 18, 2017 5:21 pm

Re: Automate dietpi-backup

Post by MichaIng » Sat May 19, 2018 9:53 am

@johnvick
Forgot the second closing parenthesis: ((...i--));

Also mv inside same folder should really just rename files, but to assure you could add time output around the loop to check if it really just takes a second or so:
echo $(date +'%H:%M:%S')

Would be nice to keep timestamps inside the file names actually for better archiving/review, but then there is a safe way needed to assure that really the oldest file is removed. Will try around a bid :).

I opened issue on github, would like to have this implemented into DietPi-Backup: https://github.com/Fourdee/DietPi/issues/1785

Post Reply