Erstellen eines komplettes Backups der gesamten SD-Karte eines Raspberry Pis. https://strobelstefan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Stefan 097884c24f Quitting Twitter 2 weeks ago
LICENSE first commit 2 years ago
README.md Quitting Twitter 2 weeks ago
README_DE.md Quitting Twitter 2 weeks ago
imagebackup.sh Log File Management 8 months ago
storage-mount.sh Log File Management 8 months ago
storage-umount.sh Log File Management 8 months ago

README.md

imagebackup

🇩🇪 README auf deutsch lesen

What is the goal of "imagebackup"?

Backup

  1. creates via dd a full copy of your Raspberry Pi´s SD card
  2. stores the backup on an external storage device
  3. shrinks the image with Drewsif´s pishrink.sh to avoid any problems with "incorrect" sizes of SD cards ( https://github.com/Drewsif/PiShrink)
  4. zips the shrinked image via gizp to save storage space on your external storage device
  5. removes the now "useless" dd backup and the shrinked image
  6. sends status via e-mail to admin

Restore

The so created image file can be flashed to a new SD card to fully restore your system.

Before you flash the image you have to unzip it.

You can use the flashing tool https://www.balena.io/etcher/

What Raspberry Pi model should I use?

The script can consume a lot of resources of your Raspberry Pi especially for

  • creating backup via dd
  • shrinking via pishrink
  • zipping via gzip

At least a Raspberry Pi model 3 is recommended even better a Raspberry Pi 4. The newest model has more memory power which will speed up the whole process significantly.

Show Raspberry Pi model:

cat /sys/firmware/devicetree/base/model

The load on the CPU is almost nothing. It varies between all four cores and it is between 5% to 25%.

No worries, if you have an older Raspberry Pi you can use the process to create a full backup of your sd card, too. Just skip the PiShrink and gzip process.
Put the dd backup on an external storage and run the other two operations (pishrink & gzip) on a "stronger" hardware, e.g. newer Raspberry Pi, laptop, server, virtual box, etc.

Even this process which is not very handy can be atomized so it is more user friendly. Please have a look at More features that can be easily used with imagebackup to get inspired. 😊

What is required to use "imagebackup"?

  1. Make sure your external storage device can be mounted without any problems. A entry in /etc/fstab is required. Raspberry Pi Image verkleinern mit pishrink
  2. You have to use PiShrink to shrink the dd image file https://github.com/Drewsif/PiShrink
  3. A working Mail Transfer Agent (MTA) like Postfix and an e-mail client like mutt installed on your server.
    Of course you find a detailed manual how to setup both on my website: E-Mail-Versand für den Raspberry Pi konfigurieren

Why multiple shell scripts?

In the new version of imagebackup.sh 3 shell scripts are used.

  • imagebackup.sh
  • storage-mount.sh - Mounts the external storage
  • storage-umount.sh - Umounts the external storage

On a Raspberry Pi several scripts are used for different backup tasks, but each of them mounts a different external storage medium. For mounting and unmounting always the two scripts are used (storage-mount.sh and storage-umount.sh).

The required variables are always passed via the "main script", imagebackup.sh, to the other two scripts (storage-mount.sh & storage-umount.sh ).

Shellskript Variable an anderes Skript übergeben

For more details please refer to
Shellskript - Variablen an andere Skripte übergeben

kalenderbackup, a script to backup the address books and calendars of a Nextcloud, also uses storage-mount.sh and storage-umount.sh.

Installation

  1. Create a directory for your custom scripts on you Pi`s hard drive.
sudo mkdir /etc/scripts
cd /etc/scripts
  1. Clone the PiShrink repository on your local hard drive into the newly created folder /etc/scripts. This command will create a new sub-folder called PiShrink which contains all files of the GitHub repository.
sudo git clone https://github.com/Drewsif/PiShrink.git
  1. Change directory
cd /etc/scripts/PiShrink
  1. Set script permissions for pishrink.sh
sudo chown root:root pishrink.sh
sudo chmod 0600 pishrink.sh
sudo chmod +x pishrink.sh
  1. Clone the imagebackup repository from codeberg.org on your Pi´s hard drive
cd /etc/scripts
sudo git clone https://codeberg.org/strobelstefan.org/imagebackup.git

The repository WON`T get updated automatically. We only copy the current version from Codeberg on the Raspberry Pi´s . Auto update for imagebackup ist not recommended because your variables at the top of the script will get over written.

  1. Adjust the variables at the top of the script according your needs.
 sudo nano /etc/scripts/imagebackup/imagebackup.sh
  1. Adjust permissions for imagebackup.sh
sudo chown root:root imagebackup.sh
sudo chmod 0600 imagebackup.sh
sudo chmod +x imagebackup.sh
  1. Edit /etc/fstab

Make sure you have configured your external storage device in /etc/fstab. This is requred so that the script can mount an umount the device.

Examples

UUID=xxxxxxx /mnt/nextcloud ntfs defaults,noauto,umask=000,users,rw 0 0
UUID=xxxxxxx /mnt/nextcloud exfat defaults,noauto,umask=000,users,rw 0 0
  1. Run script - Test run

The runtime of the script can be very very very long. So it is highly recommended to use screen to avoid any interruptions.

cd /etc/scripts
screen # This is optional
sudo ./imagebackup.sh
  1. Setup a cron job

To execute imagebackup.sh via Cron follow this step.

# imagebackup
# At 01:00 on Wednesday
0 01 * * 3 /bin/bash /etc/scripts/imagebackup/imagebackup.sh
# At 01:00 on Sunday.
0 01 * * 0 /bin/bash /etc/scripts/imagebackup/imagebackup.sh

If you would like to call the script on another time maybe crontab guru is helpful: https://crontab.guru

Logrotate

The script creates the log file in /var/log directory. Once the file exists, the logs will continuously written into the file. This can cause the file to become very large and waste disk space.

To avoid manually deleting the file, a small logrotate script can be created to manage the log file.

Show installed Logrotate version:

logrotate --version

Install logrotate

sudo apt install logrotate

A new file must be created in the directory /etc/logrotate.d:

sudo nano /etc/logrotate.d/imagebackup

The contents of the file may look like this:

/var/log/imagebackup.log {
        rotate 4
        daily
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        endscript
}

For more info on logrotate see the man logrotate.

More features that can be easily used with imagebackup

There are many other ways to store your exports on another physically separated devices. Here are some examples:


Follow Me

🐘 MastodonRSS


Kaffeespende

Du findest meine Vorlage hilfreich, dann kannst du mir gerne einen Kaffee ausgeben.

😊 😎 bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj 😄

Do you find the template helpful, then feel free to buy me a coffee.


Disclaimer

EN

The working methods described here are based on my personal experience and my personal thought processes. I do not guarantee the results; trying out and implementing the described procedures is entirely at your own risk and responsibility.

Therefore, please make a backup before making any changes to your system or configuration so that you can restore it if the worst comes to the worst.

DE

Die hier geschilderten Arbeitsweisen basieren auf meiner persönlichen Erfahrung und meinen persönlichen Gedankengängen. Ich übernehme keine Garantie für die Resultate; Ausprobieren und Implementieren der beschriebenen Vorgehensweisen erfolgt ausschließlich auf eigene Verantwortung und Gefahr.

Bitte erstellt deshalb vor jeder Änderung an eurem System oder Konfiguration ein Backup, damit ihr im Fall der Fälle eine Wiederherstellung durchführen könnt.