billw2 / rpi-clone

A shell script to clone a booted disk.
BSD 3-Clause "New" or "Revised" License
2.49k stars 327 forks source link

Failing sd cards can cause problems for rpi-clone #103

Open pcollinson opened 3 years ago

pcollinson commented 3 years ago

My crontab file in cron.daily used to say

/usr/local/sbin/rpi-clone -q -l mmcblk0

However, failing sd cards can cause kernel errors and these block disk access, and after some days, it's possible to have many instances of rpi-clone running, all blocked on disk IO. The machine continues to run because it doesn't need the sd card, and is fine until it runs out of resources. It can also be blocked during a system upgrade which calls sync, this hangs and the only solution is a reboot, which can leave things in a somewhat unknown state.

My solution is to put a lock into the crontab file, and echo a message which should be sent by email to alert that the sd card has died well before any system upgrade happens. The lock uses a read-only lock on the crontab file, and will go away when rpi-clone finishes.

#!/bin/sh
( flock -n 9 || exit 1
  /usr/local/sbin/rpi-clone -q -l mmcblk0
  exit 0
) 9< /etc/cron.daily/rpi-clone
if [ $? -eq 1 ]; then
    echo "Failed to get lock on rpi-clone - possible problem with mmcblk0"
fi

Of course, you need to ensure that email from cron will get off the machine and into your mailbox - but checking that only one instance of rpi-clone is running is a good thing anyway.