Automate dietpi-backup

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

Re: Automate dietpi-backup

Post by johnvick »

All working well thanks again @MichaIng
unixmit
Posts: 45
Joined: Wed Jun 13, 2018 7:37 pm

Re: Automate dietpi-backup

Post by unixmit »

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:

Code: Select all

#!/bin/bash

/DietPi/dietpi/dietpi-backup 1
In crontab I have:

Code: Select all

* 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
User avatar
MichaIng
Site Admin
Posts: 2421
Joined: Sat Nov 18, 2017 6:21 pm

Re: Automate dietpi-backup

Post by MichaIng »

@unixmit
Jep, somehow cron does not allow tput anymore. Fix: https://github.com/Fourdee/DietPi/commi ... 0174844090
User avatar
brightwolf
Posts: 6
Joined: Sun Feb 10, 2019 2:51 pm
Location: The Netherlands

Re: Automate dietpi-backup

Post by brightwolf »

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:

Code: Select all

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
DietPi@Raspberry PI 3B+ running FHEM domotics server

"I have never tried that before, so I think I should definitely be able to do that" - Pippi Longstocking
User avatar
MichaIng
Site Admin
Posts: 2421
Joined: Sat Nov 18, 2017 6:21 pm

Re: Automate dietpi-backup

Post by MichaIng »

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.

Code: Select all

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
/DietPi/dietpi/dietpi-backup 1
@brightwolf
brightwolf wrote: Sun Feb 10, 2019 2:58 pm 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.
Ah I see, but the result will be exactly the same?

Code: Select all

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

Code: Select all

    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 :lol:.
User avatar
noobian
Posts: 26
Joined: Sun Mar 24, 2019 8:57 am

Re: Automate dietpi-backup

Post by noobian »

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 :?

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?
User avatar
MichaIng
Site Admin
Posts: 2421
Joined: Sat Nov 18, 2017 6:21 pm

Re: Automate dietpi-backup

Post by MichaIng »

@noobian
The below will automatically create a backup every day:

Code: Select all

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
/DietPi/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 also possible as well to define a more individual schedule via crontab.
User avatar
noobian
Posts: 26
Joined: Sun Mar 24, 2019 8:57 am

Re: Automate dietpi-backup

Post by noobian »

@MichaIng

wow ... thanks so much!
never heard of "cat << EO" before ... nice :)

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?
User avatar
MichaIng
Site Admin
Posts: 2421
Joined: Sat Nov 18, 2017 6:21 pm

Re: Automate dietpi-backup

Post by MichaIng »

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