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 86bf4b3605 Added Git Clone https Address 3 months ago
LICENSE first commit 4 months ago
README.md Added Git Clone https Address 3 months ago
README_DE.md Added Git Clone https Address 3 months ago
imagebackup.sh Typos 4 months ago

README.md

imagebackup

🇩🇪 README auf deutsch lesen

What is the purpose of "imagebackup"?

The script

  1. creates via dda full copy of you Raspberry Pi´s SD card as a complete backup
  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. zip shrinked image via gizp to save storage space on external storage device
  5. removes the now "useless" ddbackup and the shrinked image
  6. sends status via e-mail to admin

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 required even better a Raspberry Pi 4. The newest model has more memory power which will speed up the whole process significantly.

The script and a running Nextcloud (no logged in users) consumes the following memory of a Raspberry Pi:

Mem ~ 350 to 400 M of 3.74G

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 Pi, too. Just skip the shrink and zip process on it. Put the ddbackup 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. Firstly, you have to have a working Mail Transfer Agent (MTA) like Postfix and an e-mail client like mutt installed on you server. Of course you find a detailed manual how to setup both on my website: 🔗 E-Mail-Versand für den Raspberry Pi konfigurieren

  2. Secondly, make sure your external storage device can be mounted without any problems 🔗 Raspberry Pi Image verkleinern mit pishrink

  3. Thirdly, you have to use PiShrink 🔗 https://github.com/Drewsif/PiShrink

Can the whole process get automatized?

Of course, you can automatize the hole process by setting up a cron job to call imagebackup.sh.

Are there any security issues?

To be honest, there can be some. But the issues are not really related to the script itself it is more related to the script permissions and you server admins.

The script requires some high permissions,

  • root permissions to mount external storage devices and
  • root permissions to call imagebackup

To achieve both that we have to use sudo crontab -e.

The high risk is therefore, if an user gains write access to the script, he/she can execute unauthorized commands though the crontab.

Please double check

  • the permissions of the script
  • users who can use sudo or root on your system are setup correctly
  • that personalized accounts for your admins are in use

If you are interested in securing your system in a good way with hardware tokens and certificates, please have a close look at the YubiKey category on my website

🔗 https://strobelstefan.org/yubikey/

Despite the concerns mention above you have to trust

  • PiShrink

because the script gets updated automatically by imagebackup.sh. If you have an uneasy feeling about it uncomment the auto-upgrade function in the script. So you can check the script manually before you upgrade it.

Install

I use gitto clone the whole PiShrink repository from GitHub in my local hard drive to easily update the script via script. All my scripts are located in one folder and organized in sub-folders which makes it very easy to maintain them.

  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. This command will create a new sub-folder called PiShrink which contains all files of the GitHub repository.
sudo clone https://github.com/Drewsif/PiShrink.git
  1. Change directory
/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. Copy the imagebackup.sh on you local hard drive
cd /etc/scripts
  1. Clone the imagebackup repository from codeberg.org on your Pi´s hard drive
sudo 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
# Storage device as defined in your /etc/fstab.
mountpoint='/mnt/usbbackup/'

# Path were the image of your SD card should be saved to
STORAGEPATH="/mnt/usbbackup/nextcloud/images"

# Location of Nextcloud Installation
NEXTCLOUDINSTALLATION="/var/www/html/nextcloud"

# Location of PiShrink script pishrink.sh
PISHRINK="/etc/scripts/PiShrink"

# Location of imagebackup
IMAGEBACKUP="/etc/scripts/imagebackup"

# Location of image backup script
IMAGESCRIPT="/etc/scripts/imagebackup/imagebackup.sh"

# Image name
IMAGENAME="nextcloud"

# E-Mail Address For Messages
EMAIL="mail@e-mail.de"

# E-Mail Address for sending out backup script
EMAILIMAGESCRIPT="mail@e-mail.de"

#Log File location and name
LOGFILE="/var/log/imagebackup.log"
  1. Adjust permissions for imagebackup.sh
sudo chown root:root imagebackup.sh
sudo chmod 0600 imagebackup.sh
sudo chmod +x imagebackup.sh
  1. Run script

    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
# 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

More features that can be easily used with imagebackup

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


Donation

Do you find the templates helpful, then feel free to buy me a coffee Donate 😊 Donate 😎

Du findest meine Vorlagen hilfreich, dann kannst du mir gerne einen Kaffee ausgeben Donate 😊 Donate 😎


You can find more exciting articles on IT topics on my website www.strobelstefan.org

There you can find more articles about Nextcloud and the popular Raspberry Pi:


⚠️ 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.