Das Skript imagebackup.sh
ermöglicht es dir automatisiert und in regelmäßigen Abständen ein komplettes Image der SD-Karte deines Raspberry Pis als Backup auf ein externes Speichermedium zu schreiben. Ich nutze das für meine beiden Pis auf denen eine Nextcloud und Pi-hole läuft.
imagebackup nun auf codeberg.org
Nachdem ich vor kurzem das Skript zur automatischen auf Codeberg für die bessere Verwaltung geladen habe, habe ich mich auch dazu entschieden das Gleiche für das imagebackup-Skript zu machen.
Raspberry Pi SD-Karte im Livebetrieb klonen – Neue Version
In meinem Beitrag ➡ Raspberry Pi SD-Karte im Livebetrieb klonen habe ich ein Skript erstellt, dass ein komplettes Image der SD-Karte eines Raspberry Pis im laufenden Betrieb erstellt. Das Skript habe ich ein wenig optimiert und angepasst.
Es werden für das neue Skript zwei Tools benötigt:
- Mutt – The Mutt Mail User Agent
- PiShrink
Das erste Tool ist in den Repositories enthalten und kann ganz einfach installiert werden:
sudo aptitude install mutt
Das zweite Tool ist auf GitHub verfügbar und kann von dort heruntergeladen werden. Zu dem Tool habe ich bereits einen Artikel geschrieben
➡ Raspberry Pi Image verkleinern
Das neue Skript verwendet das Tool „PiShrink„, um das mit „dd“ erstellt Image zu verkleinern. Damit wird weniger Speicherplatz im Backup-Archiv verwendet und ihr könnt sehr schnell das Image bei Bedarf auf andere SD-Karten (andere Größe, anderer Hersteller) ohne großen Aufwand schreiben . Durch das Verkleinern des Image mit dem neuen Skript verkürzt sich dadurch hoffentlich die Wiederherstellungszeit und damit die Ausfallzeit eures Systems im Notfall.
Gib mir gerne einen Kaffee ☕ aus!
Wenn dir meine Beiträge gefallen und geholfen haben, dann kannst du mir gerne einen Kaffee ☕ ausgeben.
bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj
Zudem sollte beachtet werden, dass das neue Skript mehr Ressourcen durch PiShrink benötigt. Auf älteren Raspberry Pi-Versionen kann das ggf. zu einigen Problemen führen!
Ich verwende das Skript auf einem Raspberry Pi 4 mit 4GB RAM. Der externe Speicher ist an den USB-3.0-Anschluss angeschlossen, wodurch die Schreibgeschwindigkeit ebenfalls erhöht wird.
Die Laufzeit des gesamten Skripts, für die Erstellung des Images und dessen Verkleinerung beträgt trotzdem fas 2 Stunden.
#!/bin/bash # by strobelstefan.org # 2019-10-23 # Version: 2.0 # https://strobelstefan.de/?p=5985 # # This script creates a full clone of your Raspberry Pi´s SD card. # ################################### # Define Variables ################################### # Storage device as defined in your /etc/fstab. mountpoint='/mnt/backup/' # Path were the image of your SD card should be saved to STORAGEPATH="/mnt/backup/imagebackup" # Location of Nextcloud Installation NEXTCLOUDINSTALLATION="/var/www/html/nextcloud" # E-Mail Address EMAIL="mail@email.de" # Image name IMAGENAME="rpi" #Log File location and name LOGFILE="/var/log/"$HOSTNAME_${IMAGENAME}_"backup-image.log" ################################### # This removes your old log file ################################### # This removes your old log file. # When you run the script the first time an error will be displayed, # because no log file is in the defined path. rm ${LOGFILE} ################################### # MOUNTPOINT Section - Check Mount point Availability ################################### # It checks if your mount point is accessible by your RPi. # This is a crucial step, if the storage is not available the clone process of the SD card cannot conducted. # Process # 1. Check if mount point is accessible # 2. If YES go to DELETION Section # 3.1 If NO, try to mount storage device as defined in /etc/fstab # 3.2 If mount is again not successful exit script, no further action will be conducted if [ "$(findmnt ${mountpoint})" ] ; then echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount point accessible by your "$HOSTNAME >> ${LOGFILE} else echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount point was not accessible, try to mount it now as defined in your /etc/fstab" >> ${LOGFILE} #This command mounts all storages defined in /etc/fstab mount -a if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount of storage in first try successfully completed" >> ${LOGFILE} sleep 5 mount -a if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Backup FAILED! Was not able to mount your storage device. Stop backup process. You have to check it manually." >> ${LOGFILE} echo "Sent backup status via e-mail" | mutt ${EMAIL} -a ${LOGFILE} -s $HOSTNAME" - Backup FAILED" >> ${LOGFILE} exit fi fi fi ################################################################## # DELETION Section - Remove old Images from Storage Device ################################################################## # Use this command with CAUTION! # This will help you to automatically remove old images from your storage device to avoid that your # storage will run out of disk space echo $(date +%Y-%m-%d_%H-%M-%S) " - Start to delete files older than defined time " >> ${LOGFILE} # Uncomment if the files should be identified by days, file > 30 days than it gets deleted #find ${STORAGEPATH}/*.* -mtime +30 -exec rm -r {} \; # Uncomment if you would like to use minutes file > 10080 minutes than it gets deleted find ${STORAGEPATH}/*.* -type f -mmin +43200 -exec rm {} \; if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Deletion of old image files successfully completed" >> ${LOGFILE} if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Was not able to delete old image files. You have to check it manually." >> ${LOGFILE} break fi fi ################################### # Nextcloud Maintenance Mode ON ################################### #Aktiviere den Maintenancemode für Nextcloud - Keine Useranmeldung mehr möglich! #Wechsel in das Nexcloud-Verzeichnis cd ${NEXTCLOUDINSTALLATION} sudo -u www-data php occ maintenance:mode --on echo $(date +%Y-%m-%d_%H-%M-%S) " - Nextcloud maintenance mode ON" >> ${LOGFILE} ################################### # CLONE Section - Clone SD Card Image ################################### # This line creates a full copy of the SD card and writes it as an image file to the defined patch echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to clone image" >> ${LOGFILE} # Saves a plain img file on your storage device sudo dd if=/dev/mmcblk0 of=${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d).img bs=1MB echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to clone image" >> ${LOGFILE} ################################### # Nextcloud Maintenance Mode OFF ################################### #Entferne den Maintenancemode für Nextcloud - Useranmeldungen sind wieder möglich! #Wechsel in das Nexcloud-Verzeichnis cd ${NEXTCLOUDINSTALLATION} sudo -u www-data php occ maintenance:mode --off echo $(date +%Y-%m-%d_%H-%M-%S) " - Nextcloud maintenance mode OFF" >> ${LOGFILE} ################################### # Resize dd Image ################################### # Resize image with pishrink # Please see https://github.com/Drewsif/PiShrink for further details # pishrink.sh must be located in the same directory as this script! echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to resize image" >> ${LOGFILE} sudo /bin/bash /etc/scripts/pishrink.sh -d ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d).img ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d)-small.img echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to resize big image" >> ${LOGFILE} #Delete big image file echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to delete big image" >> ${LOGFILE} sudo rm ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d).img echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to delete big image" >> ${LOGFILE} # Creates a compressed file of the resized image # This command will create a compressed gz archive of the small image file. # The small file will get deleted during the process if you would like to keep # the small image file use the command gzip -k ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d)-small.img # Before you change the compression process check your disk space size echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to compress small image" >> ${LOGFILE} gzip -q ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d)-small.img echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to compress small image" >> ${LOGFILE} if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Image file created" >> ${LOGFILE} if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Was not able to create your image file. You have to check it manually." >> ${LOGFILE} break fi fi ################################### # UMOUNT Section - Unmount Storage Device ################################### # This command unmounts the defined storage device. # In the first try it will gently try to unmount, if the device is busy the command will force the unmount. if [ "$(umount ${mountpoint})" ] ; then echo $(date +%Y-%m-%d_%H-%M-%S) " - Umounted your storage device" >> ${LOGFILE} else echo $(date +%Y-%m-%d_%H-%M-%S) " - Umount of storage device was not possible in first run, wait for 30 seconds" >> ${LOGFILE} sleep 30 if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Umount was successful" >> ${LOGFILE} sleep 5 umount ${mountpoint} if [ $? != 0 ] then echo $(date +%Y-%m-%d_%H-%M-%S) " - Umount successful" >> ${LOGFILE} exit fi fi fi # Skript finished echo $(date +%Y-%m-%d_%H-%M-%S) " - Mission Accomplished!!! System is going for a reboot." >> ${LOGFILE} # Send status via e-mail echo "Sent backup status via e-mail" | mutt ${EMAIL} -a ${LOGFILE} pishrink.log -s $HOSTNAME" - Backup SUCCESSFULL" >> ${LOGFILE} #Optional # If you like to reboot your system, please uncomment #sudo reboot
Nun das Skript noch ausführbar machen
sudo chmod +x skript.sh
Zu automatischen Sicherung ist noch ein Crontab zu erstellen:
sudo crontab -e
Dort tragt ihr die Zeile ein, um das Skript zu einem bestimmten Zeitpunkt zu starten.
0 23 1 * * /bin/bash /home/pi/skripte/skript.sh
Photo by Denise Jans on Unsplash
Raspberry Pi Image verkleinern mit pishrink
Ich hatte kürzlich das Problem, dass auf einem meiner Raspberry Pis die SD-Karte ausgetauscht werden musste. Ich habe dort eine 32GB-Karte von Samsung im Einsatz gehabt. Bei einem Onlinehändler habe ich mir ebenfalls eine 32GB-Karte jedoch von SanDisk gekauft.
Glücklicherweise habe ich für den Raspberry Pi ein Backup eingestellt gehabt, dass ein komplettes Image der SD-Karte erstellt und auf einen externen Datenträger schreibt.
Die Anleitung dafür gibt es hier
➡ Raspberry Pi SD-Karte im Livebetrieb klonen.
Das so erstellte Image wollte ich mit dem Tool Etcher ( ➡ https://www.balena.io/etcher/) auf die neue Karte schreiben lassen. Jedoch gab es dabei eine unschöne Überraschung. Obwohl auf beiden SD-Karten 32GB aufgedruckt war, war nicht 32GB drin. Bei der neuen Karte meldete Etcher, dass Speicherplatz fehlt und deshalb das Image nicht auf die Karte geschrieben werden kann. Es galt also das Image zu verkleinern, um es auf die neue SD-Karte zu bekommen.
Es gibt dafür das recht cooles kleine Skript „pishrink“ von Drewsif auf github.com, dass diese Aufgabe übernimmt.
➡ https://github.com/Drewsif/PiShrink.
Die Bedienung ist sehr einfach gehalten und das Ganze funktioniert ohne Probleme.
Ich habe für das Shrinken einen Raspberry Pi 4 verwendet, da er mehr Leistung und über USB 3.0-Ports verfügt als der alte Raspberry Pi 3. Es geht auch ein älterer Pi, jedoch benötigt dieser für die Aufgabe wesentlich mehr Zeit.
Die Funktionsweise des Skripts ist schnell erklärt:
- Original-Image wird ausgewählt
- pishrink erstellt eine exakte Kopie des Original-Images
- pishrink verkleinert die Kopie
- Original-Image und Kopie werden im gleichen Ordner belassen
Es ist also bei diesem ganzen Prozess unbedingt darauf zu achten, dass eine ausreichend große Festplatte verwendet wird, auf der beide Image-Dateien Platz finden.
Hier ein Screenshot, der das besser veranschaulicht:
Ich habe mir das Skript in das Verzeichnis „/etc/skripte“ geladen.
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh sudo mv pishrink.sh /etc/skripte/
Im nächsten Schritt habe ich das Skript ausgeführt
sudo /bin/bash pishrink.sh nextcloud_2019-10-19.img nextcloud_2019-10-19_klein.img
Im obigen Screenshot ist schön zu sehen, was genau passiert und die einzelnen Schritte sind gut nachvollziehbar.
Im nächsten Schritt werde ich versuchen „pishrink.sh“ sinnvoll in beim Backup-Skript ( ➡ Raspberry Pi SD-Karte im Livebetrieb klonen)zu integrieren. Wahrscheinlich ist das nur sinnvoll bei einem Raspberry Pi 4, da sonst ggf. ältere Pis mit geringerem RAM swappen und dadurch die SD-Karte in Mitleidenschaft gezogen wird.
Ich habe das Image auf einer externen Festplatte gespeichert und diese an den RPi angebunden.
Hier noch ganz kurz, wenn ihr eine Festplatte direkt per /etc/fstab einhängen wollt.
UUID der Festplatte identifizieren
sudo blkid
Mount Piont erstellen
/mnt/pishrink
/etc/fstab editieren
sudo nano /etc/fstab
Die externe Festplatte ist im NTFS-Format formatiert.
Damit das Speichermedium schreibend eingebunden werden kann, wird das Paket „ntfs-3g“ benötigt.
Installiert wird das mit
sudo aptitude install ntfs-3g
Die folgende Zeile am Ende der Datei einfügen und die UUID anpassen.
# wird nur für das verkleinern eines Image eingebunden. UUID=xyxyxyxyxy /mnt/pishrink ntfs defaults,noauto,umask=000,users,rw 0 0
Festplatte einhängen
sudo mount /mnt/pishrink
Das wars, nun könnt ihr mit „pishrink“ das Image auf der Festplatte verkleinern und anschließend auf die neue SD-Karte schreiben.
Gib mir gerne einen Kaffee ☕ aus!
Wenn dir meine Beiträge gefallen und geholfen haben, dann kannst du mir gerne einen Kaffee ☕ ausgeben.
bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj
Photo by Denise Jans on Unsplash