first steps with command line args

This commit is contained in:
Hartmut Bauer committed 2025-03-19 01:38:35 +01:00
1 parent 6d69da50bc
commit 24f6fc05ea
7 files changed
+234 -26

No files matched your search

+44 -26
View File
@@ -34,7 +34,7 @@ log() {
# zB. if is_in "foo" "foo" "bar" "foo bar" ; then ...
# Leere Liste liefert immer false
#
function is_in {
is_in() {
local search="$1"
shift 1
local e
@@ -47,7 +47,7 @@ function is_in {
# $1 - Verzeichnis in dem gesucht wird als absoluter Pfad
# $2 ff. - auszuschliessende Verzeichnisse (kann leer bleiben)
#
function iterate_dirs_in() {
iterate_dirs_in() {
local dirname
local root=$1
shift 1
@@ -63,7 +63,7 @@ function iterate_dirs_in() {
#
# $1 - dir to check
#
function check_directory() {
check_directory() {
local dir=$1
if [ -d "$1" ]; then
return 0
@@ -82,7 +82,7 @@ function check_directory() {
#
# $1 - zu löschendes Verzeichnis
#
function delete_dir() {
delete_dir() {
local dir=$1
sudo rm -rf "$dir" && return 0
log ERROR "### could not delete directory $1!"
@@ -93,7 +93,7 @@ function delete_dir() {
# Slashes "/" im Pfadnamen durch Dashes "-" ersetzen, so dass der String als Dateiname verwendet werden kann.
#
# $1 - path
function backup_file_prefix() {
backup_file_prefix() {
echo "$1" | sed -E 's/^\///' | sed -E 's/\//-/g'
}
@@ -103,7 +103,7 @@ function backup_file_prefix() {
# $2 - Zielverzeichnis für .tgz.gpg-Dateien
# $3 ff. - Verzeichnisse, die nicht gesichert werden sollen (kann leer bleiben)
#
function backup_all_dirs() {
backup_all_dirs() {
local root=$1
local target=$2
local prefix=$(backup_file_prefix $root)
@@ -114,7 +114,7 @@ function backup_all_dirs() {
for dir in $(iterate_dirs_in "$root" "$@"); do
tar_file="$target/$prefix-$dir.$EXT"
backup_dir "$root" "$tar_file" "$dir"
list "$tar_file" "$dir"
create_list "$tar_file" "$dir"
test_integrity "$tar_file"
done
}
@@ -125,7 +125,7 @@ function backup_all_dirs() {
# $2 - Zielpfad und -name für das Archiv
# $2 - zu sicherndes Verzeichnis
#
function backup_dir() {
backup_dir() {
local root=$1
local archive=$2
local dir=$3
@@ -143,7 +143,7 @@ function backup_dir() {
#
# $1 - Archiv
#
function list() {
create_list() {
local archive=$1
log INFO "--- List $archive"
sudo gpg --batch --decrypt --passphrase $PASSPHRASE "$archive" | sudo tar -tzv | sudo tee $archive.txt >/dev/null
@@ -160,7 +160,7 @@ function list() {
#
# $1 - Archiv
#
function test_integrity() {
test_integrity() {
local archive=$1
log INFO "--- Check integrity of $archive"
sudo gpg --batch --decrypt --passphrase $PASSPHRASE "$archive" | sudo tar -xzO >/dev/null
@@ -179,7 +179,7 @@ function test_integrity() {
# $1 - Archiv, ggf. mit Pfad
# $2 ff. - zurückzusichernde Dateien/Verzeichnisse
#
function restore() {
restore() {
local archive=$1
shift 1
sudo gpg --batch --decrypt --passphrase $PASSPHRASE "$archive" | sudo tar -xzv $@ >/dev/null
@@ -191,9 +191,25 @@ function restore() {
fi
}
# Liste der Archives auf stdout ausgeben
#
# Zeigt alle Archive incl. des letzten Verzeichnisses an
#
# z.B.
# backup.7/volume1-data-Web.tgz.gpg
# media/volume1-media-books.tgz.gpg
#
# $1 - root path i.d.R. ein Verzeichnis oberhalb, damit alle täglichen Backups angezeigt werden
#
list_archives() {
local target=$1
local targ=${target//\//\\\/} # slashes escapen
echo "#$targ#"
sudo find "$target" -type f -name "*.gpg" | sed -E "s/^$targ//"
}
# *** NAS Test ***
function nas_test() {
nas_test() {
LOG_FILE="/volume1/nfs/Server/tmp/backup.log"
log INFO "*** *** Begin NAS backup (data, nfs, media/books, media/music)"
@@ -208,22 +224,26 @@ function nas_test() {
# nfs
backup_all_dirs "/volume1/nfs" "$TARGET_DIR" "${EXCLUDE_DIRS[@]}"
TARGET_DIR="/volume1/nfs/backups/tmp/media"
BACKUP_ROOT=/volume1/nfs/backups/tmp
TARGET_DIR="$BACKUP_ROOT/media"
EXCLUDE_DIRS=("@eaDir" "Temporary Items" "lost+found" serien tmp video "video.archive")
check_directory $TARGET_DIR
# media/books media/music
backup_all_dirs "/volume1/media" "$TARGET_DIR" "${EXCLUDE_DIRS[@]}"
log INFO "*** End of NAS backuip"
log INFO "*** End of NAS backup"
list_archives $BACKUP_ROOT
}
# *** NATHAN Test ***
function nathan_test() {
nathan_test() {
LOG_FILE=/var/docker/tmp/backup.log
log INFO "*** *** Begin nathan server backup"
TARGET_DIR=/var/docker/tmp/backup.$DAY
BACKUP_ROOT=/var/docker/tmp
TARGET_DIR="$BACKUP_ROOT/backup.$DAY"
delete_dir $TARGET_DIR
check_directory $TARGET_DIR
@@ -232,14 +252,17 @@ function nathan_test() {
backup_all_dirs "/var/docker/vol" "$TARGET_DIR"
log INFO "*** End of nathan server backup"
list_archives $BACKUP_ROOT
}
# *** HANILLER Test ***
function hamiller_test() {
hamiller_test() {
LOG_FILE=/var/docker/tmp/backup.log
log INFO "*** *** Begin hamiller server backup"
TARGET_DIR=/var/docker/tmp/backup.$DAY
BACKUP_ROOT=/var/docker/tmp
TARGET_DIR="$BACKUP_ROOT/backup.$DAY"
delete_dir $TARGET_DIR
check_directory $TARGET_DIR
@@ -248,9 +271,10 @@ function hamiller_test() {
# backup_all_dirs "/var/docker/vol" "$TARGET_DIR"
log INFO "*** End of hamiller server backup"
list_archives $BACKUP_ROOT
}
function test() {
test() {
#LOG_FILE=./t1.log
#log INFO "error test"
echo "host: #$1#"
@@ -261,13 +285,7 @@ function test() {
#hamiller_test
#test1
test "$HOSTNAME"
if [ ! -z $ERROR ]; then
log ERROR "*** *** Errors occured during the progem run, see full log for details."
else
log INFO "*** *** Program ended successfully."
fi
list_archives /var/docker/tmp
exit