Backup : Use Archive module.
This commit is contained in:
parent
b5c1603413
commit
c2961c4fd7
|
@ -105,7 +105,7 @@ function archive_fast() {
|
||||||
function archive_check() {
|
function archive_check() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=(${@})
|
local targets=(${@})
|
||||||
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
|
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
# extract hash from name.
|
# extract hash from name.
|
||||||
|
@ -122,64 +122,39 @@ function archive_check() {
|
||||||
_iterate_targets process ${targets[@]}
|
_iterate_targets process ${targets[@]}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete old versions of archives.
|
# Delete old versions of an archive.
|
||||||
# All archives by default.
|
# All archives with 1 version by default.
|
||||||
# Usage: archive_prune [NAMES]
|
# Usage: archive_prune [NAME] [VERSIONS]
|
||||||
function archive_prune() {
|
function archive_prune() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=()
|
local targets=(${1})
|
||||||
for target in ${@}; do
|
local versions=${2}
|
||||||
targets+=($(ls ${target}* | grep -E ${_archive_pattern})) || return 2
|
|
||||||
done
|
[[ "${targets}" = "" ]] && targets=($(_archive_names))
|
||||||
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
|
[[ "${versions}" = "" ]] && versions=1
|
||||||
|
|
||||||
|
if [[ ${#} -gt 2 ]]; then
|
||||||
|
help archive_prune
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
local data=($(_archive_parse ${target}))
|
local prune=($(ls ${target}* | _filter_archive | sort -r | sed -e "1,${versions}d"))
|
||||||
local name=${data[0]}
|
|
||||||
local time=${data[1]}
|
|
||||||
local copies=($(ls ${name}* | grep -E ${_archive_pattern}))
|
|
||||||
|
|
||||||
# Iterate each copy.
|
for archive in ${prune[@]}; do
|
||||||
for copy in ${copies[@]}; do
|
rm -- "${archive}" && echo "Prune: ${archive}"
|
||||||
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
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
_iterate_targets process ${targets[@]}
|
_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.
|
# Delete specified or all archive files.
|
||||||
# Usage: archive_rm [FILES]
|
# Usage: archive_rm [FILES]
|
||||||
function archive_rm() {
|
function archive_rm() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=(${@})
|
local targets=(${@})
|
||||||
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
|
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
rm -- "${target}"
|
rm -- "${target}"
|
||||||
|
@ -193,7 +168,7 @@ function archive_rm() {
|
||||||
function archive_xz() {
|
function archive_xz() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=(${@})
|
local targets=(${@})
|
||||||
[[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern_fast})
|
[[ "${targets}" = "" ]] && targets=$(_ls_archive_fast)
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
local data=($(_archive_parse "${target}"))
|
local data=($(_archive_parse "${target}"))
|
||||||
|
@ -224,7 +199,7 @@ function archive_name() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets="${1}"
|
local targets="${1}"
|
||||||
local name="${2}"
|
local name="${2}"
|
||||||
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
|
[[ "${targets}" = "" ]] && targets=($(_ls_archive))
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
# simplify name by default.
|
# simplify name by default.
|
||||||
|
@ -258,7 +233,7 @@ function archive_name() {
|
||||||
function unarchive() {
|
function unarchive() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=(${@})
|
local targets=(${@})
|
||||||
[[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern})
|
[[ "${targets}" = "" ]] && targets=$(_ls_archive)
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
# extract hash from name.
|
# extract hash from name.
|
||||||
|
@ -305,7 +280,7 @@ function _archive_parse() {
|
||||||
|
|
||||||
# Autocomplete for archive_name function.
|
# Autocomplete for archive_name function.
|
||||||
# First arg is the archives list, second one is selected archive's current name.
|
# First arg is the archives list, second one is selected archive's current name.
|
||||||
function _archive_name() {
|
function _comp_archive_name() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
|
|
||||||
|
@ -325,12 +300,12 @@ function _archive_name() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Autocomplete with archives in current dir.
|
# Autocomplete with archives in current dir.
|
||||||
function _archive_grep() {
|
function _comp_archive_grep() {
|
||||||
_autocomplete_grep ${_archive_pattern}
|
_autocomplete_grep ${_archive_pattern}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Autocomplete with fast archives in current dir.
|
# Autocomplete with fast archives in current dir.
|
||||||
function _archive_grep_fast() {
|
function _comp_archive_grep_fast() {
|
||||||
_autocomplete_grep ${_archive_pattern_fast}
|
_autocomplete_grep ${_archive_pattern_fast}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,29 +317,23 @@ function _archive_date() {
|
||||||
# Get names of all archives.
|
# Get names of all archives.
|
||||||
function _archive_names() {
|
function _archive_names() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local archives=($(ls | grep -E ${_archive_pattern}))
|
local archives=($(_ls_archive))
|
||||||
local names=()
|
local names=()
|
||||||
|
|
||||||
for archive in ${archives[@]}; do
|
for archive in ${archives[@]}; do
|
||||||
local data=($(_archive_parse ${archive}))
|
local data=($(_archive_parse ${archive}))
|
||||||
local name=${data[0]}
|
names+=(${data[0]})
|
||||||
names+=(${name})
|
|
||||||
done
|
done
|
||||||
|
|
||||||
_autocomplete ${names[@]}
|
# Remove copies.
|
||||||
|
names=($(printf '%s\n' "${names[@]}" | sort -u))
|
||||||
|
|
||||||
|
printf '%s\n' "${names[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get names of all archives once.
|
# Autocomplete with names of all archives.
|
||||||
function _archive_names_first() {
|
function _comp_archive_names() {
|
||||||
local IFS=$'\n'
|
_autocomplete $(_archive_names)
|
||||||
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[@]}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if file is an archive.
|
# Check if file is an archive.
|
||||||
|
@ -374,8 +343,22 @@ function _is_archive() {
|
||||||
[[ "${out}" != "" ]]
|
[[ "${out}" != "" ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
complete -o filenames -F _archive_grep archive_check unarchive archive_rm
|
# List all archives.
|
||||||
complete -o filenames -F _archive_grep_fast archive_xz
|
function _ls_archive() {
|
||||||
complete -o filenames -F _archive_name archive_name
|
ls | grep -E ${_archive_pattern}
|
||||||
complete -o filenames -F _archive_names archive_prune
|
}
|
||||||
complete -o filenames -F _archive_names_first archive_prune_versions
|
|
||||||
|
# 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
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# TODO: Rewrite me, maybe? <3
|
for file in /var/roothome/.config/bash/module/*.sh; do
|
||||||
|
source "${file}"
|
||||||
source /var/roothome/.config/bash/module/notify.sh
|
done
|
||||||
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.
|
dow=$(/usr/bin/date +%u) # current day of week number.
|
||||||
date=$(/usr/bin/date +%Y%m%d%H%M) # date stamp.
|
date=$(/usr/bin/date +%Y%m%d%H%M) # date stamp.
|
||||||
|
@ -36,25 +34,18 @@ shopt -s dotglob
|
||||||
builtin cd "${path_src}"
|
builtin cd "${path_src}"
|
||||||
|
|
||||||
# Save media list.
|
# 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.
|
# Backup docker.
|
||||||
rm ${path_docker}/${dow}_*.tgz
|
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!'
|
archive_fast docker/ && mv Docker_* ${path_docker}/ || notify 'Backup : Failed to save docker!'
|
||||||
|
cd ${path_docker}
|
||||||
# Update checksums. NOTE: Now I manually archive all of that.
|
archive_prune Docker 7
|
||||||
# builtin cd ${path_src}/media/book/ && recursive checksum_create || notify 'Backup : Checksum book failed!'
|
cd -
|
||||||
# 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'
|
|
||||||
|
|
||||||
# Notify completion & size.
|
# Notify completion & size.
|
||||||
bupsize=$(/usr/bin/du -h ${path_docker}/${dow}_*.tgz | awk '{print $1}')
|
bupsize=$(/usr/bin/du -h ${path_docker}/${dow}_*.tgz | awk '{print $1}')
|
||||||
|
|
Reference in a new issue