Created
November 1, 2022 17:05
-
-
Save ip75/664049f6163ae2dfd5561921d9d851da to your computer and use it in GitHub Desktop.
periodic script to make backup of system on flash drive FreeBSD 13. Scan HDD in system and send SMART to root
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh - | |
# | |
# $FreeBSD$ | |
# | |
if [ -r /etc/defaults/periodic.conf ]; then | |
. /etc/defaults/periodic.conf | |
source_periodic_confs | |
fi | |
rc=0 | |
send_smart_info() { | |
devices=$(geom disk list| grep -E "Geom name: ada." | sed "s/Geom name: //g" | xargs) | |
for disk in ${devices}; do | |
report=$(smartctl -x /dev/$disk) | |
echo -e "Subject: $disk smart info\n$report" | sendmail root | |
done | |
} | |
backup_system() { | |
SNAPSHOT_DT="$(date +%Y-%m-%dT%T)" | |
SNAPSHOT_NAME=zsys@$SNAPSHOT_DT | |
BACKUP_DIRECTORY=/var/EMERGENCY-SYSTEM/root/backups | |
echo -e "Create snapshot backup zsys@\033[1m$SNAPSHOT_NAME.ZFS.xz\033[0m..." | |
zfs snapshot -r $SNAPSHOT_NAME | |
snapshot_list="$(zfs list -o name -Ht snapshot | xargs)" | |
for snapshot in ${snapshot_list}; do | |
echo $snapshot | grep -q -E '^.*(tmp|ports|obj|src|log).*$' | |
if [ $? -eq 0 ]; then | |
echo -e "skip \033[1m$snapshot\033[0m" | |
else | |
echo -e "backup \033[1m$snapshot\033[0m" | |
zfs send -v $snapshot | xz > $BACKUP_DIRECTORY/$(echo $snapshot|sed -r 's/\//+/g').ZFS.xz | |
fi | |
done | |
zfs destroy -r $SNAPSHOT_NAME | |
# get list of snapshots | |
#zfs list -t snapshot | |
# boot from usb stick, then create pool and then restore to newly created pool this backup | |
#xz -cdk $SNAPSHOT_NAME.xz | zfs receive -sv zsys | |
# Unable to make snapshot on worked bootable system pool | |
# cannot receive: cannot specify snapshot name for multi-snapshot stream | |
# | |
# create tar backup | |
echo -e "Create tar backup \033[1m$SNAPSHOT_NAME.tar.xz\033[0m..." | |
tar cJpvf $BACKUP_DIRECTORY/$SNAPSHOT_NAME.tar.xz --exclude=/data --exclude=/var/EMERGENCY-SYSTEM --exclude=/usr/ports --exclude=/usr/src --exclude=/usr/obj --exclude=/tmp --exclude=/var/tmp / | |
# find necessary files | |
#tar ztpvf $SNAPSHOT_NAME.xz --include etc | |
# delete files older then 90 days | |
find $BACKUP_DIRECTORY/ -mtime +90d -exec rm {} \; | |
return $rc | |
} | |
case "${monthly_system_backup_enable:-NO}" in | |
[Yy][Ee][Ss]) | |
send_smart_info | |
backup_system | |
rc=$? | |
;; | |
*) | |
rc=0 | |
;; | |
esac | |
exit $rc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment