Backup script in cron.daily

Hello,

I created a backup Script on my raspberry pi 4 and placed it in /etc/cron.daily and named it “backup”. I also Set the cron.daily runtime.

But the Script won’t run. When i Start it via terminal it just works. Can you help me?

Thank you very much :slight_smile:

The permissions i set:

-rwxr-xr-x 1 root root 169 Oct 26 10:39 /etc/cron.daily/backup

The Script itself:

#!/bin/bash
#
rm -rf /mnt/hdd1/backup/dietpi-backup_old
cd /mnt/hdd1
mv /mnt/hdd1/backup/dietpi-backup "/mnt/hdd1/backup/dietpi-backup_old"
/boot/dietpi/dietpi-backup 1

Hi,

many thanks for your message. You could perform a dry-run using run-parts --test /etc/cron.daily/. This should display all scripts that’s going to be executed on daily basis.

Thank you very much.

root@xxx:~# run-parts --test /etc/cron.daily/
/etc/cron.daily//apt-compat
/etc/cron.daily//backup
/etc/cron.daily//dietpi
/etc/cron.daily//dpkg
/etc/cron.daily//passwd
/etc/cron.daily//samba
root@xxx:~#

Seems like it should work. But the backup was not created last night. When i run the Script via terminal the backup is working…

you can have a look to journalctl -u cron.service and check for the time frame the daily jobs should be executed. Maybe there are messages related to your script.

I think i found the issue… I excluded cron service from dietpi autocontrol… :roll_eyes: :rofl:

The Script is now executed daily. Many Thanks! :slight_smile:

ah ok, the cron.service was not started, right?

Exactly, i think so :smiley:

clear, good you found it :slight_smile:

If you want to keep cron running through DietPi updates/installs/backups and such, hence exclude it from DietPi control, enable it to be started by systemd instead: systemctl enable --now cron
But while there are cases where servers/services need to run while you still want to install something, cron is really the last thing I’d consider important enough, while it’s important to be generally running as used by many 3rd party software to do their maintenance jobs.

Thanks, that would be useful for samba :slight_smile: can i only Set services that are shown in dietpi Service control?

For anyone who is interested in, my updated basic backup Script for cron.weekly:

#!/bin/bash
#
rm -rf /mnt/hdd1/backup/dietpi-backup_4
mv /mnt/hdd1/backup/dietpi-backup_3 "/mnt/hdd1/backup/dietpi-backup_4"
mv /mnt/hdd1/backup/dietpi-backup_2 "/mnt/hdd1/backup/dietpi-backup_3"
mv /mnt/hdd1/backup/dietpi-backup "/mnt/hdd1/backup/dietpi-backup_2"
/boot/dietpi/dietpi-backup 1

The script keeps the 4 latest backups, a whole month on my usb hard disk mounted as /hdd1.

You can add services to the dietpi-services control via its GUI.

Btw in your rotate script, move your last directory (4) to the initial place instead of removing it. This will reduce the execution time and writes as only changes are synced while identical files are left unchanged :wink:.

Great idea, thanks :slight_smile:

#!/bin/bash
#
mv /mnt/hdd1/backup/dietpi-backup_4 "/mnt/hdd1/backup/dietpi-backup"
mv /mnt/hdd1/backup/dietpi-backup_3 "/mnt/hdd1/backup/dietpi-backup_4"
mv /mnt/hdd1/backup/dietpi-backup_2 "/mnt/hdd1/backup/dietpi-backup_3"
cp /mnt/hdd1/backup/dietpi-backup "/mnt/hdd1/backup/dietpi-backup_2"
/boot/dietpi/dietpi-backup 1

Do you mean Like this:
Move (4) to Initial and copy Initial to (2)?

should be more like this

mv /mnt/hdd1/backup/dietpi-backup_3 "/mnt/hdd1/backup/dietpi-backup_4"
mv /mnt/hdd1/backup/dietpi-backup_2 "/mnt/hdd1/backup/dietpi-backup_3"
mv /mnt/hdd1/backup/dietpi-backup "/mnt/hdd1/backup/dietpi-backup_2"
mv /mnt/hdd1/backup/dietpi-backup_4 "/mnt/hdd1/backup/dietpi-backup"
/boot/dietpi/dietpi-backup 1

Thank you very much :slight_smile:

My external hdd1 is formatted in ext4. How can i copy the files to a ntfs formatted Computer or hdd? I tried it but there were symlink/permission errors or to many files to copy or the Filesystem does Not Support something.

Another question: on raspbian it was possible to backup my whole sd card to an Image. Is this also possible on dietpi?

I would recommend to use ext4 as target device to keep thinks like permissions and sym links.

To creaqte an image, you can use dietpi-imager: For this you would need to plug your SD card into another system

https://github.com/MichaIng/DietPi/issues/3229#issuecomment-554477828

Jep dietpi-imager is an idea, although its purpose is to create a fresh DietPi image and for this it minimises the file system and partition size.

Was this possible on Raspbian from the running system itself? I mean in theory /dev/mmcblk0 can be read to an image file via dd, but I would never assume that there will be a reliable result as long as any of the file systems is still writable. And remounting the root file system read-only is not reliably possible as there are usually still parts write-opened by some system process, as Joulinar might remember, the failing and hard to investigate mount -o remount,ro / :frowning:. So if there is any tool on Raspbian that manages this, I would be happy to check out the method and implement something similar into dietpi-backup :slight_smile:.