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.

260 lines
7.3 KiB

#!/usr/bin/env bash
# Copyright by Stefan Strobel
# https://www.strobelstefan.de
# Hosted on https://codeberg.org/strobelstefan.org/
# If you like this script, feel free to buy me a coffee.
# PayPal https://www.paypal.com/donate?hosted_button_id=BNV5XKAAXK6TJ
# Bitcoin - bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj
# This script creates an image of your Raspberry Pi's SD card.
# The script uses PiShrink to shrink the image to save space on your storage device
# and to make it easier to flash the image on a new SD card.
# The size of SD cards can vary based on the manufacturer and lot.
###################################
# Variables
###################################
# Script name
MAINSCRIPT=$(basename -- "$0")
# PWD of script
SCRIPTPWD="/etc/scripts/nextcloudpi/"
# External storage device as defined in your /etc/fstab
MOUNTPOINT="/mnt/usbbackup/"
# Path were the dd image of your SD card on ${MOUNTPOINT} should be saved to
STORAGEPATH="${MOUNTPOINT}nextcloud/images/"
# Location of your Nextcloud Installation
# This is the standard path of a Nextcloud installation
NEXTCLOUDINSTALLATION="/var/www/html/nextcloud/"
# PiShrink Path
PISHRINKPATH="/etc/scripts/PiShrink/"
# Name of PiShrink Script
# Default: pishrink.sh
PISHRINK="${PISHRINKPATH}pishrink.sh"
# dd Image name
IMAGENAME="nextcloud"
# E-Mail Address
EMAIL="mail@e-mail.de"
# Log File location and name
LOGFILE="/var/log/imagebackup.log"
exec 1> ${LOGFILE}
###################################
# Log File
###################################
if test -f "${LOGFILE}"
then
echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Start ${MAINSCRIPT} on $(hostname)" >&1
echo "++++++++++++++++++++++++++" >&1
echo "" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File already exists!" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File location: ${LOGFILE}" >&1
else
touch ${LOGFILE}
echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Start ${MAINSCRIPT} on $(hostname)" >&1
echo "++++++++++++++++++++++++++" >&1
echo "" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File newly created!" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File location: ${LOGFILE}" >&1
fi
###################################
# Update PiShrink Script
###################################
# This update functions only works, when you have cloned the official GitHub repository on your server
# 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 "PiShrink Update - git pull" >&1
# echo "--------------------------" >&1
# echo "$(date +%Y-%m-%d_%H-%M-%S) - Check for PiShrink Updates" >&1
#cd ${PISHRINKPATH} >&1
#git pull >&1
echo "" >&1
###################################
# MOUNTPOINT Section - Check Mount point Availability
###################################
# Calls the script storage-mount.sh
# and passes relevant variables
/bin/bash ${SCRIPTPWD}storage-mount.sh "${MOUNTPOINT}" "${EMAIL}" "${LOGFILE}" "${MAINSCRIPT}" >&1
if [ $? != 0 ]
then
exit 1
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 before you start the backup process
echo "Delete old images" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Start" >&1
# 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 > 14400 minutes than it gets deleted
find ${STORAGEPATH}/*.* -type f -mmin +14400 -exec rm {} \;
echo "$(date +%Y-%m-%d_%H-%M-%S) - End" >&1
echo "" >&1
###################################
# Nextcloud Maintenance Mode ON
###################################
echo "Nextcloud" >&1
echo "--------------------------" >&1
sudo -u www-data php ${NEXTCLOUDINSTALLATION}occ maintenance:mode --on >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Nextcloud maintenance mode ON" >&1
echo "" >&1
###################################
# 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 "dd" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - dd - Start" >&1
# Saves a plain image 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) - dd - End" >&1
echo "" >&1
###################################
# Nextcloud Maintenance Mode OFF
###################################
echo "Nextcloud" >&1
echo "--------------------------" >&1
sudo -u www-data php ${NEXTCLOUDINSTALLATION}occ maintenance:mode --off >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Nextcloud maintenance mode OFF" >&1
echo "" >&1
###################################
# PiShrink - Resize Image
###################################
# Resize image with PiShrink
# Please see https://github.com/Drewsif/PiShrink for further details
echo "PiShrink" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - PiShrink - Start" >&1
sudo /bin/bash ${PISHRINK} ${STORAGEPATH}${IMAGENAME}_$(date +%Y-%m-%d).img ${STORAGEPATH}${IMAGENAME}_$(date +%Y-%m-%d)-small.img >&1
echo "" >&1
if [ $? -eq 0 ]
then
echo "rm - Delete big image file" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - rm Start" >&1
rm ${STORAGEPATH}${IMAGENAME}_$(date +%Y-%m-%d).img >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - rm End" >&1
echo "" >&1
echo "gzip" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - gzip - Start" >&1
gzip -q ${STORAGEPATH}${IMAGENAME}_$(date +%Y-%m-%d)-small.img >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - gzip - End" >&1
else
echo "$(date +%Y-%m-%d_%H-%M-%S) - PiShrink Failed" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - PiShrink Failed! Sent email to ${EMAIL}" | mutt ${EMAIL} -a ${LOGFILE} -s "$(hostname) - PiShrink Failed! Check your backup manually!" >&1
exit 2
fi
echo "$(date +%Y-%m-%d_%H-%M-%S) - PiShrink - End" >&1
echo "" >&1
###################################
# UMOUNT Section - Unmount Storage Device
###################################
# Calls the script storage-umount.sh
# and passes relevant variables
/bin/bash ${SCRIPTPWD}storage-umount.sh "${MOUNTPOINT}" "${EMAIL}" "${LOGFILE}" "${MAINSCRIPT}" >&1
###################################
# Sends Backup Script To defined E-Mail Address ${EMAIL}
###################################
echo "Sent E-Mail" >&1
echo "--------------------------" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Imagebackup Finished - Sent notification to ${EMAIL}" | mutt ${EMAIL} -a ${LOGFILE} -s "$(hostname) - Imagebackup Finished" >&1
echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - End ${MAINSCRIPT} on $(hostname)" >&1
echo "++++++++++++++++++++++++++" >&1
echo "" >&1
echo "" >&1
###################################
# System Reboot
###################################
#sudo reboot
exit 0