Automate dietpi-backup

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

Re: Automate dietpi-backup

Post by johnvick » Sat May 19, 2018 7:26 pm

All working well thanks again @MichaIng

unixmit
Posts: 32
Joined: Wed Jun 13, 2018 6:37 pm

Re: Automate dietpi-backup

Post by unixmit » Tue Jul 10, 2018 4:42 pm

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
Legend
Posts: 816
Joined: Sat Nov 18, 2017 5:21 pm

Re: Automate dietpi-backup

Post by MichaIng » Tue Jul 10, 2018 4:56 pm

@unixmit
Jep, somehow cron does not allow tput anymore. Fix: https://github.com/Fourdee/DietPi/commi ... 0174844090

User avatar
brightwolf
Posts: 4
Joined: Sun Feb 10, 2019 1:51 pm
Location: The Netherlands

Re: Automate dietpi-backup

Post by brightwolf » Sun Feb 10, 2019 1: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. 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
[email protected] PI 3B+ running FHEM domotics server

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

Re: Automate dietpi-backup

Post by MichaIng » Sun Feb 10, 2019 3:48 pm

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

Post Reply