This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/app/bin/home/backup

64 lines
3.1 KiB
Bash
Executable file

#!/bin/bash
# TODO: Rewrite me, maybe? <3
source /var/roothome/.config/bash/module/notify.sh
source /var/roothome/.config/bash/module/checksum.sh
source /var/roothome/.config/bash/module/recursive.sh
dow=$(/usr/bin/date +%u) # current day of week number.
date=$(/usr/bin/date +%Y%m%d%H%M) # date stamp.
path_src="/var/mnt/storage/hot" # backup src location.
path_media="/var/mnt/storage/cold_1/media /var/mnt/storage/cold_2/media" # media paths.
path_mount="/var/mnt/storage/cold_1" # backup mount location.
path_backup="${path_mount}/backup" # backup dir location.
path_docker="${path_backup}/home" # docker backup location.
# Check if backup drive is mounted.
if [ ! -f "${path_mount}"/.mount ]; then
echo "Backup : ${path_mount} not mounted!"
notify "Backup : ${path_mount} not mounted!"
exit 1
fi
# Check if hot storage is mounted.
if [ ! -f "${path_src}"/.mount ]; then
echo "Backup : ${path_src} not mounted!"
notify "Backup : ${path_src} not mounted!"
exit 1
fi
# Tweak bash.
shopt -s globstar
shopt -s dotglob
# Cd to src storage.
builtin cd "${path_src}"
# Save media list.
/usr/bin/find ${path_media} -type d | gzip -1 > ${path_backup}/tmp/ColdMedia.txt.gz || notify 'Backup : Failed to save media list!'
# Backup docker.
rm ${path_docker}/${dow}_*.tgz
/usr/bin/tar --exclude-from=docker/.tarignore -c docker | /usr/bin/gzip -1 > ${path_docker}/docker.tgz && mv ${path_docker}/docker.tgz ${path_docker}/${dow}_${date}-$(/usr/bin/sha1sum ${path_docker}/docker.tgz | cut -d\ -f1).tgz || notify 'Backup : Failed to save docker!'
# Update checksums.
cd ${path_src}/media/book/ && recursive checksum_create || notify 'Backup : Checksum book failed!'
cd ${path_src}/media/music/ && recursive checksum_create || notify 'Backup : Checksum music failed!'
cd ${path_src}/media/paper/ && recursive checksum_create || notify 'Backup : Checksum paper failed!'
# Copy tmp media.
/usr/bin/rsync -a --delete ${path_src}/media/book/ ${path_backup}/tmp/book/ || notify 'Backup : Failed to save books!'
/usr/bin/rsync -a --delete ${path_src}/media/music/ ${path_backup}/tmp/music/ || notify 'Backup : Failed to save music!'
/usr/bin/rsync -a --delete ${path_src}/media/paper/ ${path_backup}/tmp/paper/ || notify 'Backup : Failed to save paper!'
/usr/bin/rsync -a --delete ${path_src}/media/porn/ ${path_backup}/tmp/porn/ || notify 'Backup : Failed to save porn!'
/usr/bin/rsync -a --delete /var/mnt/storage/cold_2/media/youtube/best/ ${path_backup}/tmp/youtube/ || notify 'Backup : Failed to save youtube!'
# Upload external.
#/usr/bin/rsync -ahP -e 'ssh -p 143 -i /var/roothome/document/key/ssh/pi.key' /var/mnt/backup/ root@192.168.1.6:/var/mnt/backup/ || notify 'backup : failed upload'
# Notify completion & size.
bupsize=$(/usr/bin/du -h ${path_docker}/${dow}_*.tgz | awk '{print $1}')
notify_silent "Backup : Complete ${bupsize}."
echo "Backup : Complete ${bupsize}."