Automate dietpi-backup

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

John

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_INTERACTIVE=0 to force recognition of non-interactive shell. Full command would be then:
G_INTERACTIVE=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.

Hi MichaIng

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

#!/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 :slight_smile:

John

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

This works nicely

#!/bin/bash
#
cd /mnt/usb_2
cp -aR /mnt/usb_2/dietpi-backup "/mnt/usb_2/dietpi-backup-$(date +"%m-%d-%y-%r")"
/boot/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

Better solution:

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

johnvick
Nice one :smiley:. 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:

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
/boot/dietpi/dietpi-backup 1

Thought about implementing something like into dietpi-backups itself.

Nice one MichaIng I’ll adopt your method.

Thanks, John

I get this error - any ideas? MichaIng

I’m not very good at scripts or coding!

root@Odroid:/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'

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 :slight_smile:.

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

All working well thanks again MichaIng

I am trying and failing with this. I want a cronjob to run a script that runs the backup automatically.

In my script I have:

#!/bin/bash

/DietPi/dietpi/dietpi-backup 1

In crontab I have:

* 3  *   *   *     sh /home/unix/dietpi-backup.sh

It never runs the backup utility. The script runs as I have set it to create a log file when it runs. If I run the script manually it works too.

Can anyone see what I’m missing?
Thank you

unixmit
Jep, somehow cron does not allow tput anymore. Fix: https://github.com/Fourdee/DietPi/commit/82ac7b32d32dca9db4fdb824c7ead80174844090

Thanks for the script. However, when I run it as published here, I end up with a ‘dietpi-backup-’ directory, i.e. a dietpi backup without a number. If you’re a Linux scripting beginner like me, this could cause some headaches. I solved it by isolating the new backup number calculation on a seperate line. Here’s my version of the script:

MAX_BACKUPS=10
cd /mnt/usbstick
if [[ -d dietpi-backup ]]; then
  rm dietpi-backup-$MAX_BACKUPS
  for ((i=$MAX_BACKUPS-1;i>1;i--)); do
    j=$((i+1))
    mv dietpi-backup-$i dietpi-backup-$j
  done
  mv dietpi-backup dietpi-backup-2
fi
/DietPi/dietpi/dietpi-backup 1

rm dietpi-backup-$MAX_BACKUPS does not work for directories. This requires the -R command. As well to reduce disk writes, I would use the oldest backup as a start point for the new one, so incremental sync can avoid unnecessary writes.
As well check for existing dirs, before moving them, to avoid error messages.

MAX_BACKUPS=10
cd /mnt/usbstick
[[ -d dietpi-backup-$MAX_BACKUPS ]] && mv dietpi-backup-$MAX_BACKUPS dietpi-backup-tmp
if [[ -d dietpi-backup ]]; then
  for ((i=$MAX_BACKUPS-1;i>1;i--)); do
    [[ -d dietpi-backup-$i ]] && mv dietpi-backup-$i dietpi-backup-$((i+1))
  done
  mv dietpi-backup dietpi-backup-2
fi
[[ -d dietpi-backup-tmp ]] && mv dietpi-backup-tmp dietpi-backup
/boot/dietpi/dietpi-backup 1

brightwolf

Ah I see, but the result will be exactly the same?

    j=$((i+1))
    mv dietpi-backup-$i dietpi-backup-$j

and

    mv dietpi-backup-$i dietpi-backup-$((i+1))

leads to the same backup dir structure.

Currently /path/to/dietpi-backup (without appending number) is required to allow the dietpi-backup script restoring this backup. You could tweak the naming a bid, e.g. starting the rotated backup dirs with dietpi-backup-1 or dietpi-backup.1, but for easy rotation a number is required and as said the initial dir without number as well.

I see if I can add this natively with v6.22 to have a more consistent/user friendly solution and find a way to use time/date format instead of numbers. So e.g. every backup will have a time/data appendix and when recovering you can choose from which date to recover. It just causes me headache when thinking about how to reliably find the oldest date :rofl:.

Hi

I’m running the “Native PC for UEFI”-DietPi on a Z83-F & love it.

Since I am new to Linux, I don’t know where to save or call the .sh/script mentioned in this thread :thinking:

I would need to generate versioned (named by timestamp?) backups in /mnt/mybackuplocation
… so I could return to different states of the DietPi. Instead of re-installing the whole system over and over again.
But whenever I run “dietpi-backup 1” my existing backup just gets overwritten.

An automated system-backup would be great (like a timeshift for DietPi), but I even would call it manually, mainly the versioned backups would be helpfull. Any tip on how to achieve this?

noobian
The below will automatically create a backup every day:

cat << '_EOF_' > /etc/cron.daily/backup
#!/bin/bash
MAX_BACKUPS=10
cd /mnt/mybackuplocation
[[ -d dietpi-backup-$MAX_BACKUPS ]] && mv dietpi-backup-$MAX_BACKUPS dietpi-backup-tmp
if [[ -d dietpi-backup ]]; then
  for ((i=$MAX_BACKUPS-1;i>1;i--)); do
    [[ -d dietpi-backup-$i ]] && mv dietpi-backup-$i dietpi-backup-$((i+1))
  done
  mv dietpi-backup dietpi-backup-2
fi
[[ -d dietpi-backup-tmp ]] && mv dietpi-backup-tmp dietpi-backup
/boot/dietpi/dietpi-backup 1
_EOF_
chmod +x /etc/cron.daily/backup
  • You can adjust the backup location via dietpi-backup directly. Then assure that the parent directory matches the one above: /mnt/mybackuplocation
  • You can adjust the time (hour + minute) of the daily cron job via dietpi-cron.
  • And of course adjust MAX_BACKUPS to your needs.
  • Place the script into /etc/cron.weekly instead if this is regular enough. It would be possible as well to define a more individual schedule via crontab.

MichaIng

wow … thanks so much!
never heard of “cat << EO” before … nice :slight_smile:

is it possible to run a daily & a weekly backupscript next to each other, without them overwriting each other? so I could keep 2 weekly backups, and 7 daily ones?

The one weekly backup would match one of the daily backups but of course it is possible. You could add a weekly cron job that syncs the 7th daily backup to another sub dir and before that the existing weekly backup to another one. So you would have 7 daily backups and two weekly that are up to two weeks older than the last daily. Since cron jobs are executed concurrently we have to think about how to assure the same dir is not synced concurrently. Perhaps add it to the same script and check for a certain day of week.

Hi MichaIng

Above is not included yet inside the menu options?

So the above coding is the way to go?