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.

385 lines
12 KiB

#!/bin/bash
# by strobelstefan.org
# 2021-06-01
# Version: 3.0
# https://strobelstefan.org/2019/10/21/raspberry-pi-sd-karte-im-livebetrieb-klonen-neue-version/
#
# 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/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"
###################################
# This removes your old log file
###################################
# This removes your old log file and creates a new one
rm -f ${LOGFILE}
touch ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Created New Log File ${LOGFILE}" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Update imagebackup script
###################################
# This update functions only work, when you use your own Git repository!
# If you use the official imagebackup repository your defined variables will be over written
# every time you update the repo and you have to adjust the manuals manually
# cd ${IMAGEBACKUP}
# sudo git clone
# Set the correct permissions for the script
# sudo chown root:root imagebackup.sh
# sudo chmod 0600 imagebackup.sh
# sudo chmod +x imagebackup.sh
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# echo $(date +%Y-%m-%d_%H-%M-%S) " - Check for imagebackup Updates" >> ${LOGFILE}
#cd ${IMAGEBACKUP} >> ${LOGFILE}
#sudo git pull >> ${LOGFILE}
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# echo >> ${LOGFILE}
# echo >> ${LOGFILE}
###################################
# Update PiShrink script
###################################
# This update functions only works, when you have cloned the GitHub repository on your Raspberry Pi
# cd ${PISHRINK}
# sudo git clone https://github.com/Drewsif/PiShrink.git
# Set the correct permissions for the script
# sudo chown root:root pishrink.sh
# sudo chmod 0600 pishrink.sh
# sudo chmod +x pishrink.sh
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Check for PiShrink Updates" >> ${LOGFILE}
###################################
# Update PiShrink Git Repo via git pull
###################################
cd ${PISHRINK} >> ${LOGFILE}
sudo git pull >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${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
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
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
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
##################################################################
# 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 "++++++++++++++++++++++++++" >> ${LOGFILE}
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 +14400 -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
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Nextcloud Maintenance Mode ON
###################################
# Activates Maintenance mode for Nextcloud
# ATTENTION:
# When Maintenance mode ist active! No user can login to the system!
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# Change directory
cd ${NEXTCLOUDINSTALLATION} >> ${LOGFILE}
sudo -u www-data php occ maintenance:mode --on >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Nextcloud maintenance mode ON" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${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 "++++++++++++++++++++++++++" >> ${LOGFILE}
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}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Nextcloud Maintenance Mode OFF
###################################
# Deactivates Maintenance mode for Nextcloud
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# Change directory
cd ${NEXTCLOUDINSTALLATION} >> ${LOGFILE}
sudo -u www-data php occ maintenance:mode --off >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Nextcloud maintenance mode OFF" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Resize Image
###################################
# Resize image with PiShrink
# Please see https://github.com/Drewsif/PiShrink for further details
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to resize image" >> ${LOGFILE}
sudo /bin/bash ${PISHRINKSCRIPT}/pishrink.sh -d ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d).img ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d)-small.img >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to resize big image" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Delete big image file
###################################
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to delete big image" >> ${LOGFILE}
sudo rm ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d).img >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to delete big image" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${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 "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to compress small image" >> ${LOGFILE}
gzip -q ${STORAGEPATH}/${IMAGENAME}_$(date +%Y-%m-%d)-small.img >> ${LOGFILE}
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
echo $(date +%Y-%m-%d_%H-%M-%S) " - Finished to delete big image" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Umount Storage Device
###################################
# This command umounts the defined storage device.
# In the first try it will gently try to umount,
# if the device is busy the command will force the umount.
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
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
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Script Finished
###################################
echo $(date +%Y-%m-%d_%H-%M-%S) " - Mission Accomplished!!! System is going for a reboot." >> ${LOGFILE}
###################################
# Sends Backup Script To defined E-Mail Address
###################################
echo $(date +%Y-%m-%d_%H-%M-%S) " - Sent Backup Script to ${EMAILIMAGESCRIPT}" | mutt ${EMAILIMAGESCRIPT} -a ${IMAGESCRIPT} -s "NEXTCLOUD Pi - Image Backup Script" >> ${LOGFILE}
###################################
# Sends Log File To Defined E-Mail Address
###################################
echo "Sent backup status via e-mail" | mutt ${EMAIL} -a ${LOGFILE} pishrink.log -s $HOSTNAME" - Backup SUCCESSFULL" >> ${LOGFILE}
###################################
# System Reboot
###################################
sudo reboot