Backup : Use Archive module.

This commit is contained in:
Dmitry Voronin 2024-01-03 18:39:38 +03:00
parent b5c1603413
commit c2961c4fd7
2 changed files with 65 additions and 91 deletions

View file

@ -105,7 +105,7 @@ function archive_fast() {
function archive_check() {
local IFS=$'\n'
local targets=(${@})
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
process() {
# extract hash from name.
@ -122,64 +122,39 @@ function archive_check() {
_iterate_targets process ${targets[@]}
}
# Delete old versions of archives.
# All archives by default.
# Usage: archive_prune [NAMES]
# Delete old versions of an archive.
# All archives with 1 version by default.
# Usage: archive_prune [NAME] [VERSIONS]
function archive_prune() {
local IFS=$'\n'
local targets=()
for target in ${@}; do
targets+=($(ls ${target}* | grep -E ${_archive_pattern})) || return 2
done
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
local targets=(${1})
local versions=${2}
[[ "${targets}" = "" ]] && targets=($(_archive_names))
[[ "${versions}" = "" ]] && versions=1
if [[ ${#} -gt 2 ]]; then
help archive_prune
return 2
fi
process() {
local data=($(_archive_parse ${target}))
local name=${data[0]}
local time=${data[1]}
local copies=($(ls ${name}* | grep -E ${_archive_pattern}))
local prune=($(ls ${target}* | _filter_archive | sort -r | sed -e "1,${versions}d"))
# Iterate each copy.
for copy in ${copies[@]}; do
local copy_data=($(_archive_parse ${copy}))
local copy_time=${copy_data[1]}
if [[ "${copy_time}" -lt "${time}" ]]; then
rm -- ${copy} && echo "${name}: Prune ${copy_time}."
fi
for archive in ${prune[@]}; do
rm -- "${archive}" && echo "Prune: ${archive}"
done
}
_iterate_targets process ${targets[@]}
}
# Delete old versions of an archive, keeping specified amount of versions. One version by default.
# Usage: archive_prune_versions <NAME> [VERSIONS]
function archive_prune_versions() {
local IFS=$'\n'
local target=${1}
local versions=${2}
[[ "${versions}" = "" ]] && versions=1
if [[ "${target}" = "" ]]; then
help archive_prune_versions
return 2
fi
local prune=($(ls ${target}* | grep -E ${_archive_pattern} | sort -r | sed -e "1,${versions}d"))
for archive in ${prune[@]}; do
rm -- "${archive}" && echo "Prune: ${archive}"
done
}
# Delete specified or all archive files.
# Usage: archive_rm [FILES]
function archive_rm() {
local IFS=$'\n'
local targets=(${@})
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
process() {
rm -- "${target}"
@ -193,7 +168,7 @@ function archive_rm() {
function archive_xz() {
local IFS=$'\n'
local targets=(${@})
[[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern_fast})
[[ "${targets}" = "" ]] && targets=$(_ls_archive_fast)
process() {
local data=($(_archive_parse "${target}"))
@ -224,7 +199,7 @@ function archive_name() {
local IFS=$'\n'
local targets="${1}"
local name="${2}"
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
process() {
# simplify name by default.
@ -258,7 +233,7 @@ function archive_name() {
function unarchive() {
local IFS=$'\n'
local targets=(${@})
[[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern})
[[ "${targets}" = "" ]] && targets=$(_ls_archive)
process() {
# extract hash from name.
@ -305,7 +280,7 @@ function _archive_parse() {
# Autocomplete for archive_name function.
# First arg is the archives list, second one is selected archive's current name.
function _archive_name() {
function _comp_archive_name() {
local IFS=$'\n'
COMPREPLY=()
@ -325,12 +300,12 @@ function _archive_name() {
}
# Autocomplete with archives in current dir.
function _archive_grep() {
function _comp_archive_grep() {
_autocomplete_grep ${_archive_pattern}
}
# Autocomplete with fast archives in current dir.
function _archive_grep_fast() {
function _comp_archive_grep_fast() {
_autocomplete_grep ${_archive_pattern_fast}
}
@ -342,29 +317,23 @@ function _archive_date() {
# Get names of all archives.
function _archive_names() {
local IFS=$'\n'
local archives=($(ls | grep -E ${_archive_pattern}))
local archives=($(_ls_archive))
local names=()
for archive in ${archives[@]}; do
local data=($(_archive_parse ${archive}))
local name=${data[0]}
names+=(${name})
names+=(${data[0]})
done
_autocomplete ${names[@]}
# Remove copies.
names=($(printf '%s\n' "${names[@]}" | sort -u))
printf '%s\n' "${names[@]}"
}
# Get names of all archives once.
function _archive_names_first() {
local IFS=$'\n'
local archives=($(ls | grep -E ${_archive_pattern}))
local names=()
for archive in ${archives[@]}; do
local data=($(_archive_parse ${archive}))
local name=${data[0]}
names+=(${name})
done
_autocomplete_first ${names[@]}
# Autocomplete with names of all archives.
function _comp_archive_names() {
_autocomplete $(_archive_names)
}
# Check if file is an archive.
@ -374,8 +343,22 @@ function _is_archive() {
[[ "${out}" != "" ]]
}
complete -o filenames -F _archive_grep archive_check unarchive archive_rm
complete -o filenames -F _archive_grep_fast archive_xz
complete -o filenames -F _archive_name archive_name
complete -o filenames -F _archive_names archive_prune
complete -o filenames -F _archive_names_first archive_prune_versions
# List all archives.
function _ls_archive() {
ls | grep -E ${_archive_pattern}
}
# List fast archives.
function _ls_archive_fast() {
ls | grep -E ${_archive_pattern_fast}
}
# Filter input for archives only.
function _filter_archive() {
grep -E ${_archive_pattern}
}
complete -o filenames -F _comp_archive_grep archive_check unarchive archive_rm
complete -o filenames -F _comp_archive_grep_fast archive_xz
complete -o filenames -F _comp_archive_name archive_name
complete -o filenames -F _comp_archive_names archive_prune

View file

@ -1,10 +1,8 @@
#!/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
for file in /var/roothome/.config/bash/module/*.sh; do
source "${file}"
done
dow=$(/usr/bin/date +%u) # current day of week number.
date=$(/usr/bin/date +%Y%m%d%H%M) # date stamp.
@ -36,25 +34,18 @@ shopt -s dotglob
builtin cd "${path_src}"
# Save media list.
/usr/bin/find ${path_media} -type d | gzip -1 > ${path_backup}/media/ColdMedia.txt.gz || notify 'Backup : Failed to save media list!'
/usr/bin/find ${path_media} -type d > ${path_backup}/media/ColdMedia.txt || notify 'Backup : Failed to save media list!'
cd ${path_backup}/media/
archive_fast ColdMedia.txt && rm ColdMedia.txt
archive_prune ColdMedia 1
cd -
# 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. NOTE: Now I manually archive all of that.
# builtin cd ${path_src}/media/book/ && recursive checksum_create || notify 'Backup : Checksum book failed!'
# builtin cd ${path_src}/media/music/ && recursive checksum_create || notify 'Backup : Checksum music failed!'
# builtin 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!'
# 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'
archive_fast docker/ && mv Docker_* ${path_docker}/ || notify 'Backup : Failed to save docker!'
cd ${path_docker}
archive_prune Docker 7
cd -
# Notify completion & size.
bupsize=$(/usr/bin/du -h ${path_docker}/${dow}_*.tgz | awk '{print $1}')