From 5d6c19f6394c0a321c69285f133a116f94b9d347 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 4 Nov 2024 19:05:37 +0100 Subject: [PATCH 01/54] style: apply automatic formatting --- domposy.sh | 186 +++++++++++++++++++++++++++-------------------------- 1 file changed, 94 insertions(+), 92 deletions(-) diff --git a/domposy.sh b/domposy.sh index a871abf..e159e83 100644 --- a/domposy.sh +++ b/domposy.sh @@ -3,13 +3,11 @@ # DESCRIPTION: # This script simplifies your Docker Compose management. - ENABLE_ADVANCED_LOGGING=false ENABLE_DEBUG_LOGGING=false LOG_FILE_PATH="/tmp" - # # # # # # # # # # # #|# # # # # # # # # # # # # SCRIPT INFORMATION # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -19,7 +17,6 @@ SCRIPT_NAME="$0" SIMPLE_SCRIPT_NAME=$(basename "$SCRIPT_NAME") SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION="${SIMPLE_SCRIPT_NAME%.*}" - # # # # # # # # # # # #|# # # # # # # # # # # # # LOGGING DIRECTORIES # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -28,7 +25,6 @@ LOG_DIR="${LOG_FILE_PATH}/${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_logs/" LOG_FILE="$(date +"%Y-%m-%d_%H-%M-%S")_log_${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}.txt" LOG_FILE_WITH_LOG_DIR="${LOG_DIR}${LOG_FILE}" - # # # # # # # # # # # #|# # # # # # # # # # # # # LOGGING FUNCTIONALITY # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -48,9 +44,9 @@ log() { if [[ -n "$message" ]]; then while IFS= read -r line; do - echo "$(date +"%d.%m.%Y %H:%M:%S") - $level - $line" >> "$LOG_FILE_WITH_LOG_DIR" + echo "$(date +"%d.%m.%Y %H:%M:%S") - $level - $line" >>"$LOG_FILE_WITH_LOG_DIR" echo " $level$script_info - $line" - done <<< "$message" + done <<<"$message" fi } @@ -91,7 +87,6 @@ log_error() { exit 1 } - # # # # # # # # # # # #|# # # # # # # # # # # # # PREPARATIONS # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -110,9 +105,9 @@ check_permissions() { # Returns the Docker Compose command. So whether 'docker-compose' or 'docker compose'. get_docker_compose_command() { - if command -v docker-compose &> /dev/null; then + if command -v docker-compose &>/dev/null; then echo "docker-compose" - elif docker compose version &> /dev/null; then + elif docker compose version &>/dev/null; then echo "docker compose" else log_error "Neither 'docker-compose' nor 'docker compose' command found. Is it installed?" @@ -137,7 +132,6 @@ DOCKER_COMPOSE_CMD=$(get_docker_compose_command) log_debug "'${DOCKER_COMPOSE_CMD}' is used" validate_docker_compose_command - # # # # # # # # # # # #|# # # # # # # # # # # # # GETOPTS # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -157,58 +151,57 @@ ENABLE_CLEANUP=false while getopts ":hdna:s:b:e:c" opt; do case ${opt} in - h ) - echo "It is recommended to run the script with root rights to ensure that the backups work properly." - echo - echo "Usage: (sudo) $SCRIPT_NAME [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" - echo " -h Show help" - echo " -d Enables debug logging" - echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" - echo " -a ACTION ACTION to be performed: 'update', 'backup' or 'all' (Default: '${DEFAULT_ACTION}')" - echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" - echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" - echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" - echo " -c Additional docker cleanup" - exit 0 - ;; - d ) - log_debug "'-d' selected" - ENABLE_DEBUG_LOGGING=true - ;; - n ) - log_debug "'-n' selected" - ENABLE_DRY_RUN=true - ;; - a ) - log_debug "'-a' selected: '$OPTARG'" - ACTION="${OPTARG}" - ;; - s ) - log_debug "'-s' selected: '$OPTARG'" - SEARCH_DIR="${OPTARG}" - ;; - b ) - log_debug "'-b' selected: '$OPTARG'" - BACKUP_DIR="${OPTARG}" - ;; - e ) - log_debug "'-e' selected: '$OPTARG'" - EXCLUDE_DIR="${OPTARG}" - ;; - c ) - log_debug "'-c' selected" - ENABLE_CLEANUP=true - ;; - \? ) - log_error "Invalid option: -$OPTARG" - ;; - : ) - log_error "Option -$OPTARG requires an argument!" - ;; + h) + echo "It is recommended to run the script with root rights to ensure that the backups work properly." + echo + echo "Usage: (sudo) $SCRIPT_NAME [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" + echo " -h Show help" + echo " -d Enables debug logging" + echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" + echo " -a ACTION ACTION to be performed: 'update', 'backup' or 'all' (Default: '${DEFAULT_ACTION}')" + echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" + echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" + echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" + echo " -c Additional docker cleanup" + exit 0 + ;; + d) + log_debug "'-d' selected" + ENABLE_DEBUG_LOGGING=true + ;; + n) + log_debug "'-n' selected" + ENABLE_DRY_RUN=true + ;; + a) + log_debug "'-a' selected: '$OPTARG'" + ACTION="${OPTARG}" + ;; + s) + log_debug "'-s' selected: '$OPTARG'" + SEARCH_DIR="${OPTARG}" + ;; + b) + log_debug "'-b' selected: '$OPTARG'" + BACKUP_DIR="${OPTARG}" + ;; + e) + log_debug "'-e' selected: '$OPTARG'" + EXCLUDE_DIR="${OPTARG}" + ;; + c) + log_debug "'-c' selected" + ENABLE_CLEANUP=true + ;; + \?) + log_error "Invalid option: -$OPTARG" + ;; + :) + log_error "Option -$OPTARG requires an argument!" + ;; esac done -shift $((OPTIND -1)) - +shift $((OPTIND - 1)) # # # # # # # # # # # #|# # # # # # # # # # # # # FUNCTIONS # @@ -354,7 +347,12 @@ backup_docker_compose_folder() { log_info "TAR..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || { log_warning "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations."; rm -f "$tar_file_with_backup_dir"; return; } + tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || + { + log_warning "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + rm -f "$tar_file_with_backup_dir" + return + } else log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." fi @@ -362,7 +360,12 @@ backup_docker_compose_folder() { log_info "GZIP..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - gzip "$tar_file_with_backup_dir" || { log_warning "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations."; rm -f "$tar_file_with_backup_dir" "$gz_file_with_backup_dir"; return; } + gzip "$tar_file_with_backup_dir" || + { + log_warning "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + rm -f "$tar_file_with_backup_dir" "$gz_file_with_backup_dir" + return + } else log_dry_run "gzip $tar_file_with_backup_dir" fi @@ -403,16 +406,16 @@ perform_action_for_single_docker_compose_container() { fi case $ACTION in - update ) - remove_docker_compose_images - ;; - backup ) - backup_docker_compose_folder "$file" - ;; - all ) - backup_docker_compose_folder "$file" - remove_docker_compose_images - ;; + update) + remove_docker_compose_images + ;; + backup) + backup_docker_compose_folder "$file" + ;; + all) + backup_docker_compose_folder "$file" + remove_docker_compose_images + ;; esac log_info "UP ('${file_simple_dirname}')..." @@ -429,24 +432,24 @@ perform_action_for_single_docker_compose_container() { perform_action_for_all_docker_compose_containers() { log_info ">>>>>>>>>>>>>>> DOCKER COMPOSE >>>>>>>>>>>>>>>" case $ACTION in - update|backup|all ) - log_debug "Action selected: '${ACTION}'" - - local docker_compose_files=$(find_docker_compose_files) - - if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." - else - log_info "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" - fi - - while IFS= read -r file; do - perform_action_for_single_docker_compose_container "$file" - done <<< "$docker_compose_files" - ;; - * ) - log_error "Invalid action: '${ACTION}'" - ;; + update | backup | all) + log_debug "Action selected: '${ACTION}'" + + local docker_compose_files=$(find_docker_compose_files) + + if [ -z "$docker_compose_files" ]; then + log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." + else + log_info "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" + fi + + while IFS= read -r file; do + perform_action_for_single_docker_compose_container "$file" + done <<<"$docker_compose_files" + ;; + *) + log_error "Invalid action: '${ACTION}'" + ;; esac log_info "<<<<<<<<<<<<<<< DOCKER COMPOSE <<<<<<<<<<<<<<<" } @@ -492,7 +495,6 @@ cleanup() { log_info "<<<<<<<<<<<<<<< CLEANUP <<<<<<<<<<<<<<<" } - # # # # # # # # # # # #|# # # # # # # # # # # # # LOGIC # # # # # # # # # # # # #|# # # # # # # # # # # # @@ -520,4 +522,4 @@ fi show_docker_info show_log_file -exit 0 \ No newline at end of file +exit 0 From 6453ee0fb395ac7de6b6fa420c9cd19c0fa8cc79 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 4 Nov 2024 19:16:49 +0100 Subject: [PATCH 02/54] feat: remove update functionality (BREAKING CHANGE) Closes #4 --- domposy.sh | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/domposy.sh b/domposy.sh index e159e83..d0ae02a 100644 --- a/domposy.sh +++ b/domposy.sh @@ -136,7 +136,7 @@ validate_docker_compose_command # GETOPTS # # # # # # # # # # # # #|# # # # # # # # # # # # -DEFAULT_ACTION="all" +DEFAULT_ACTION="backup" DEFAULT_SEARCH_DIR="/home/" DEFAULT_BACKUP_DIR="/tmp/${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" DEFAULT_EXCLUDE_DIR="tmp" @@ -158,7 +158,7 @@ while getopts ":hdna:s:b:e:c" opt; do echo " -h Show help" echo " -d Enables debug logging" echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" - echo " -a ACTION ACTION to be performed: 'update', 'backup' or 'all' (Default: '${DEFAULT_ACTION}')" + echo " -a ACTION ACTION to be performed: 'backup' (Default: '${DEFAULT_ACTION}')" echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" @@ -270,21 +270,6 @@ find_docker_compose_files() { echo "$docker_compose_files" } -# Removes Docker images that are defined in a Docker Compose configuration file. -remove_docker_compose_images() { - local images=$($DOCKER_COMPOSE_CMD config | grep 'image:' | sed -E 's/.*image: *//') - - for image in $images; do - log_info "Remove image ('${image}')..." - - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$(docker rmi "${image}")" - else - log_dry_run "docker rmi ${image}" - fi - done -} - # Outputs debug information for a file. debug_file_info() { local func_description="$1" @@ -406,16 +391,9 @@ perform_action_for_single_docker_compose_container() { fi case $ACTION in - update) - remove_docker_compose_images - ;; backup) backup_docker_compose_folder "$file" ;; - all) - backup_docker_compose_folder "$file" - remove_docker_compose_images - ;; esac log_info "UP ('${file_simple_dirname}')..." @@ -432,7 +410,7 @@ perform_action_for_single_docker_compose_container() { perform_action_for_all_docker_compose_containers() { log_info ">>>>>>>>>>>>>>> DOCKER COMPOSE >>>>>>>>>>>>>>>" case $ACTION in - update | backup | all) + backup) log_debug "Action selected: '${ACTION}'" local docker_compose_files=$(find_docker_compose_files) From 2df597ec624f99937dbdd23ec3615a2e4ea1a319 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 4 Nov 2024 20:37:33 +0100 Subject: [PATCH 03/54] Move domposy script to src --- domposy.sh => src/domposy.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename domposy.sh => src/domposy.sh (100%) diff --git a/domposy.sh b/src/domposy.sh similarity index 100% rename from domposy.sh rename to src/domposy.sh From c27e369615a4e4e2bb1686535ee1a58b2646a81a Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 4 Nov 2024 20:38:14 +0100 Subject: [PATCH 04/54] Change file extension sh to bash --- src/{domposy.sh => domposy.bash} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{domposy.sh => domposy.bash} (100%) diff --git a/src/domposy.sh b/src/domposy.bash similarity index 100% rename from src/domposy.sh rename to src/domposy.bash From e398e127f09a04d8911f3d60a98adcff3f37f805 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Wed, 6 Nov 2024 23:33:27 +0100 Subject: [PATCH 05/54] Use simbashlog as logger, add initial setup script, change license to GPL3, adjust README --- LICENSE | 695 +++++++++++++++++++++++++++++++++++++++++++++-- README.md | 10 +- setup.bash | 121 +++++++++ src/domposy.bash | 297 ++++++++++---------- 4 files changed, 949 insertions(+), 174 deletions(-) create mode 100644 setup.bash diff --git a/LICENSE b/LICENSE index 1617c0d..e72bfdd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,674 @@ -MIT License - -Copyright (c) 2024 Fabian Fuchs - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/README.md b/README.md index 28eb4dc..1c10102 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # `domposy`: Simplify your Docker Compose management - Create backups...

- - MIT License + + GPL-3.0 License

@@ -105,3 +105,9 @@ If you think this tool is useful and saves you a lot of work and nerves and lets ## This might also interest you [`esase`](https://github.com/fuchs-fabian/esase): Easy Setup And System Enhancement (Popup based Linux Bash script/tool) + +--- + +> This repository uses [`simbashlog`](https://github.com/fuchs-fabian/simbashlog) ([LICENSE](https://github.com/fuchs-fabian/simbashlog/blob/main/LICENSE)). +> +> *Copyright (C) 2024 Fabian Fuchs* diff --git a/setup.bash b/setup.bash new file mode 100644 index 0000000..7ab71d5 --- /dev/null +++ b/setup.bash @@ -0,0 +1,121 @@ +#!/usr/bin/env bash + +APP_NAME="domposy" +APP_BRANCH_NAME="v2" + +# NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'! +REPO_URLS=( + "https://github.com/fuchs-fabian/simbashlog.git" + "https://github.com/fuchs-fabian/${APP_NAME}.git" +) + +DEPENDENCIES=( + "git" + "rsync" +) + +OPT_DIR="/opt" + +function install { + function check_dependencies { + for dependency in "${DEPENDENCIES[@]}"; do + echo "Checking if '$dependency' is available..." + command -v "$dependency" >/dev/null 2>&1 || + { + echo "'$dependency' is not available. Please install it and try again." + exit 1 + } + echo "'$dependency' is available." + done + } + + function get_app_name_from_repo_url { + "$(basename "$1" .git)" + } + + read -r -p "Do you want to install the $APP_NAME globally ('g') or just for the current user ('u')? (g/u): " bin_dir_choice + if [[ "$bin_dir_choice" =~ ^[Gg]$ ]]; then + # Symlink in /usr/bin for all users + bin_path="/usr/bin" + else + # Symlink in ~/bin for the current user + bin_path="$HOME/bin" + fi + + for repo_url in "${REPO_URLS[@]}"; do + app_name=$(get_app_name_from_repo_url "$repo_url") + app_dir="$OPT_DIR/$app_name" + + if [ -d "$app_dir" ]; then + echo "$app_name is already installed." + else + read -r -p "$app_name is not installed. Do you want to install it? (y/n): " choice + if [[ "$choice" =~ ^[Yy]$ ]]; then + echo "Cloning $app_name..." + + if [[ "$app_name" == "$APP_NAME" ]]; then + git clone --branch "$APP_BRANCH_NAME" "$repo_url" "$app_dir" + else + git clone "$repo_url" "$app_dir" + fi + + if [ -f "$app_dir/src/${app_name}.bash" ]; then + chmod +x "$app_dir/src/${app_name}.bash" + + echo "Creating symlink for $app_name..." + ln -sf "$app_dir/src/${app_name}.bash" "$bin_path/$app_name" + echo "$app_name has been installed and is now executable." + + if [ -n "$(command -v "$app_name")" ]; then + "$app_name" --version + echo "$app_name is working." + else + echo "$app_name is not working." + fi + else + echo "The file ${app_name}.bash was not found in the directory $app_dir/src." + fi + fi + fi + done +} + +function uninstall { + local bin_paths=( + "/usr/bin" + "$HOME/bin" + ) + + for repo_url in "${REPO_URLS[@]}"; do + app_name=$(basename "$repo_url" .git) + app_dir="$OPT_DIR/$app_name" + + for bin_path in "${bin_paths[@]}"; do + if [ -L "$bin_path/$app_name" ]; then + echo "Removing symlink for $app_name in $bin_path..." + rm "$bin_path/$app_name" + fi + done + + if [ -d "$app_dir" ]; then + echo "Removing directory $app_dir..." + sudo rm -rf "$app_dir" + else + echo "$app_name is not installed." + fi + done +} + +# Main menu +case "$1" in +install) + install + ;; +uninstall) + uninstall + ;; +*) + echo "Usage: $0 {install|uninstall}" + exit 1 + ;; +esac diff --git a/src/domposy.bash b/src/domposy.bash index d0ae02a..0313d23 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -1,90 +1,88 @@ -#!/bin/bash +#!/usr/bin/env bash # DESCRIPTION: # This script simplifies your Docker Compose management. -ENABLE_ADVANCED_LOGGING=false -ENABLE_DEBUG_LOGGING=false - -LOG_FILE_PATH="/tmp" - -# # # # # # # # # # # #|# # # # # # # # # # # # -# SCRIPT INFORMATION # -# # # # # # # # # # # #|# # # # # # # # # # # # - -SCRIPT_DIR=$(dirname "$(realpath "$0")") -SCRIPT_NAME="$0" -SIMPLE_SCRIPT_NAME=$(basename "$SCRIPT_NAME") -SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION="${SIMPLE_SCRIPT_NAME%.*}" - -# # # # # # # # # # # #|# # # # # # # # # # # # -# LOGGING DIRECTORIES # -# # # # # # # # # # # #|# # # # # # # # # # # # - -LOG_DIR="${LOG_FILE_PATH}/${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_logs/" -LOG_FILE="$(date +"%Y-%m-%d_%H-%M-%S")_log_${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}.txt" -LOG_FILE_WITH_LOG_DIR="${LOG_DIR}${LOG_FILE}" - -# # # # # # # # # # # #|# # # # # # # # # # # # -# LOGGING FUNCTIONALITY # -# # # # # # # # # # # #|# # # # # # # # # # # # - -log() { - local level="$1" - local message="$2" - - if [[ ! -d "$LOG_DIR" ]]; then - mkdir -p "$LOG_DIR" - fi - - local script_info="" - if [ "$ENABLE_ADVANCED_LOGGING" = true ]; then - script_info=" ($SIMPLE_SCRIPT_NAME)" - fi - - if [[ -n "$message" ]]; then - while IFS= read -r line; do - echo "$(date +"%d.%m.%Y %H:%M:%S") - $level - $line" >>"$LOG_FILE_WITH_LOG_DIR" - echo " $level$script_info - $line" - done <<<"$message" - fi -} +function source_bin_script { + local script_name="$1" + local bin_paths=( + "/bin" + "/usr/bin" + "/usr/local/bin" + "$HOME/bin" + ) + + for bin_path in "${bin_paths[@]}"; do + local path="$bin_path/$script_name" + + if [ -L "$path" ]; then + local original_path + original_path=$(readlink -f "$path") + + if [ -f "$original_path" ]; then + # shellcheck source=/dev/null + source "$original_path" || + { + echo "Error: Unable to source symlinked script '$original_path'" + return 1 + } + return 0 + else + echo "Error: Unable to source symlinked script '$script_name' because the original file '$original_path' was not found." + return 1 + fi + elif [ -f "$path" ]; then + # shellcheck source=/dev/null + source "$path" || + { + echo "Error: Unable to source script '$path'" + return 1 + } + return 0 + fi + done -log_debug() { - if [ "$ENABLE_DEBUG_LOGGING" = true ]; then - log "DEBUG " "$1" - fi + echo "Error: Unable to source '$script_name' because it was not found in the specified bin paths (${bin_paths[*]// /, })." + return 1 } -log_cmd() { - log "CMD " "$1" -} +LOGGER="simbashlog" + +source_bin_script "$LOGGER" || + { + echo "Critical: Unable resolve logger script '$LOGGER'. Exiting..." + exit 1 + } + +# shellcheck disable=SC2034 +ENABLE_LOG_FILE=true +# shellcheck disable=SC2034 +ENABLE_LOG_TO_SYSTEM=false +# shellcheck disable=SC2034 +LOG_DIR="/tmp/simbashlogs/" +# shellcheck disable=SC2034 +ENABLE_SIMPLE_LOG_DIR_STRUCTURE=true +# shellcheck disable=SC2034 +ENABLE_COMBINED_LOG_FILES=false +# shellcheck disable=SC2034 +LOG_LEVEL=6 +# shellcheck disable=SC2034 +LOG_LEVEL_FOR_SYSTEM_LOGGING=4 +# shellcheck disable=SC2034 +FACILITY_NAME_FOR_SYSTEM_LOGGING="user" +# shellcheck disable=SC2034 +ENABLE_EXITING_SCRIPT_IF_AT_LEAST_ERROR_IS_LOGGED=false +# shellcheck disable=SC2034 +ENABLE_DATE_IN_CONSOLE_OUTPUTS_FOR_LOGGING=true +# shellcheck disable=SC2034 +SHOW_CURRENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING="path" +# shellcheck disable=SC2034 +ENABLE_PARENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING=false +# shellcheck disable=SC2034 +ENABLE_SUMMARY_ON_EXIT=true log_dry_run() { - log "DRY-RUN" "Dry run is enabled. Skipping '$1'" -} - -log_info() { - log "INFO " "$1" -} - -log_warning() { - log "WARNING" "$1" -} - -show_log_file() { - if [[ -f "$LOG_FILE_WITH_LOG_DIR" ]]; then - log_info "Log file: '${LOG_FILE_WITH_LOG_DIR}'" - else - echo "E R R O R - Log file creation failed: '${LOG_FILE_WITH_LOG_DIR}' - E R R O R" - fi -} - -log_error() { - log "ERROR " "$1" - - show_log_file - exit 1 + log_info "Dry run is enabled. Skipping '$1'" } # # # # # # # # # # # #|# # # # # # # # # # # # @@ -93,10 +91,10 @@ log_error() { # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. check_permissions() { - log_info "Current user: '$(whoami)'" + log_notice "Current user: '$(whoami)'" if [[ $(id -u) -ne 0 ]]; then if groups $(whoami) | grep -q '\bdocker\b'; then - log_warning "You do not have root rights. If you want to create backups, they may not work properly." + log_warn "You do not have root rights. If you want to create backups, they may not work properly." else log_error "You need to be either a member of the 'docker' group or have root privileges to run this script." fi @@ -121,7 +119,7 @@ validate_docker_compose_command() { if [[ $? -ne 0 ]]; then log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" fi - log_cmd "$version_output" + log_info "$version_output" } check_permissions @@ -138,7 +136,7 @@ validate_docker_compose_command DEFAULT_ACTION="backup" DEFAULT_SEARCH_DIR="/home/" -DEFAULT_BACKUP_DIR="/tmp/${SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" +DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" DEFAULT_EXCLUDE_DIR="tmp" ACTION="${DEFAULT_ACTION}" @@ -154,7 +152,7 @@ while getopts ":hdna:s:b:e:c" opt; do h) echo "It is recommended to run the script with root rights to ensure that the backups work properly." echo - echo "Usage: (sudo) $SCRIPT_NAME [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" + echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" echo " -h Show help" echo " -d Enables debug logging" echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" @@ -212,7 +210,7 @@ validate_search_dir() { if [[ "${SEARCH_DIR: -1}" != "/" ]]; then tmp_search_dir="${SEARCH_DIR}" SEARCH_DIR="${SEARCH_DIR}/" - log_warning "SEARCH_DIR: '${tmp_search_dir}' changed to '${SEARCH_DIR}'" + log_warn "SEARCH_DIR: '${tmp_search_dir}' changed to '${SEARCH_DIR}'" fi if [[ ! -d "$SEARCH_DIR" ]]; then @@ -222,38 +220,36 @@ validate_search_dir() { local absolute_search_dir=$(realpath "$SEARCH_DIR") if [[ "$SEARCH_DIR" != "$absolute_search_dir/" ]]; then - log_warning "SEARCH_DIR: '${SEARCH_DIR}' replaced with the absolute path '${absolute_search_dir}/'" + log_warn "SEARCH_DIR: '${SEARCH_DIR}' replaced with the absolute path '${absolute_search_dir}/'" SEARCH_DIR="${absolute_search_dir}/" fi } # Returns the most important variables used by this script. get_vars() { - log_info ">>>>>>>>>>>>>>> VARIABLES >>>>>>>>>>>>>>>" - log_info "Script name: '${SCRIPT_NAME}'" - log_info "Log file with log dir: '${LOG_FILE_WITH_LOG_DIR}'" - log_info "Action: '${ACTION}'" - log_info "Search dir: '${SEARCH_DIR}'" - log_info "Backup dir: '${BACKUP_DIR}'" - log_info "Exclude dir: '${EXCLUDE_DIR}'" - log_info "<<<<<<<<<<<<<<< VARIABLES <<<<<<<<<<<<<<<" + log_notice ">>>>>>>>>>>>>>> VARIABLES >>>>>>>>>>>>>>>" + log_notice "Action: '${ACTION}'" + log_notice "Search dir: '${SEARCH_DIR}'" + log_notice "Backup dir: '${BACKUP_DIR}'" + log_notice "Exclude dir: '${EXCLUDE_DIR}'" + log_notice "<<<<<<<<<<<<<<< VARIABLES <<<<<<<<<<<<<<<" } # Outputs information on the Docker status. show_docker_info() { - log_info ">>>>>>>>>>>>>>> DOCKER INFO >>>>>>>>>>>>>>>" - log_info "docker system df..." - log_cmd "$(docker system df)" + log_notice ">>>>>>>>>>>>>>> DOCKER INFO >>>>>>>>>>>>>>>" + log_notice "docker system df..." + log_info "$(docker system df)" - log_info "docker ps..." - log_cmd "$(docker ps)" + log_notice "docker ps..." + log_info "$(docker ps)" - log_info "docker info (formatted)..." - log_cmd "$(docker info --format "Containers: {{.Containers}} | Running: {{.ContainersRunning}} | Paused: {{.ContainersPaused}} | Stopped: {{.ContainersStopped}} | Images: {{.Images}} | Docker Root Dir: {{.DockerRootDir}}")" + log_notice "docker info (formatted)..." + log_info "$(docker info --format "Containers: {{.Containers}} | Running: {{.ContainersRunning}} | Paused: {{.ContainersPaused}} | Stopped: {{.ContainersStopped}} | Images: {{.Images}} | Docker Root Dir: {{.DockerRootDir}}")" - log_info "docker images..." - log_cmd "$(docker images)" - log_info "<<<<<<<<<<<<<<< DOCKER INFO <<<<<<<<<<<<<<<" + log_notice "docker images..." + log_info "$(docker images)" + log_notice "<<<<<<<<<<<<<<< DOCKER INFO <<<<<<<<<<<<<<<" } # Searches for Docker Compose files in a specific directory and excludes a specified subdirectory. @@ -290,7 +286,7 @@ check_file_creation() { if [[ "$ENABLE_DRY_RUN" == false ]]; then if [[ -f "$file" ]]; then - log_info "File created: '$file'" + log_notice "File created: '$file'" else log_error "File creation failed: '$file'" fi @@ -310,7 +306,7 @@ backup_docker_compose_folder() { if [[ "${BACKUP_DIR: -1}" != "/" ]]; then BACKUP_DIR="${BACKUP_DIR}/" - log_warning "BACKUP_DIR: '${tmp_backup_dir}' changed to '${BACKUP_DIR}'" + log_warn "BACKUP_DIR: '${tmp_backup_dir}' changed to '${BACKUP_DIR}'" fi BACKUP_DIR="${BACKUP_DIR}$(date +"%Y-%m-%d")/" @@ -318,7 +314,7 @@ backup_docker_compose_folder() { if [[ ! -d "$BACKUP_DIR" ]]; then if [[ "$ENABLE_DRY_RUN" == false ]]; then mkdir -p "$BACKUP_DIR" - log_info "Backup directory '$(realpath "$BACKUP_DIR")' was created" + log_notice "Backup directory '$(realpath "$BACKUP_DIR")' was created" else log_dry_run "mkdir -p $BACKUP_DIR" fi @@ -330,11 +326,11 @@ backup_docker_compose_folder() { local tar_file_with_backup_dir="${BACKUP_DIR}${tar_file}" local gz_file_with_backup_dir="${BACKUP_DIR}${gz_file}" - log_info "TAR..." + log_notice "TAR..." if [[ "$ENABLE_DRY_RUN" == false ]]; then tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || { - log_warning "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + log_warn "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." rm -f "$tar_file_with_backup_dir" return } @@ -343,11 +339,11 @@ backup_docker_compose_folder() { fi check_file_creation $tar_file_with_backup_dir - log_info "GZIP..." + log_notice "GZIP..." if [[ "$ENABLE_DRY_RUN" == false ]]; then gzip "$tar_file_with_backup_dir" || { - log_warning "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + log_warn "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." rm -f "$tar_file_with_backup_dir" "$gz_file_with_backup_dir" return } @@ -356,18 +352,18 @@ backup_docker_compose_folder() { fi check_file_creation $gz_file_with_backup_dir - log_info "'${BACKUP_DIR}'..." + log_notice "'${BACKUP_DIR}'..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$(ls -larth "${BACKUP_DIR}")" + log_info "$(ls -larth "${BACKUP_DIR}")" else log_dry_run "ls -larth $BACKUP_DIR" fi - log_info "[-->] Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_info "[-->] To navigate to the backup folder: 'cd ${BACKUP_DIR}'" - log_info "[-->] To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-containers/${file_simple_dirname}/'" - log_info "[-->] To undo gzip: '(sudo) gunzip ${gz_file}'" - log_info "[-->] To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" + log_notice "[-->] Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." + log_notice "[-->] To navigate to the backup folder: 'cd ${BACKUP_DIR}'" + log_notice "[-->] To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-containers/${file_simple_dirname}/'" + log_notice "[-->] To undo gzip: '(sudo) gunzip ${gz_file}'" + log_notice "[-->] To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } # Performs a specific action for a Docker Compose configuration file. @@ -377,15 +373,15 @@ perform_action_for_single_docker_compose_container() { local file_simple_dirname=$(basename "$(dirname "$file")") debug_file_info "Perform action for single Docker Compose container" "$file" "$file_dir" "$file_simple_dirname" - log_info ">>>>>>>>>> '${file}' >>>>>>>>>>" + log_notice ">>>>>>>>>> '${file}' >>>>>>>>>>" cd "${file_dir}" - log_info "Changed directory to '$(pwd)'" + log_notice "Changed directory to '$(pwd)'" - log_info ">>>>> '${ACTION}' >>>>>" - log_info "DOWN ('${file_simple_dirname}')..." + log_notice ">>>>> '${ACTION}' >>>>>" + log_notice "DOWN ('${file_simple_dirname}')..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$($DOCKER_COMPOSE_CMD down)" + log_info "$($DOCKER_COMPOSE_CMD down)" else log_dry_run "$DOCKER_COMPOSE_CMD down" fi @@ -396,19 +392,19 @@ perform_action_for_single_docker_compose_container() { ;; esac - log_info "UP ('${file_simple_dirname}')..." + log_notice "UP ('${file_simple_dirname}')..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$($DOCKER_COMPOSE_CMD up -d)" + log_info "$($DOCKER_COMPOSE_CMD up -d)" else log_dry_run "$DOCKER_COMPOSE_CMD up -d" fi - log_info "<<<<< '${ACTION}' <<<<<" - log_info "<<<<<<<<<< '${file}' <<<<<<<<<<" + log_notice "<<<<< '${ACTION}' <<<<<" + log_notice "<<<<<<<<<< '${file}' <<<<<<<<<<" } # Performs a specified action for all Docker Compose files in a search directory. perform_action_for_all_docker_compose_containers() { - log_info ">>>>>>>>>>>>>>> DOCKER COMPOSE >>>>>>>>>>>>>>>" + log_notice ">>>>>>>>>>>>>>> DOCKER COMPOSE >>>>>>>>>>>>>>>" case $ACTION in backup) log_debug "Action selected: '${ACTION}'" @@ -418,7 +414,7 @@ perform_action_for_all_docker_compose_containers() { if [ -z "$docker_compose_files" ]; then log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." else - log_info "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" + log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi while IFS= read -r file; do @@ -429,63 +425,63 @@ perform_action_for_all_docker_compose_containers() { log_error "Invalid action: '${ACTION}'" ;; esac - log_info "<<<<<<<<<<<<<<< DOCKER COMPOSE <<<<<<<<<<<<<<<" + log_notice "<<<<<<<<<<<<<<< DOCKER COMPOSE <<<<<<<<<<<<<<<" } # Performs a cleanup of the Docker resources cleanup() { - log_info ">>>>>>>>>>>>>>> CLEANUP >>>>>>>>>>>>>>>" + log_notice ">>>>>>>>>>>>>>> CLEANUP >>>>>>>>>>>>>>>" - log_info ">>>>>>>>>> PREVIEW >>>>>>>>>>" - log_info "Listing non-running containers..." - log_cmd "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" + log_notice ">>>>>>>>>> PREVIEW >>>>>>>>>>" + log_notice "Listing non-running containers..." + log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" - log_info "Listing unused docker images..." - log_cmd "$(docker image ls -a --filter dangling=true)" + log_notice "Listing unused docker images..." + log_info "$(docker image ls -a --filter dangling=true)" - log_info "Listing unused volumes..." - log_cmd "$(docker volume ls --filter dangling=true)" - log_info "<<<<<<<<<< PREVIEW <<<<<<<<<<" + log_notice "Listing unused volumes..." + log_info "$(docker volume ls --filter dangling=true)" + log_notice "<<<<<<<<<< PREVIEW <<<<<<<<<<" - log_info ">>>>>>>>>> CLEAN >>>>>>>>>>" - log_info "Removing non-running containers..." + log_notice ">>>>>>>>>> CLEAN >>>>>>>>>>" + log_notice "Removing non-running containers..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$(docker container prune -f)" + log_info "$(docker container prune -f)" else log_dry_run "docker container prune -f" fi - log_info "Removing unused docker images..." + log_notice "Removing unused docker images..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$(docker image prune -f)" + log_info "$(docker image prune -f)" else log_dry_run "docker image prune -f" fi - log_info "Removing unused volumes..." + log_notice "Removing unused volumes..." if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_cmd "$(docker volume prune -f)" + log_info "$(docker volume prune -f)" else log_dry_run "docker volume prune -f" fi - log_info "<<<<<<<<<< CLEAN <<<<<<<<<<" + log_notice "<<<<<<<<<< CLEAN <<<<<<<<<<" - log_info "<<<<<<<<<<<<<<< CLEANUP <<<<<<<<<<<<<<<" + log_notice "<<<<<<<<<<<<<<< CLEANUP <<<<<<<<<<<<<<<" } # # # # # # # # # # # #|# # # # # # # # # # # # # LOGIC # # # # # # # # # # # # #|# # # # # # # # # # # # -log_info "'$SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." +log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." if $ENABLE_DRY_RUN; then - log_warning "Dry run is enabled!" + log_warn "Dry run is enabled!" fi validate_search_dir -log_info "Current directory: '$(pwd)'" +log_notice "Current directory: '$(pwd)'" get_vars @@ -499,5 +495,4 @@ fi show_docker_info -show_log_file exit 0 From 97b5966cc441eed9ec1acb1c5b817f36ceca2d40 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Wed, 6 Nov 2024 23:41:03 +0100 Subject: [PATCH 06/54] Update README.md --- README.md | 51 ++++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 1c10102..efd426f 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,17 @@

+ + ## Description ### What is `domposy`? -`domposy` is a Linux Bash script that helps to backup and update Docker Compose containers (preferably with bind mounts). In addition, it is possible to perform a secure Docker cleanup of resources that are no longer used. +`domposy` is a Linux Bash script that helps to backup Docker Compose containers (with bind mounts). In addition, it is possible to perform a secure Docker cleanup of resources that are no longer used. This involves searching for the Docker Compose folder, which then also contains the `.yml` file, the bind mounts that are important for the container itself and hopefully an `.env` file. 😜 @@ -62,45 +68,28 @@ The whole thing is still at an early stage of development and can therefore lead ## Getting Started -It is best to download the latest release to your target environment. +The easiest way is to download and run the setup.bash script. -Make the script executable: - -```bash -chmod +x domposy.sh -``` - -Use the script: - -```plain -Usage: (sudo) ./domposy.sh [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c] - -h Show help - -d Enables debug logging - -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging - -a ACTION ACTION to be performed: 'update', 'backup' or 'all' (Default: 'all') - -s SEARCH_DIR Directory to search for docker-compose files (Default: '/home/') - -b BACKUP_DIR Destination directory for backups (Default: '/tmp/domposy_backups/') - -e EXCLUDE_DIR Directory to exclude from search (Default: 'tmp') - -c Additional docker cleanup -``` + ### Example -```bash -sudo ./domposy.sh -d -a all -s . -b /tmp/test -e /tmp -c -``` - -### Example with Advanced Debugging - -```bash -sudo ./domposy.sh > /tmp/debug-log_domposy.txt -``` + ## Donate with [PayPal](https://www.paypal.com/donate/?hosted_button_id=4G9X8TDNYYNKG) If you think this tool is useful and saves you a lot of work and nerves and lets you sleep better, then a small donation would be very nice. -[![Paypal](https://www.paypalobjects.com/de_DE/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate/?hosted_button_id=4G9X8TDNYYNKG) + + + Donate with PayPal + ## This might also interest you From 89fd21feb5d6fcb3626a53ff3a38bebc89b08ca8 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Wed, 6 Nov 2024 23:42:38 +0100 Subject: [PATCH 07/54] Fix debug logging --- src/domposy.bash | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/domposy.bash b/src/domposy.bash index 0313d23..d11c3fe 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -165,7 +165,8 @@ while getopts ":hdna:s:b:e:c" opt; do ;; d) log_debug "'-d' selected" - ENABLE_DEBUG_LOGGING=true + # shellcheck disable=SC2034 + LOG_LEVEL=7 ;; n) log_debug "'-n' selected" From de26cab14e79f1421150c35e54ed5babdc7d5133 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 00:04:45 +0100 Subject: [PATCH 08/54] Improve delimiter logging, Update function definition syntax --- src/domposy.bash | 96 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 30 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index d11c3fe..4462775 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -81,16 +81,50 @@ ENABLE_PARENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING=false # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=true -log_dry_run() { +function log_dry_run { log_info "Dry run is enabled. Skipping '$1'" } +function log_delimiter { + local level="$1" + local text="$2" + local char="$3" + local use_uppercase="$4" + local number + local separator="" + + case $level in + 1) number=15 ;; + 2) number=10 ;; + 3) number=5 ;; + *) number=3 ;; + esac + + for ((i = 0; i < number; i++)); do + separator+="$char" + done + + if is_true "$use_uppercase"; then + text=$(to_uppercase "$text") + fi + + log_info "$separator ${text} $separator" +} + +function log_delimiter_start { + log_delimiter "$1" "$2" ">" false +} + +function log_delimiter_end { + log_delimiter "$1" "$2" "<" false +} + # # # # # # # # # # # #|# # # # # # # # # # # # # PREPARATIONS # # # # # # # # # # # # #|# # # # # # # # # # # # # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. -check_permissions() { +function check_permissions { log_notice "Current user: '$(whoami)'" if [[ $(id -u) -ne 0 ]]; then if groups $(whoami) | grep -q '\bdocker\b'; then @@ -102,7 +136,7 @@ check_permissions() { } # Returns the Docker Compose command. So whether 'docker-compose' or 'docker compose'. -get_docker_compose_command() { +function get_docker_compose_command { if command -v docker-compose &>/dev/null; then echo "docker-compose" elif docker compose version &>/dev/null; then @@ -113,7 +147,7 @@ get_docker_compose_command() { } # Validates whether the docker compose command can also be executed by determining the version. -validate_docker_compose_command() { +function validate_docker_compose_command { local version_output="$($DOCKER_COMPOSE_CMD version 2>&1)" if [[ $? -ne 0 ]]; then @@ -207,7 +241,7 @@ shift $((OPTIND - 1)) # # # # # # # # # # # #|# # # # # # # # # # # # # Validation of the search dir and adjustments (absolute path) if necessary. -validate_search_dir() { +function validate_search_dir { if [[ "${SEARCH_DIR: -1}" != "/" ]]; then tmp_search_dir="${SEARCH_DIR}" SEARCH_DIR="${SEARCH_DIR}/" @@ -227,18 +261,18 @@ validate_search_dir() { } # Returns the most important variables used by this script. -get_vars() { - log_notice ">>>>>>>>>>>>>>> VARIABLES >>>>>>>>>>>>>>>" +function get_vars { + log_delimiter_start 1 "VARIABLES" log_notice "Action: '${ACTION}'" log_notice "Search dir: '${SEARCH_DIR}'" log_notice "Backup dir: '${BACKUP_DIR}'" log_notice "Exclude dir: '${EXCLUDE_DIR}'" - log_notice "<<<<<<<<<<<<<<< VARIABLES <<<<<<<<<<<<<<<" + log_delimiter_end 1 "VARIABLES" } # Outputs information on the Docker status. -show_docker_info() { - log_notice ">>>>>>>>>>>>>>> DOCKER INFO >>>>>>>>>>>>>>>" +function show_docker_info { + log_delimiter_start 1 "DOCKER INFO" log_notice "docker system df..." log_info "$(docker system df)" @@ -250,11 +284,11 @@ show_docker_info() { log_notice "docker images..." log_info "$(docker images)" - log_notice "<<<<<<<<<<<<<<< DOCKER INFO <<<<<<<<<<<<<<<" + log_delimiter_end 1 "DOCKER INFO" } # Searches for Docker Compose files in a specific directory and excludes a specified subdirectory. -find_docker_compose_files() { +function find_docker_compose_files { local docker_compose_file_names=("${DOCKER_COMPOSE_NAME}.yml" "${DOCKER_COMPOSE_NAME}.yaml") local docker_compose_files="" @@ -268,7 +302,7 @@ find_docker_compose_files() { } # Outputs debug information for a file. -debug_file_info() { +function debug_file_info { local func_description="$1" local file="$2" local file_dir="$3" @@ -280,7 +314,7 @@ debug_file_info() { } # Checks whether a file has been created, if not, the script is cancelled. -check_file_creation() { +function check_file_creation { local file=$1 debug_file_info "Check file creation" "$file" @@ -297,7 +331,7 @@ check_file_creation() { } # Creates a backup of a Docker Compose folder by packing the files into a tar archive and then compressing them. -backup_docker_compose_folder() { +function backup_docker_compose_folder { local file=$1 local file_dir=$(dirname "$file") local file_simple_dirname=$(basename "$(dirname "$file")") @@ -368,18 +402,19 @@ backup_docker_compose_folder() { } # Performs a specific action for a Docker Compose configuration file. -perform_action_for_single_docker_compose_container() { +function perform_action_for_single_docker_compose_container { local file=$1 local file_dir=$(dirname "$file") local file_simple_dirname=$(basename "$(dirname "$file")") debug_file_info "Perform action for single Docker Compose container" "$file" "$file_dir" "$file_simple_dirname" - log_notice ">>>>>>>>>> '${file}' >>>>>>>>>>" + log_delimiter_start 2 "'${file}'" cd "${file_dir}" log_notice "Changed directory to '$(pwd)'" - log_notice ">>>>> '${ACTION}' >>>>>" + log_delimiter_start 3 "'${ACTION}'" + log_notice "DOWN ('${file_simple_dirname}')..." if [[ "$ENABLE_DRY_RUN" == false ]]; then log_info "$($DOCKER_COMPOSE_CMD down)" @@ -399,13 +434,14 @@ perform_action_for_single_docker_compose_container() { else log_dry_run "$DOCKER_COMPOSE_CMD up -d" fi - log_notice "<<<<< '${ACTION}' <<<<<" - log_notice "<<<<<<<<<< '${file}' <<<<<<<<<<" + + log_delimiter_end 3 "'${ACTION}'" + log_delimiter_end 2 "'${file}'" } # Performs a specified action for all Docker Compose files in a search directory. -perform_action_for_all_docker_compose_containers() { - log_notice ">>>>>>>>>>>>>>> DOCKER COMPOSE >>>>>>>>>>>>>>>" +function perform_action_for_all_docker_compose_containers { + log_delimiter_start 1 "DOCKER COMPOSE" case $ACTION in backup) log_debug "Action selected: '${ACTION}'" @@ -426,14 +462,14 @@ perform_action_for_all_docker_compose_containers() { log_error "Invalid action: '${ACTION}'" ;; esac - log_notice "<<<<<<<<<<<<<<< DOCKER COMPOSE <<<<<<<<<<<<<<<" + log_delimiter_end 1 "DOCKER COMPOSE" } # Performs a cleanup of the Docker resources -cleanup() { - log_notice ">>>>>>>>>>>>>>> CLEANUP >>>>>>>>>>>>>>>" +function cleanup { + log_delimiter_start 1 "CLEANUP" - log_notice ">>>>>>>>>> PREVIEW >>>>>>>>>>" + log_delimiter_start 2 "PREVIEW" log_notice "Listing non-running containers..." log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" @@ -442,9 +478,9 @@ cleanup() { log_notice "Listing unused volumes..." log_info "$(docker volume ls --filter dangling=true)" - log_notice "<<<<<<<<<< PREVIEW <<<<<<<<<<" + log_delimiter_end 2 "PREVIEW" - log_notice ">>>>>>>>>> CLEAN >>>>>>>>>>" + log_delimiter_start 2 "CLEAN" log_notice "Removing non-running containers..." if [[ "$ENABLE_DRY_RUN" == false ]]; then log_info "$(docker container prune -f)" @@ -465,9 +501,9 @@ cleanup() { else log_dry_run "docker volume prune -f" fi - log_notice "<<<<<<<<<< CLEAN <<<<<<<<<<" + log_delimiter_end 2 "CLEAN" - log_notice "<<<<<<<<<<<<<<< CLEANUP <<<<<<<<<<<<<<<" + log_delimiter_end 1 "CLEANUP" } # # # # # # # # # # # #|# # # # # # # # # # # # From dec238640ee42184ec853fbe0899f5c7ccbe0c92 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 18:18:09 +0100 Subject: [PATCH 09/54] Simplify source bin scripts --- src/domposy.bash | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 4462775..129c7a8 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -3,7 +3,7 @@ # DESCRIPTION: # This script simplifies your Docker Compose management. -function source_bin_script { +function find_bin_script { local script_name="$1" local bin_paths=( "/bin" @@ -20,32 +20,33 @@ function source_bin_script { original_path=$(readlink -f "$path") if [ -f "$original_path" ]; then - # shellcheck source=/dev/null - source "$original_path" || - { - echo "Error: Unable to source symlinked script '$original_path'" - return 1 - } + echo "$original_path" return 0 - else - echo "Error: Unable to source symlinked script '$script_name' because the original file '$original_path' was not found." - return 1 fi elif [ -f "$path" ]; then - # shellcheck source=/dev/null - source "$path" || - { - echo "Error: Unable to source script '$path'" - return 1 - } + echo "$path" return 0 fi done - echo "Error: Unable to source '$script_name' because it was not found in the specified bin paths (${bin_paths[*]// /, })." + echo "Error: '$script_name' not found in the specified bin paths (${bin_paths[*]// /, })." >&2 return 1 } +function source_bin_script { + local script_name="$1" + local script_path + + script_path=$(find_bin_script "$script_name") || return 1 + + # shellcheck source=/dev/null + source "$script_path" || + { + echo "Error: Unable to source script '$script_path'" + return 1 + } +} + LOGGER="simbashlog" source_bin_script "$LOGGER" || From 93bdd9e862dcd3224d525f05781456f9eb031acd Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 18:28:01 +0100 Subject: [PATCH 10/54] Improve comments --- src/domposy.bash | 56 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 129c7a8..dc761c8 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -3,6 +3,14 @@ # DESCRIPTION: # This script simplifies your Docker Compose management. +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ SOURCING HELPER ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + function find_bin_script { local script_name="$1" local bin_paths=( @@ -47,6 +55,14 @@ function source_bin_script { } } +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ LOGGING HELPER ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + LOGGER="simbashlog" source_bin_script "$LOGGER" || @@ -120,9 +136,13 @@ function log_delimiter_end { log_delimiter "$1" "$2" "<" false } -# # # # # # # # # # # #|# # # # # # # # # # # # -# PREPARATIONS # -# # # # # # # # # # # #|# # # # # # # # # # # # +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ PREPARATIONS ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. function check_permissions { @@ -165,9 +185,13 @@ DOCKER_COMPOSE_CMD=$(get_docker_compose_command) log_debug "'${DOCKER_COMPOSE_CMD}' is used" validate_docker_compose_command -# # # # # # # # # # # #|# # # # # # # # # # # # -# GETOPTS # -# # # # # # # # # # # #|# # # # # # # # # # # # +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ ARGUMENTS ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ DEFAULT_ACTION="backup" DEFAULT_SEARCH_DIR="/home/" @@ -237,9 +261,13 @@ while getopts ":hdna:s:b:e:c" opt; do done shift $((OPTIND - 1)) -# # # # # # # # # # # #|# # # # # # # # # # # # -# FUNCTIONS # -# # # # # # # # # # # #|# # # # # # # # # # # # +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ FUNCTIONS ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # Validation of the search dir and adjustments (absolute path) if necessary. function validate_search_dir { @@ -507,9 +535,13 @@ function cleanup { log_delimiter_end 1 "CLEANUP" } -# # # # # # # # # # # #|# # # # # # # # # # # # -# LOGIC # -# # # # # # # # # # # #|# # # # # # # # # # # # +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ MAIN ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." From 32b7e61a1803edc4b78e5f5518773f29627d5166 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 18:54:01 +0100 Subject: [PATCH 11/54] Fix shellcheck warnings --- src/domposy.bash | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index dc761c8..1d33fda 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -88,7 +88,7 @@ LOG_LEVEL_FOR_SYSTEM_LOGGING=4 # shellcheck disable=SC2034 FACILITY_NAME_FOR_SYSTEM_LOGGING="user" # shellcheck disable=SC2034 -ENABLE_EXITING_SCRIPT_IF_AT_LEAST_ERROR_IS_LOGGED=false +ENABLE_EXITING_SCRIPT_IF_AT_LEAST_ERROR_IS_LOGGED=true # shellcheck disable=SC2034 ENABLE_DATE_IN_CONSOLE_OUTPUTS_FOR_LOGGING=true # shellcheck disable=SC2034 @@ -148,7 +148,7 @@ function log_delimiter_end { function check_permissions { log_notice "Current user: '$(whoami)'" if [[ $(id -u) -ne 0 ]]; then - if groups $(whoami) | grep -q '\bdocker\b'; then + if groups "$(whoami)" | grep -q '\bdocker\b'; then log_warn "You do not have root rights. If you want to create backups, they may not work properly." else log_error "You need to be either a member of the 'docker' group or have root privileges to run this script." @@ -169,11 +169,8 @@ function get_docker_compose_command { # Validates whether the docker compose command can also be executed by determining the version. function validate_docker_compose_command { - local version_output="$($DOCKER_COMPOSE_CMD version 2>&1)" - - if [[ $? -ne 0 ]]; then - log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" - fi + local version_output + version_output=$($DOCKER_COMPOSE_CMD version 2>&1) || log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" log_info "$version_output" } @@ -281,7 +278,8 @@ function validate_search_dir { log_error "The specified search directory '$SEARCH_DIR' could not be found" fi - local absolute_search_dir=$(realpath "$SEARCH_DIR") + local absolute_search_dir + absolute_search_dir=$(realpath "$SEARCH_DIR") if [[ "$SEARCH_DIR" != "$absolute_search_dir/" ]]; then log_warn "SEARCH_DIR: '${SEARCH_DIR}' replaced with the absolute path '${absolute_search_dir}/'" @@ -362,8 +360,13 @@ function check_file_creation { # Creates a backup of a Docker Compose folder by packing the files into a tar archive and then compressing them. function backup_docker_compose_folder { local file=$1 - local file_dir=$(dirname "$file") - local file_simple_dirname=$(basename "$(dirname "$file")") + + local file_dir + file_dir=$(dirname "$file") + + local file_simple_dirname + file_simple_dirname=$(basename "$(dirname "$file")") + debug_file_info "Backup Docker Compose folder" "$file" "$file_dir" "$file_simple_dirname" local tmp_backup_dir="${BACKUP_DIR}" @@ -384,7 +387,9 @@ function backup_docker_compose_folder { fi fi - local tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" + local tar_file + tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" + local gz_file="${tar_file}.gz" local tar_file_with_backup_dir="${BACKUP_DIR}${tar_file}" @@ -401,7 +406,7 @@ function backup_docker_compose_folder { else log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." fi - check_file_creation $tar_file_with_backup_dir + check_file_creation "$tar_file_with_backup_dir" log_notice "GZIP..." if [[ "$ENABLE_DRY_RUN" == false ]]; then @@ -414,7 +419,7 @@ function backup_docker_compose_folder { else log_dry_run "gzip $tar_file_with_backup_dir" fi - check_file_creation $gz_file_with_backup_dir + check_file_creation "$gz_file_with_backup_dir" log_notice "'${BACKUP_DIR}'..." if [[ "$ENABLE_DRY_RUN" == false ]]; then @@ -433,13 +438,19 @@ function backup_docker_compose_folder { # Performs a specific action for a Docker Compose configuration file. function perform_action_for_single_docker_compose_container { local file=$1 - local file_dir=$(dirname "$file") - local file_simple_dirname=$(basename "$(dirname "$file")") + + local file_dir + file_dir=$(dirname "$file") + + local file_simple_dirname + file_simple_dirname=$(basename "$(dirname "$file")") + debug_file_info "Perform action for single Docker Compose container" "$file" "$file_dir" "$file_simple_dirname" log_delimiter_start 2 "'${file}'" - cd "${file_dir}" + cd "${file_dir}" || log_error "Failed to change directory to '${file_dir}'" + log_notice "Changed directory to '$(pwd)'" log_delimiter_start 3 "'${ACTION}'" @@ -475,7 +486,8 @@ function perform_action_for_all_docker_compose_containers { backup) log_debug "Action selected: '${ACTION}'" - local docker_compose_files=$(find_docker_compose_files) + local docker_compose_files + docker_compose_files=$(find_docker_compose_files) if [ -z "$docker_compose_files" ]; then log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." From ae5caa84df331437d14f3e69f92e94dd63dac031 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 20:31:53 +0100 Subject: [PATCH 12/54] Improve dry run logic --- src/domposy.bash | 73 +++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 1d33fda..46de7b5 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -266,6 +266,10 @@ shift $((OPTIND - 1)) # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +function dry_run_enabled { + is_true "$ENABLE_DRY_RUN" +} + # Validation of the search dir and adjustments (absolute path) if necessary. function validate_search_dir { if [[ "${SEARCH_DIR: -1}" != "/" ]]; then @@ -346,14 +350,14 @@ function check_file_creation { debug_file_info "Check file creation" "$file" - if [[ "$ENABLE_DRY_RUN" == false ]]; then + if dry_run_enabled; then + log_dry_run "ls -larth $file" + else if [[ -f "$file" ]]; then log_notice "File created: '$file'" else log_error "File creation failed: '$file'" fi - else - log_dry_run "-f $file" fi } @@ -379,11 +383,11 @@ function backup_docker_compose_folder { BACKUP_DIR="${BACKUP_DIR}$(date +"%Y-%m-%d")/" if [[ ! -d "$BACKUP_DIR" ]]; then - if [[ "$ENABLE_DRY_RUN" == false ]]; then + if dry_run_enabled; then + log_dry_run "mkdir -p $BACKUP_DIR" + else mkdir -p "$BACKUP_DIR" log_notice "Backup directory '$(realpath "$BACKUP_DIR")' was created" - else - log_dry_run "mkdir -p $BACKUP_DIR" fi fi @@ -396,37 +400,34 @@ function backup_docker_compose_folder { local gz_file_with_backup_dir="${BACKUP_DIR}${gz_file}" log_notice "TAR..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then + if dry_run_enabled; then + log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." + else tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || { log_warn "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." rm -f "$tar_file_with_backup_dir" return } - else - log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." fi check_file_creation "$tar_file_with_backup_dir" log_notice "GZIP..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then + if dry_run_enabled; then + log_dry_run "gzip $tar_file_with_backup_dir" + else gzip "$tar_file_with_backup_dir" || { log_warn "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." rm -f "$tar_file_with_backup_dir" "$gz_file_with_backup_dir" return } - else - log_dry_run "gzip $tar_file_with_backup_dir" fi + check_file_creation "$gz_file_with_backup_dir" log_notice "'${BACKUP_DIR}'..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$(ls -larth "${BACKUP_DIR}")" - else - log_dry_run "ls -larth $BACKUP_DIR" - fi + if dry_run_enabled; then log_dry_run "ls -larth $BACKUP_DIR"; else log_info "$(ls -larth "$BACKUP_DIR")"; fi log_notice "[-->] Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." log_notice "[-->] To navigate to the backup folder: 'cd ${BACKUP_DIR}'" @@ -456,11 +457,7 @@ function perform_action_for_single_docker_compose_container { log_delimiter_start 3 "'${ACTION}'" log_notice "DOWN ('${file_simple_dirname}')..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$($DOCKER_COMPOSE_CMD down)" - else - log_dry_run "$DOCKER_COMPOSE_CMD down" - fi + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_info "$($DOCKER_COMPOSE_CMD down)"; fi case $ACTION in backup) @@ -469,11 +466,7 @@ function perform_action_for_single_docker_compose_container { esac log_notice "UP ('${file_simple_dirname}')..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$($DOCKER_COMPOSE_CMD up -d)" - else - log_dry_run "$DOCKER_COMPOSE_CMD up -d" - fi + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_info "$($DOCKER_COMPOSE_CMD up -d)"; fi log_delimiter_end 3 "'${ACTION}'" log_delimiter_end 2 "'${file}'" @@ -511,6 +504,7 @@ function cleanup { log_delimiter_start 1 "CLEANUP" log_delimiter_start 2 "PREVIEW" + log_notice "Listing non-running containers..." log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" @@ -519,29 +513,20 @@ function cleanup { log_notice "Listing unused volumes..." log_info "$(docker volume ls --filter dangling=true)" + log_delimiter_end 2 "PREVIEW" log_delimiter_start 2 "CLEAN" + log_notice "Removing non-running containers..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$(docker container prune -f)" - else - log_dry_run "docker container prune -f" - fi + if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_info "$(docker container prune -f)"; fi log_notice "Removing unused docker images..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$(docker image prune -f)" - else - log_dry_run "docker image prune -f" - fi + if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_info "$(docker image prune -f)"; fi log_notice "Removing unused volumes..." - if [[ "$ENABLE_DRY_RUN" == false ]]; then - log_info "$(docker volume prune -f)" - else - log_dry_run "docker volume prune -f" - fi + if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_info "$(docker volume prune -f)"; fi + log_delimiter_end 2 "CLEAN" log_delimiter_end 1 "CLEANUP" @@ -557,9 +542,7 @@ function cleanup { log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." -if $ENABLE_DRY_RUN; then - log_warn "Dry run is enabled!" -fi +if dry_run_enabled; then log_warn "Dry run is enabled!"; fi validate_search_dir From 19a6c6a20124ecdb868a274a13f9c6c86e308262 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 20:33:03 +0100 Subject: [PATCH 13/54] Improve backup log output --- src/domposy.bash | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 46de7b5..aa1d644 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -429,11 +429,11 @@ function backup_docker_compose_folder { log_notice "'${BACKUP_DIR}'..." if dry_run_enabled; then log_dry_run "ls -larth $BACKUP_DIR"; else log_info "$(ls -larth "$BACKUP_DIR")"; fi - log_notice "[-->] Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_notice "[-->] To navigate to the backup folder: 'cd ${BACKUP_DIR}'" - log_notice "[-->] To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-containers/${file_simple_dirname}/'" - log_notice "[-->] To undo gzip: '(sudo) gunzip ${gz_file}'" - log_notice "[-->] To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" + log_notice "--> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." + log_notice "--> To navigate to the backup folder: 'cd ${BACKUP_DIR}'" + log_notice "--> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-containers/${file_simple_dirname}/'" + log_notice "--> To undo gzip: '(sudo) gunzip ${gz_file}'" + log_notice "--> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } # Performs a specific action for a Docker Compose configuration file. From 579f40b0b5806f59f1897113be3cca9a72ea3dbc Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 20:42:12 +0100 Subject: [PATCH 14/54] Use inner functions --- src/domposy.bash | 58 +++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index aa1d644..babd603 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -451,13 +451,21 @@ function perform_action_for_single_docker_compose_container { log_delimiter_start 2 "'${file}'" cd "${file_dir}" || log_error "Failed to change directory to '${file_dir}'" - log_notice "Changed directory to '$(pwd)'" log_delimiter_start 3 "'${ACTION}'" - log_notice "DOWN ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_info "$($DOCKER_COMPOSE_CMD down)"; fi + function down { + log_notice "DOWN ('${file_simple_dirname}')..." + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_info "$($DOCKER_COMPOSE_CMD down)"; fi + } + + function up { + log_notice "UP ('${file_simple_dirname}')..." + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_info "$($DOCKER_COMPOSE_CMD up -d)"; fi + } + + down case $ACTION in backup) @@ -465,8 +473,7 @@ function perform_action_for_single_docker_compose_container { ;; esac - log_notice "UP ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_info "$($DOCKER_COMPOSE_CMD up -d)"; fi + up log_delimiter_end 3 "'${ACTION}'" log_delimiter_end 2 "'${file}'" @@ -501,34 +508,39 @@ function perform_action_for_all_docker_compose_containers { # Performs a cleanup of the Docker resources function cleanup { - log_delimiter_start 1 "CLEANUP" - - log_delimiter_start 2 "PREVIEW" + function process_preview { + log_delimiter_start 2 "PREVIEW" - log_notice "Listing non-running containers..." - log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" + log_notice "Listing non-running containers..." + log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" - log_notice "Listing unused docker images..." - log_info "$(docker image ls -a --filter dangling=true)" + log_notice "Listing unused docker images..." + log_info "$(docker image ls -a --filter dangling=true)" - log_notice "Listing unused volumes..." - log_info "$(docker volume ls --filter dangling=true)" + log_notice "Listing unused volumes..." + log_info "$(docker volume ls --filter dangling=true)" - log_delimiter_end 2 "PREVIEW" + log_delimiter_end 2 "PREVIEW" + } - log_delimiter_start 2 "CLEAN" + function process_clean { + log_delimiter_start 2 "CLEAN" - log_notice "Removing non-running containers..." - if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_info "$(docker container prune -f)"; fi + log_notice "Removing non-running containers..." + if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_info "$(docker container prune -f)"; fi - log_notice "Removing unused docker images..." - if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_info "$(docker image prune -f)"; fi + log_notice "Removing unused docker images..." + if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_info "$(docker image prune -f)"; fi - log_notice "Removing unused volumes..." - if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_info "$(docker volume prune -f)"; fi + log_notice "Removing unused volumes..." + if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_info "$(docker volume prune -f)"; fi - log_delimiter_end 2 "CLEAN" + log_delimiter_end 2 "CLEAN" + } + log_delimiter_start 1 "CLEANUP" + process_preview + process_clean log_delimiter_end 1 "CLEANUP" } From 907864a7b1f6e552aaf0b8db34ae605629b97d59 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 21:14:15 +0100 Subject: [PATCH 15/54] Change script usage, renamings --- src/domposy.bash | 107 +++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 60 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index babd603..aebec49 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -201,9 +201,8 @@ BACKUP_DIR="${DEFAULT_BACKUP_DIR}" EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" ENABLE_DRY_RUN=false -ENABLE_CLEANUP=false -while getopts ":hdna:s:b:e:c" opt; do +while getopts ":hdna:s:b:e" opt; do case ${opt} in h) echo "It is recommended to run the script with root rights to ensure that the backups work properly." @@ -212,11 +211,10 @@ while getopts ":hdna:s:b:e:c" opt; do echo " -h Show help" echo " -d Enables debug logging" echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" - echo " -a ACTION ACTION to be performed: 'backup' (Default: '${DEFAULT_ACTION}')" + echo " -a ACTION ACTION to be performed: 'backup' or 'clean' (Default: '${DEFAULT_ACTION}')" echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" - echo " -c Additional docker cleanup" exit 0 ;; d) @@ -244,10 +242,6 @@ while getopts ":hdna:s:b:e:c" opt; do log_debug "'-e' selected: '$OPTARG'" EXCLUDE_DIR="${OPTARG}" ;; - c) - log_debug "'-c' selected" - ENABLE_CLEANUP=true - ;; \?) log_error "Invalid option: -$OPTARG" ;; @@ -361,8 +355,8 @@ function check_file_creation { fi } -# Creates a backup of a Docker Compose folder by packing the files into a tar archive and then compressing them. -function backup_docker_compose_folder { +# Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. +function create_backup_file_for_single_docker_compose_project { local file=$1 local file_dir @@ -431,13 +425,12 @@ function backup_docker_compose_folder { log_notice "--> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." log_notice "--> To navigate to the backup folder: 'cd ${BACKUP_DIR}'" - log_notice "--> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-containers/${file_simple_dirname}/'" + log_notice "--> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" log_notice "--> To undo gzip: '(sudo) gunzip ${gz_file}'" log_notice "--> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } -# Performs a specific action for a Docker Compose configuration file. -function perform_action_for_single_docker_compose_container { +function backup_single_docker_compose_project { local file=$1 local file_dir @@ -446,7 +439,7 @@ function perform_action_for_single_docker_compose_container { local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - debug_file_info "Perform action for single Docker Compose container" "$file" "$file_dir" "$file_simple_dirname" + debug_file_info "Perform action for single Docker Compose project" "$file" "$file_dir" "$file_simple_dirname" log_delimiter_start 2 "'${file}'" @@ -466,48 +459,35 @@ function perform_action_for_single_docker_compose_container { } down - - case $ACTION in - backup) - backup_docker_compose_folder "$file" - ;; - esac - + create_backup_file_for_single_docker_compose_project "$file" up log_delimiter_end 3 "'${ACTION}'" log_delimiter_end 2 "'${file}'" } -# Performs a specified action for all Docker Compose files in a search directory. -function perform_action_for_all_docker_compose_containers { - log_delimiter_start 1 "DOCKER COMPOSE" - case $ACTION in - backup) - log_debug "Action selected: '${ACTION}'" +function backup_docker_compose_projects { + log_delimiter_start 1 "BACKUP" - local docker_compose_files - docker_compose_files=$(find_docker_compose_files) + log_debug "Action selected: '${ACTION}'" - if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." - else - log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" - fi + local docker_compose_files + docker_compose_files=$(find_docker_compose_files) - while IFS= read -r file; do - perform_action_for_single_docker_compose_container "$file" - done <<<"$docker_compose_files" - ;; - *) - log_error "Invalid action: '${ACTION}'" - ;; - esac - log_delimiter_end 1 "DOCKER COMPOSE" + if [ -z "$docker_compose_files" ]; then + log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." + else + log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" + fi + + while IFS= read -r file; do + backup_single_docker_compose_project "$file" + done <<<"$docker_compose_files" + + log_delimiter_end 1 "BACKUP" } -# Performs a cleanup of the Docker resources -function cleanup { +function clean_docker_environment { function process_preview { log_delimiter_start 2 "PREVIEW" @@ -523,8 +503,8 @@ function cleanup { log_delimiter_end 2 "PREVIEW" } - function process_clean { - log_delimiter_start 2 "CLEAN" + function process_remove { + log_delimiter_start 2 "REMOVE" log_notice "Removing non-running containers..." if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_info "$(docker container prune -f)"; fi @@ -535,13 +515,28 @@ function cleanup { log_notice "Removing unused volumes..." if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_info "$(docker volume prune -f)"; fi - log_delimiter_end 2 "CLEAN" + log_delimiter_end 2 "REMOVE" } - log_delimiter_start 1 "CLEANUP" + log_delimiter_start 1 "CLEAN" process_preview - process_clean - log_delimiter_end 1 "CLEANUP" + process_remove + log_delimiter_end 1 "CLEAN" +} + +function perform_action { + log_debug "Action selected: '${ACTION}'" + case $ACTION in + backup) + backup_docker_compose_projects + ;; + clean) + clean_docker_environment + ;; + *) + log_error "Invalid action: '${ACTION}'" + ;; + esac } # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ @@ -557,19 +552,11 @@ log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." if dry_run_enabled; then log_warn "Dry run is enabled!"; fi validate_search_dir - log_notice "Current directory: '$(pwd)'" get_vars - show_docker_info - -perform_action_for_all_docker_compose_containers - -if $ENABLE_CLEANUP; then - cleanup -fi - +perform_action show_docker_info exit 0 From e08a4e0d2e8ea1ccff8ae7ac49e58d14b2d1e5af Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Fri, 8 Nov 2024 23:36:11 +0100 Subject: [PATCH 16/54] Improve backup logic --- src/domposy.bash | 73 ++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index aebec49..668a276 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -355,6 +355,29 @@ function check_file_creation { fi } +function prepare_backup_dir { + local original_backup_dir="$1" + + if [[ "${original_backup_dir: -1}" != "/" ]]; then + original_backup_dir="${original_backup_dir}/" + log_warn "Backup directory path changed to '${original_backup_dir}'" + fi + + local final_backup_dir + final_backup_dir="${original_backup_dir}$(date +"%Y-%m-%d")/" + + if [[ ! -d "$final_backup_dir" ]]; then + if dry_run_enabled; then + log_dry_run "mkdir -p $final_backup_dir" + else + mkdir -p "$final_backup_dir" || log_error "Backup directory '$(realpath "$final_backup_dir")' could not be created" + log_notice "Backup directory '$(realpath "$final_backup_dir")' was created" + fi + fi + + BACKUP_DIR="$final_backup_dir" +} + # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. function create_backup_file_for_single_docker_compose_project { local file=$1 @@ -365,33 +388,19 @@ function create_backup_file_for_single_docker_compose_project { local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - debug_file_info "Backup Docker Compose folder" "$file" "$file_dir" "$file_simple_dirname" - - local tmp_backup_dir="${BACKUP_DIR}" - - if [[ "${BACKUP_DIR: -1}" != "/" ]]; then - BACKUP_DIR="${BACKUP_DIR}/" - log_warn "BACKUP_DIR: '${tmp_backup_dir}' changed to '${BACKUP_DIR}'" - fi - - BACKUP_DIR="${BACKUP_DIR}$(date +"%Y-%m-%d")/" + debug_file_info "Backup Docker Compose project folder" "$file" "$file_dir" "$file_simple_dirname" - if [[ ! -d "$BACKUP_DIR" ]]; then - if dry_run_enabled; then - log_dry_run "mkdir -p $BACKUP_DIR" - else - mkdir -p "$BACKUP_DIR" - log_notice "Backup directory '$(realpath "$BACKUP_DIR")' was created" - fi - fi + final_backup_dir="$BACKUP_DIR" local tar_file tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" local gz_file="${tar_file}.gz" - local tar_file_with_backup_dir="${BACKUP_DIR}${tar_file}" - local gz_file_with_backup_dir="${BACKUP_DIR}${gz_file}" + local tar_file_with_backup_dir="${final_backup_dir}${tar_file}" + local gz_file_with_backup_dir="${final_backup_dir}${gz_file}" + + log_message_part_for_undoing_file_creations="Skipping further backup actions and undoing file creations." log_notice "TAR..." if dry_run_enabled; then @@ -399,7 +408,7 @@ function create_backup_file_for_single_docker_compose_project { else tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || { - log_warn "Problem while creating the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + log_warn "Problem while creating the tar file '${tar_file_with_backup_dir}'. $log_message_part_for_undoing_file_creations" rm -f "$tar_file_with_backup_dir" return } @@ -412,19 +421,15 @@ function create_backup_file_for_single_docker_compose_project { else gzip "$tar_file_with_backup_dir" || { - log_warn "Problem while compressing the tar file '${tar_file_with_backup_dir}'. Skipping further backup actions and undoing file creations." + log_warn "Problem while compressing the tar file '${tar_file_with_backup_dir}'. $log_message_part_for_undoing_file_creations" rm -f "$tar_file_with_backup_dir" "$gz_file_with_backup_dir" return } fi - check_file_creation "$gz_file_with_backup_dir" - log_notice "'${BACKUP_DIR}'..." - if dry_run_enabled; then log_dry_run "ls -larth $BACKUP_DIR"; else log_info "$(ls -larth "$BACKUP_DIR")"; fi - log_notice "--> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_notice "--> To navigate to the backup folder: 'cd ${BACKUP_DIR}'" + log_notice "--> To navigate to the backup folder: 'cd ${final_backup_dir}'" log_notice "--> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" log_notice "--> To undo gzip: '(sudo) gunzip ${gz_file}'" log_notice "--> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" @@ -446,8 +451,6 @@ function backup_single_docker_compose_project { cd "${file_dir}" || log_error "Failed to change directory to '${file_dir}'" log_notice "Changed directory to '$(pwd)'" - log_delimiter_start 3 "'${ACTION}'" - function down { log_notice "DOWN ('${file_simple_dirname}')..." if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_info "$($DOCKER_COMPOSE_CMD down)"; fi @@ -462,28 +465,32 @@ function backup_single_docker_compose_project { create_backup_file_for_single_docker_compose_project "$file" up - log_delimiter_end 3 "'${ACTION}'" log_delimiter_end 2 "'${file}'" } function backup_docker_compose_projects { log_delimiter_start 1 "BACKUP" - log_debug "Action selected: '${ACTION}'" - local docker_compose_files docker_compose_files=$(find_docker_compose_files) if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform action." + log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform backup." else log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi + prepare_backup_dir "$BACKUP_DIR" + while IFS= read -r file; do backup_single_docker_compose_project "$file" done <<<"$docker_compose_files" + local final_backup_dir="$BACKUP_DIR" + + log_notice "'${final_backup_dir}'..." + if dry_run_enabled; then log_dry_run "ls -larth $final_backup_dir"; else log_info "$(ls -larth "$final_backup_dir")"; fi + log_delimiter_end 1 "BACKUP" } From f4f9cf825f0bbf1608dd8e3ae5f6e5f350dc7df8 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 00:50:12 +0100 Subject: [PATCH 17/54] Improve structure --- src/domposy.bash | 122 ++++++++++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 44 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 668a276..9143f4a 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -264,8 +264,22 @@ function dry_run_enabled { is_true "$ENABLE_DRY_RUN" } -# Validation of the search dir and adjustments (absolute path) if necessary. -function validate_search_dir { +function get_vars { + log_delimiter_start 1 "VARIABLES" + log_notice "Action: '${ACTION}'" + log_notice "Search dir: '${SEARCH_DIR}'" + log_notice "Backup dir: '${BACKUP_DIR}'" + log_notice "Exclude dir: '${EXCLUDE_DIR}'" + log_delimiter_end 1 "VARIABLES" +} + +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ DIRECTORIES ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + +function prepare_search_dir { if [[ "${SEARCH_DIR: -1}" != "/" ]]; then tmp_search_dir="${SEARCH_DIR}" SEARCH_DIR="${SEARCH_DIR}/" @@ -285,17 +299,35 @@ function validate_search_dir { fi } -# Returns the most important variables used by this script. -function get_vars { - log_delimiter_start 1 "VARIABLES" - log_notice "Action: '${ACTION}'" - log_notice "Search dir: '${SEARCH_DIR}'" - log_notice "Backup dir: '${BACKUP_DIR}'" - log_notice "Exclude dir: '${EXCLUDE_DIR}'" - log_delimiter_end 1 "VARIABLES" +function prepare_backup_dir { + local original_backup_dir="$1" + + if [[ "${original_backup_dir: -1}" != "/" ]]; then + original_backup_dir="${original_backup_dir}/" + log_warn "Backup directory path changed to '${original_backup_dir}'" + fi + + local final_backup_dir + final_backup_dir="${original_backup_dir}$(date +"%Y-%m-%d")/" + + if [[ ! -d "$final_backup_dir" ]]; then + if dry_run_enabled; then + log_dry_run "mkdir -p $final_backup_dir" + else + mkdir -p "$final_backup_dir" || log_error "Backup directory '$(realpath "$final_backup_dir")' could not be created" + log_notice "Backup directory '$(realpath "$final_backup_dir")' was created" + fi + fi + + BACKUP_DIR="$final_backup_dir" } -# Outputs information on the Docker status. +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ DOCKER ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + function show_docker_info { log_delimiter_start 1 "DOCKER INFO" log_notice "docker system df..." @@ -312,21 +344,12 @@ function show_docker_info { log_delimiter_end 1 "DOCKER INFO" } -# Searches for Docker Compose files in a specific directory and excludes a specified subdirectory. -function find_docker_compose_files { - local docker_compose_file_names=("${DOCKER_COMPOSE_NAME}.yml" "${DOCKER_COMPOSE_NAME}.yaml") - local docker_compose_files="" - - for name in "${docker_compose_file_names[@]}"; do - files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) - if [ -n "$files" ]; then - docker_compose_files+="$files"$'\n' - fi - done - echo "$docker_compose_files" -} +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ FILES ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ -# Outputs debug information for a file. function debug_file_info { local func_description="$1" local file="$2" @@ -338,7 +361,6 @@ function debug_file_info { [[ -n "$file_simple_dirname" ]] && log_debug "(${func_description}) file simple dirname: '${file_simple_dirname}'" } -# Checks whether a file has been created, if not, the script is cancelled. function check_file_creation { local file=$1 @@ -355,27 +377,27 @@ function check_file_creation { fi } -function prepare_backup_dir { - local original_backup_dir="$1" +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ BACKUP ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ - if [[ "${original_backup_dir: -1}" != "/" ]]; then - original_backup_dir="${original_backup_dir}/" - log_warn "Backup directory path changed to '${original_backup_dir}'" - fi +function find_docker_compose_files { + local docker_compose_file_names=( + "${DOCKER_COMPOSE_NAME}.yml" + "${DOCKER_COMPOSE_NAME}.yaml" + ) - local final_backup_dir - final_backup_dir="${original_backup_dir}$(date +"%Y-%m-%d")/" + local docker_compose_files="" - if [[ ! -d "$final_backup_dir" ]]; then - if dry_run_enabled; then - log_dry_run "mkdir -p $final_backup_dir" - else - mkdir -p "$final_backup_dir" || log_error "Backup directory '$(realpath "$final_backup_dir")' could not be created" - log_notice "Backup directory '$(realpath "$final_backup_dir")' was created" + for name in "${docker_compose_file_names[@]}"; do + files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) + if [ -n "$files" ]; then + docker_compose_files+="$files"$'\n' fi - fi - - BACKUP_DIR="$final_backup_dir" + done + echo "$docker_compose_files" } # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. @@ -494,6 +516,12 @@ function backup_docker_compose_projects { log_delimiter_end 1 "BACKUP" } +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ CLEAN ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + function clean_docker_environment { function process_preview { log_delimiter_start 2 "PREVIEW" @@ -531,6 +559,12 @@ function clean_docker_environment { log_delimiter_end 1 "CLEAN" } +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ ACTIONS ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + function perform_action { log_debug "Action selected: '${ACTION}'" case $ACTION in @@ -558,7 +592,7 @@ log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." if dry_run_enabled; then log_warn "Dry run is enabled!"; fi -validate_search_dir +prepare_search_dir log_notice "Current directory: '$(pwd)'" get_vars From 6cebefecdb7a7d4c03f1bbb1531637ac74fe78e6 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 01:05:22 +0100 Subject: [PATCH 18/54] Replace debug_file_info with log_debug_var --- src/domposy.bash | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 9143f4a..c69e3d7 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -98,6 +98,11 @@ ENABLE_PARENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING=false # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=true +function log_debug_var { + local scope="$1" + log_debug "$scope -> $(print_var_with_current_value "$2")" +} + function log_dry_run { log_info "Dry run is enabled. Skipping '$1'" } @@ -350,21 +355,9 @@ function show_docker_info { # ║ ║ # ╚═════════════════════╩══════════════════════╝ -function debug_file_info { - local func_description="$1" - local file="$2" - local file_dir="$3" - local file_simple_dirname="$4" - - [[ -n "$file" ]] && log_debug "(${func_description}) file: '${file}'" - [[ -n "$file_dir" ]] && log_debug "(${func_description}) file dir: '${file_dir}'" - [[ -n "$file_simple_dirname" ]] && log_debug "(${func_description}) file simple dirname: '${file_simple_dirname}'" -} - function check_file_creation { local file=$1 - - debug_file_info "Check file creation" "$file" + log_debug_var "check_file_creation" "file" if dry_run_enabled; then log_dry_run "ls -larth $file" @@ -410,7 +403,9 @@ function create_backup_file_for_single_docker_compose_project { local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - debug_file_info "Backup Docker Compose project folder" "$file" "$file_dir" "$file_simple_dirname" + log_debug_var "create_backup_file_for_single_docker_compose_project" "file" + log_debug_var "create_backup_file_for_single_docker_compose_project" "file_dir" + log_debug_var "create_backup_file_for_single_docker_compose_project" "file_simple_dirname" final_backup_dir="$BACKUP_DIR" @@ -466,7 +461,9 @@ function backup_single_docker_compose_project { local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - debug_file_info "Perform action for single Docker Compose project" "$file" "$file_dir" "$file_simple_dirname" + log_debug_var "backup_single_docker_compose_project" "file" + log_debug_var "backup_single_docker_compose_project" "file_dir" + log_debug_var "backup_single_docker_compose_project" "file_simple_dirname" log_delimiter_start 2 "'${file}'" @@ -566,8 +563,10 @@ function clean_docker_environment { # ╚═════════════════════╩══════════════════════╝ function perform_action { - log_debug "Action selected: '${ACTION}'" - case $ACTION in + local action=$ACTION + log_debug_var "perform_action" "action" + + case $action in backup) backup_docker_compose_projects ;; @@ -575,7 +574,7 @@ function perform_action { clean_docker_environment ;; *) - log_error "Invalid action: '${ACTION}'" + log_error "Invalid action: '${action}'" ;; esac } From b113066ec8818df03951cbb3d3225e78d39cdc2b Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 01:08:47 +0100 Subject: [PATCH 19/54] Improve log_debug_var usage --- src/domposy.bash | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index c69e3d7..2c8a997 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -396,15 +396,14 @@ function find_docker_compose_files { # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. function create_backup_file_for_single_docker_compose_project { local file=$1 + log_debug_var "create_backup_file_for_single_docker_compose_project" "file" local file_dir file_dir=$(dirname "$file") + log_debug_var "create_backup_file_for_single_docker_compose_project" "file_dir" local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - - log_debug_var "create_backup_file_for_single_docker_compose_project" "file" - log_debug_var "create_backup_file_for_single_docker_compose_project" "file_dir" log_debug_var "create_backup_file_for_single_docker_compose_project" "file_simple_dirname" final_backup_dir="$BACKUP_DIR" @@ -454,15 +453,14 @@ function create_backup_file_for_single_docker_compose_project { function backup_single_docker_compose_project { local file=$1 + log_debug_var "backup_single_docker_compose_project" "file" local file_dir file_dir=$(dirname "$file") + log_debug_var "backup_single_docker_compose_project" "file_dir" local file_simple_dirname file_simple_dirname=$(basename "$(dirname "$file")") - - log_debug_var "backup_single_docker_compose_project" "file" - log_debug_var "backup_single_docker_compose_project" "file_dir" log_debug_var "backup_single_docker_compose_project" "file_simple_dirname" log_delimiter_start 2 "'${file}'" From 0252873bc58c093d15127e38a51a848b25d91f4f Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 01:12:34 +0100 Subject: [PATCH 20/54] Structure changes --- src/domposy.bash | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 2c8a997..9ef9c30 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -355,6 +355,23 @@ function show_docker_info { # ║ ║ # ╚═════════════════════╩══════════════════════╝ +function find_docker_compose_files { + local docker_compose_file_names=( + "${DOCKER_COMPOSE_NAME}.yml" + "${DOCKER_COMPOSE_NAME}.yaml" + ) + + local docker_compose_files="" + + for name in "${docker_compose_file_names[@]}"; do + files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) + if [ -n "$files" ]; then + docker_compose_files+="$files"$'\n' + fi + done + echo "$docker_compose_files" +} + function check_file_creation { local file=$1 log_debug_var "check_file_creation" "file" @@ -376,23 +393,6 @@ function check_file_creation { # ║ ║ # ╚═════════════════════╩══════════════════════╝ -function find_docker_compose_files { - local docker_compose_file_names=( - "${DOCKER_COMPOSE_NAME}.yml" - "${DOCKER_COMPOSE_NAME}.yaml" - ) - - local docker_compose_files="" - - for name in "${docker_compose_file_names[@]}"; do - files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) - if [ -n "$files" ]; then - docker_compose_files+="$files"$'\n' - fi - done - echo "$docker_compose_files" -} - # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. function create_backup_file_for_single_docker_compose_project { local file=$1 From d3ccaf5c3b9f96b7d6774f5f68a0e97bb53f5e96 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 15:44:11 +0100 Subject: [PATCH 21/54] Improve search and backup dir usage --- src/domposy.bash | 57 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 9ef9c30..b81cf23 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -284,46 +284,57 @@ function get_vars { # ║ ║ # ╚═════════════════════╩══════════════════════╝ -function prepare_search_dir { - if [[ "${SEARCH_DIR: -1}" != "/" ]]; then - tmp_search_dir="${SEARCH_DIR}" - SEARCH_DIR="${SEARCH_DIR}/" - log_warn "SEARCH_DIR: '${tmp_search_dir}' changed to '${SEARCH_DIR}'" +function contains_trailing_slash { + if is_var_not_empty "$1" && [[ "${1: -1}" == "/" ]]; then + return 0 fi + return 1 +} - if [[ ! -d "$SEARCH_DIR" ]]; then - log_error "The specified search directory '$SEARCH_DIR' could not be found" - fi +function prepare_search_dir { + local search_dir="$SEARCH_DIR" + + if ! contains_trailing_slash "$search_dir"; then search_dir="${search_dir}/"; fi + if directory_not_exists "$search_dir"; then log_error "The specified search directory '$SEARCH_DIR' could not be found"; fi local absolute_search_dir - absolute_search_dir=$(realpath "$SEARCH_DIR") + absolute_search_dir=$(realpath "$search_dir") - if [[ "$SEARCH_DIR" != "$absolute_search_dir/" ]]; then - log_warn "SEARCH_DIR: '${SEARCH_DIR}' replaced with the absolute path '${absolute_search_dir}/'" - SEARCH_DIR="${absolute_search_dir}/" + if is_var_not_equal "$search_dir" "$absolute_search_dir"; then + log_notice "Replace search directory '${search_dir}' with the absolute path '${absolute_search_dir}'..." + search_dir="$absolute_search_dir" fi + + log_debug_var "prepare_search_dir" "search_dir" + SEARCH_DIR="$search_dir" } function prepare_backup_dir { - local original_backup_dir="$1" + local backup_dir="$BACKUP_DIR" - if [[ "${original_backup_dir: -1}" != "/" ]]; then - original_backup_dir="${original_backup_dir}/" - log_warn "Backup directory path changed to '${original_backup_dir}'" - fi + if ! contains_trailing_slash "$backup_dir"; then backup_dir="${backup_dir}/"; fi local final_backup_dir - final_backup_dir="${original_backup_dir}$(date +"%Y-%m-%d")/" + final_backup_dir="${backup_dir}$(date +"%Y-%m-%d")/" - if [[ ! -d "$final_backup_dir" ]]; then + if directory_not_exists "$final_backup_dir"; then if dry_run_enabled; then log_dry_run "mkdir -p $final_backup_dir" else - mkdir -p "$final_backup_dir" || log_error "Backup directory '$(realpath "$final_backup_dir")' could not be created" - log_notice "Backup directory '$(realpath "$final_backup_dir")' was created" + mkdir -p "$final_backup_dir" || log_error "Backup directory '$final_backup_dir' could not be created" + log_notice "Backup directory '$final_backup_dir' was created" fi fi + local absolute_backup_dir + absolute_backup_dir=$(realpath "$final_backup_dir") + + if is_var_not_equal "$final_backup_dir" "$absolute_backup_dir"; then + log_notice "Replace backup directory '${final_backup_dir}' with the absolute path '${absolute_backup_dir}'..." + final_backup_dir="$absolute_backup_dir" + fi + + log_debug_var "prepare_backup_dir" "final_backup_dir" BACKUP_DIR="$final_backup_dir" } @@ -497,7 +508,8 @@ function backup_docker_compose_projects { log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi - prepare_backup_dir "$BACKUP_DIR" + prepare_search_dir + prepare_backup_dir while IFS= read -r file; do backup_single_docker_compose_project "$file" @@ -589,7 +601,6 @@ log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." if dry_run_enabled; then log_warn "Dry run is enabled!"; fi -prepare_search_dir log_notice "Current directory: '$(pwd)'" get_vars From d29f26d7bab7e7fa004e0cc35e24581a0e9816ce Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 15:54:09 +0100 Subject: [PATCH 22/54] Use simbashlog functions for better readability --- src/domposy.bash | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index b81cf23..bdb4efc 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -376,9 +376,8 @@ function find_docker_compose_files { for name in "${docker_compose_file_names[@]}"; do files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) - if [ -n "$files" ]; then - docker_compose_files+="$files"$'\n' - fi + + if is_var_not_empty "$files"; then docker_compose_files+="$files"$'\n'; fi done echo "$docker_compose_files" } @@ -390,7 +389,7 @@ function check_file_creation { if dry_run_enabled; then log_dry_run "ls -larth $file" else - if [[ -f "$file" ]]; then + if file_exists "$file"; then log_notice "File created: '$file'" else log_error "File creation failed: '$file'" @@ -499,6 +498,8 @@ function backup_single_docker_compose_project { function backup_docker_compose_projects { log_delimiter_start 1 "BACKUP" + prepare_search_dir + local docker_compose_files docker_compose_files=$(find_docker_compose_files) @@ -508,7 +509,6 @@ function backup_docker_compose_projects { log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi - prepare_search_dir prepare_backup_dir while IFS= read -r file; do From 9ba088da29dd239274e5ce70cab3d129fae53365 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 16:04:27 +0100 Subject: [PATCH 23/54] enhance dry-run: disable log file creation in dry-run mode --- src/domposy.bash | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/domposy.bash b/src/domposy.bash index bdb4efc..42d5934 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -74,6 +74,8 @@ source_bin_script "$LOGGER" || # shellcheck disable=SC2034 ENABLE_LOG_FILE=true # shellcheck disable=SC2034 +ENABLE_JSON_LOG_FILE=false +# shellcheck disable=SC2034 ENABLE_LOG_TO_SYSTEM=false # shellcheck disable=SC2034 LOG_DIR="/tmp/simbashlogs/" @@ -215,7 +217,7 @@ while getopts ":hdna:s:b:e" opt; do echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" echo " -h Show help" echo " -d Enables debug logging" - echo " -n Executes a dry run, i.e. no changes are made to the file system with the exception of logging" + echo " -n Executes a dry run, i.e. no changes are made to the file system" echo " -a ACTION ACTION to be performed: 'backup' or 'clean' (Default: '${DEFAULT_ACTION}')" echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" @@ -230,6 +232,13 @@ while getopts ":hdna:s:b:e" opt; do n) log_debug "'-n' selected" ENABLE_DRY_RUN=true + + # shellcheck disable=SC2034 + ENABLE_LOG_FILE=false + # shellcheck disable=SC2034 + ENABLE_JSON_LOG_FILE=false + # shellcheck disable=SC2034 + ENABLE_LOG_TO_SYSTEM=false ;; a) log_debug "'-a' selected: '$OPTARG'" From e947c1395b98250193e3ed95e8a1e543988d6524 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 16:28:23 +0100 Subject: [PATCH 24/54] Fix setup script --- setup.bash | 37 ++++++++++++++++++------------------- src/domposy.bash | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/setup.bash b/setup.bash index 7ab71d5..106530f 100644 --- a/setup.bash +++ b/setup.bash @@ -30,7 +30,7 @@ function install { } function get_app_name_from_repo_url { - "$(basename "$1" .git)" + basename "$1" .git } read -r -p "Do you want to install the $APP_NAME globally ('g') or just for the current user ('u')? (g/u): " bin_dir_choice @@ -47,9 +47,9 @@ function install { app_dir="$OPT_DIR/$app_name" if [ -d "$app_dir" ]; then - echo "$app_name is already installed." + echo "$app_name is already downloaded." else - read -r -p "$app_name is not installed. Do you want to install it? (y/n): " choice + read -r -p "$app_name is not downloaded. Do you want to download it? (y/n): " choice if [[ "$choice" =~ ^[Yy]$ ]]; then echo "Cloning $app_name..." @@ -58,24 +58,24 @@ function install { else git clone "$repo_url" "$app_dir" fi + fi + fi - if [ -f "$app_dir/src/${app_name}.bash" ]; then - chmod +x "$app_dir/src/${app_name}.bash" + if [ -f "$app_dir/src/${app_name}.bash" ]; then + chmod +x "$app_dir/src/${app_name}.bash" - echo "Creating symlink for $app_name..." - ln -sf "$app_dir/src/${app_name}.bash" "$bin_path/$app_name" - echo "$app_name has been installed and is now executable." + echo "Creating symlink for $app_name..." + ln -sf "$app_dir/src/${app_name}.bash" "$bin_path/$app_name" + echo "$app_name has been installed and is now executable." - if [ -n "$(command -v "$app_name")" ]; then - "$app_name" --version - echo "$app_name is working." - else - echo "$app_name is not working." - fi - else - echo "The file ${app_name}.bash was not found in the directory $app_dir/src." - fi + if [ -n "$(command -v "$app_name")" ]; then + "$app_name" --version + echo "$app_name is working." + else + echo "$app_name is not working." fi + else + echo "The file ${app_name}.bash was not found in the directory $app_dir/src." fi done } @@ -99,14 +99,13 @@ function uninstall { if [ -d "$app_dir" ]; then echo "Removing directory $app_dir..." - sudo rm -rf "$app_dir" + rm -rf "$app_dir" else echo "$app_name is not installed." fi done } -# Main menu case "$1" in install) install diff --git a/src/domposy.bash b/src/domposy.bash index 42d5934..5cd578a 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -67,7 +67,7 @@ LOGGER="simbashlog" source_bin_script "$LOGGER" || { - echo "Critical: Unable resolve logger script '$LOGGER'. Exiting..." + echo "Critical: Unable to resolve logger script '$LOGGER'. Exiting..." exit 1 } From 8c4995a1c28eb903d56c6634198f361d0b78e5dd Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 19:38:15 +0100 Subject: [PATCH 25/54] Clear arguments while sourcing process --- src/domposy.bash | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/domposy.bash b/src/domposy.bash index 5cd578a..6b4e1d3 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -45,6 +45,9 @@ function source_bin_script { local script_name="$1" local script_path + original_args=("$@") # Save the original arguments before they are set to nothing + set -- # Clear all arguments + script_path=$(find_bin_script "$script_name") || return 1 # shellcheck source=/dev/null @@ -53,6 +56,8 @@ function source_bin_script { echo "Error: Unable to source script '$script_path'" return 1 } + + set -- "${original_args[@]}" # Restore the original arguments } # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ From b6cd3fb5d9d062a83c09c40b4130ba19498007bc Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 20:01:20 +0100 Subject: [PATCH 26/54] Fix source problems --- setup.bash | 7 ++++++- src/domposy.bash | 28 ++++++++-------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/setup.bash b/setup.bash index 106530f..cf5074b 100644 --- a/setup.bash +++ b/setup.bash @@ -3,9 +3,12 @@ APP_NAME="domposy" APP_BRANCH_NAME="v2" +LOGGER_NAME="simbashlog" +LOGGER_BRANCH_NAME="15-source-causes-problems-when-arguments-are-passed-in-the-main-script" + # NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'! REPO_URLS=( - "https://github.com/fuchs-fabian/simbashlog.git" + "https://github.com/fuchs-fabian/${LOGGER_NAME}.git" "https://github.com/fuchs-fabian/${APP_NAME}.git" ) @@ -55,6 +58,8 @@ function install { if [[ "$app_name" == "$APP_NAME" ]]; then git clone --branch "$APP_BRANCH_NAME" "$repo_url" "$app_dir" + elif [[ "$app_name" == "$LOGGER_NAME" ]]; then + git clone --branch "$LOGGER_BRANCH_NAME" "$repo_url" "$app_dir" else git clone "$repo_url" "$app_dir" fi diff --git a/src/domposy.bash b/src/domposy.bash index 6b4e1d3..018532d 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -41,25 +41,6 @@ function find_bin_script { return 1 } -function source_bin_script { - local script_name="$1" - local script_path - - original_args=("$@") # Save the original arguments before they are set to nothing - set -- # Clear all arguments - - script_path=$(find_bin_script "$script_name") || return 1 - - # shellcheck source=/dev/null - source "$script_path" || - { - echo "Error: Unable to source script '$script_path'" - return 1 - } - - set -- "${original_args[@]}" # Restore the original arguments -} - # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -70,12 +51,19 @@ function source_bin_script { LOGGER="simbashlog" -source_bin_script "$LOGGER" || +ORIGINAL_LOGGER_SCRIPT_PATH=$(find_bin_script "$LOGGER") || { echo "Critical: Unable to resolve logger script '$LOGGER'. Exiting..." exit 1 } +# shellcheck source=/dev/null +source "$ORIGINAL_LOGGER_SCRIPT_PATH" || + { + echo "Critical: Unable to source logger script '$ORIGINAL_LOGGER_SCRIPT_PATH'. Exiting..." + exit 1 + } + # shellcheck disable=SC2034 ENABLE_LOG_FILE=true # shellcheck disable=SC2034 From 1e6e71cd98fde609807188610c5b5e6ef3f2ba2d Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 22:03:15 +0100 Subject: [PATCH 27/54] Add version to arguments --- setup.bash | 2 +- src/domposy.bash | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/setup.bash b/setup.bash index cf5074b..e537094 100644 --- a/setup.bash +++ b/setup.bash @@ -74,7 +74,7 @@ function install { echo "$app_name has been installed and is now executable." if [ -n "$(command -v "$app_name")" ]; then - "$app_name" --version + "$app_name" -v echo "$app_name is working." else echo "$app_name is not working." diff --git a/src/domposy.bash b/src/domposy.bash index 018532d..52b8a05 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -3,6 +3,8 @@ # DESCRIPTION: # This script simplifies your Docker Compose management. +CONST_DOMPOSY_VERSION="2.0.0" + # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -202,13 +204,14 @@ EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" ENABLE_DRY_RUN=false -while getopts ":hdna:s:b:e" opt; do +while getopts ":hvdna:s:b:e" opt; do case ${opt} in h) echo "It is recommended to run the script with root rights to ensure that the backups work properly." echo - echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION [-h] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" + echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION [-h] [-v] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" echo " -h Show help" + echo " -v Show version" echo " -d Enables debug logging" echo " -n Executes a dry run, i.e. no changes are made to the file system" echo " -a ACTION ACTION to be performed: 'backup' or 'clean' (Default: '${DEFAULT_ACTION}')" @@ -217,6 +220,10 @@ while getopts ":hdna:s:b:e" opt; do echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" exit 0 ;; + v) + echo "$CONST_DOMPOSY_VERSION" + exit 0 + ;; d) log_debug "'-d' selected" # shellcheck disable=SC2034 From 18cb564b317fefbb0d92ddde31916a75d23b40f3 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 22:06:46 +0100 Subject: [PATCH 28/54] Change logger branch to main in setup script --- setup.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.bash b/setup.bash index e537094..c662ee0 100644 --- a/setup.bash +++ b/setup.bash @@ -4,7 +4,7 @@ APP_NAME="domposy" APP_BRANCH_NAME="v2" LOGGER_NAME="simbashlog" -LOGGER_BRANCH_NAME="15-source-causes-problems-when-arguments-are-passed-in-the-main-script" +LOGGER_BRANCH_NAME="main" # NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'! REPO_URLS=( From dbb73e603eed83d483d33ad038955a86930e5409 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 22:39:59 +0100 Subject: [PATCH 29/54] Improve logging output --- src/domposy.bash | 70 ++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 52b8a05..ccef375 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -89,7 +89,7 @@ ENABLE_EXITING_SCRIPT_IF_AT_LEAST_ERROR_IS_LOGGED=true # shellcheck disable=SC2034 ENABLE_DATE_IN_CONSOLE_OUTPUTS_FOR_LOGGING=true # shellcheck disable=SC2034 -SHOW_CURRENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING="path" +SHOW_CURRENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING="simple_without_file_extension" # shellcheck disable=SC2034 ENABLE_PARENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING=false # shellcheck disable=SC2034 @@ -101,7 +101,7 @@ function log_debug_var { } function log_dry_run { - log_info "Dry run is enabled. Skipping '$1'" + log_notice "Dry run is enabled. Skipping '$1'" } function log_delimiter { @@ -310,7 +310,7 @@ function prepare_search_dir { absolute_search_dir=$(realpath "$search_dir") if is_var_not_equal "$search_dir" "$absolute_search_dir"; then - log_notice "Replace search directory '${search_dir}' with the absolute path '${absolute_search_dir}'..." + log_info "Replace search directory '${search_dir}' with the absolute path '${absolute_search_dir}'..." search_dir="$absolute_search_dir" fi @@ -339,7 +339,7 @@ function prepare_backup_dir { absolute_backup_dir=$(realpath "$final_backup_dir") if is_var_not_equal "$final_backup_dir" "$absolute_backup_dir"; then - log_notice "Replace backup directory '${final_backup_dir}' with the absolute path '${absolute_backup_dir}'..." + log_info "Replace backup directory '${final_backup_dir}' with the absolute path '${absolute_backup_dir}'..." final_backup_dir="$absolute_backup_dir" fi @@ -355,17 +355,17 @@ function prepare_backup_dir { function show_docker_info { log_delimiter_start 1 "DOCKER INFO" - log_notice "docker system df..." - log_info "$(docker system df)" + log_info "docker system df..." + log_notice "$(docker system df)" - log_notice "docker ps..." - log_info "$(docker ps)" + log_info "docker ps..." + log_notice "$(docker ps)" - log_notice "docker info (formatted)..." - log_info "$(docker info --format "Containers: {{.Containers}} | Running: {{.ContainersRunning}} | Paused: {{.ContainersPaused}} | Stopped: {{.ContainersStopped}} | Images: {{.Images}} | Docker Root Dir: {{.DockerRootDir}}")" + log_info "docker info (formatted)..." + log_notice "$(docker info --format "Containers: {{.Containers}} | Running: {{.ContainersRunning}} | Paused: {{.ContainersPaused}} | Stopped: {{.ContainersStopped}} | Images: {{.Images}} | Docker Root Dir: {{.DockerRootDir}}")" - log_notice "docker images..." - log_info "$(docker images)" + log_info "docker images..." + log_notice "$(docker images)" log_delimiter_end 1 "DOCKER INFO" } @@ -437,7 +437,7 @@ function create_backup_file_for_single_docker_compose_project { log_message_part_for_undoing_file_creations="Skipping further backup actions and undoing file creations." - log_notice "TAR..." + log_info "TAR..." if dry_run_enabled; then log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." else @@ -450,7 +450,7 @@ function create_backup_file_for_single_docker_compose_project { fi check_file_creation "$tar_file_with_backup_dir" - log_notice "GZIP..." + log_info "GZIP..." if dry_run_enabled; then log_dry_run "gzip $tar_file_with_backup_dir" else @@ -463,11 +463,11 @@ function create_backup_file_for_single_docker_compose_project { fi check_file_creation "$gz_file_with_backup_dir" - log_notice "--> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_notice "--> To navigate to the backup folder: 'cd ${final_backup_dir}'" - log_notice "--> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" - log_notice "--> To undo gzip: '(sudo) gunzip ${gz_file}'" - log_notice "--> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" + log_notice "Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." + log_notice "To navigate to the backup folder: 'cd ${final_backup_dir}'" + log_notice "To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" + log_notice "To undo gzip: '(sudo) gunzip ${gz_file}'" + log_notice "To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } function backup_single_docker_compose_project { @@ -488,13 +488,13 @@ function backup_single_docker_compose_project { log_notice "Changed directory to '$(pwd)'" function down { - log_notice "DOWN ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_info "$($DOCKER_COMPOSE_CMD down)"; fi + log_info "DOWN ('${file_simple_dirname}')..." + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_notice "$($DOCKER_COMPOSE_CMD down)"; fi } function up { - log_notice "UP ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_info "$($DOCKER_COMPOSE_CMD up -d)"; fi + log_info "UP ('${file_simple_dirname}')..." + if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi } down @@ -526,8 +526,8 @@ function backup_docker_compose_projects { local final_backup_dir="$BACKUP_DIR" - log_notice "'${final_backup_dir}'..." - if dry_run_enabled; then log_dry_run "ls -larth $final_backup_dir"; else log_info "$(ls -larth "$final_backup_dir")"; fi + log_info "'${final_backup_dir}'..." + if dry_run_enabled; then log_dry_run "ls -larth $final_backup_dir"; else log_notice "$(ls -larth "$final_backup_dir")"; fi log_delimiter_end 1 "BACKUP" } @@ -542,14 +542,14 @@ function clean_docker_environment { function process_preview { log_delimiter_start 2 "PREVIEW" - log_notice "Listing non-running containers..." - log_info "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" + log_info "Listing non-running containers..." + log_notice "$(docker ps -a --filter status=created --filter status=restarting --filter status=paused --filter status=exited --filter status=dead)" - log_notice "Listing unused docker images..." - log_info "$(docker image ls -a --filter dangling=true)" + log_info "Listing unused docker images..." + log_notice "$(docker image ls -a --filter dangling=true)" - log_notice "Listing unused volumes..." - log_info "$(docker volume ls --filter dangling=true)" + log_info "Listing unused volumes..." + log_notice "$(docker volume ls --filter dangling=true)" log_delimiter_end 2 "PREVIEW" } @@ -558,13 +558,13 @@ function clean_docker_environment { log_delimiter_start 2 "REMOVE" log_notice "Removing non-running containers..." - if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_info "$(docker container prune -f)"; fi + if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_notice "$(docker container prune -f)"; fi log_notice "Removing unused docker images..." - if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_info "$(docker image prune -f)"; fi + if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_notice "$(docker image prune -f)"; fi log_notice "Removing unused volumes..." - if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_info "$(docker volume prune -f)"; fi + if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_notice "$(docker volume prune -f)"; fi log_delimiter_end 2 "REMOVE" } @@ -606,7 +606,7 @@ function perform_action { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -log_notice "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." +log_info "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." if dry_run_enabled; then log_warn "Dry run is enabled!"; fi From aad2014bc8a3f62b816204da9a1f8ecb78bb7d96 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 22:48:26 +0100 Subject: [PATCH 30/54] Improve logging output --- src/domposy.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index ccef375..9c02eb0 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -399,7 +399,7 @@ function check_file_creation { log_dry_run "ls -larth $file" else if file_exists "$file"; then - log_notice "File created: '$file'" + log_info "File created: '$file'" else log_error "File creation failed: '$file'" fi @@ -463,11 +463,11 @@ function create_backup_file_for_single_docker_compose_project { fi check_file_creation "$gz_file_with_backup_dir" - log_notice "Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_notice "To navigate to the backup folder: 'cd ${final_backup_dir}'" - log_notice "To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" - log_notice "To undo gzip: '(sudo) gunzip ${gz_file}'" - log_notice "To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" + log_notice ">>> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." + log_notice ">>> To navigate to the backup folder: 'cd ${final_backup_dir}'" + log_notice ">>> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" + log_notice ">>> To undo gzip: '(sudo) gunzip ${gz_file}'" + log_notice ">>> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } function backup_single_docker_compose_project { From 70089460d83181fc27ecb40f3423ed0f8bb44fbd Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 23:09:53 +0100 Subject: [PATCH 31/54] Only run up and down if Docker Compose project is running --- src/domposy.bash | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 9c02eb0..546286f 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -497,9 +497,19 @@ function backup_single_docker_compose_project { if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi } - down + local is_running=false + if $DOCKER_COMPOSE_CMD ps | grep -q "Up"; then + is_running=true + log_debug "Docker-Compose project is currently running" + else + log_debug "Docker-Compose project is not running" + fi + + if $is_running; then down; else log_notice "Skip 'down' because it is not running"; fi + create_backup_file_for_single_docker_compose_project "$file" - up + + if $is_running; then up; else log_notice "Skip 'up' because it was not running"; fi log_delimiter_end 2 "'${file}'" } From 275699810111bb2d8ac252b5d8ea5c49fde96dfc Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 23:11:58 +0100 Subject: [PATCH 32/54] Enhance debug messages to provide clear indication of Docker Compose project's running status --- src/domposy.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 546286f..20b0d16 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -500,9 +500,9 @@ function backup_single_docker_compose_project { local is_running=false if $DOCKER_COMPOSE_CMD ps | grep -q "Up"; then is_running=true - log_debug "Docker-Compose project is currently running" + log_debug "Docker-Compose project '${file}' is currently running" else - log_debug "Docker-Compose project is not running" + log_debug "Docker-Compose project '${file}' is not running" fi if $is_running; then down; else log_notice "Skip 'down' because it is not running"; fi From 46b0c66d91eaaf2c8190f21fd86b7bc01e99be64 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sat, 9 Nov 2024 23:52:22 +0100 Subject: [PATCH 33/54] Improve arg logic --- src/domposy.bash | 240 ++++++++++++++++++++++++----------------------- 1 file changed, 122 insertions(+), 118 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 20b0d16..c152191 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -5,6 +5,16 @@ CONST_DOMPOSY_VERSION="2.0.0" +DEFAULT_ACTION="backup" +DEFAULT_SEARCH_DIR="/home/" +DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" +DEFAULT_EXCLUDE_DIR="tmp" + +ENABLE_DRY_RUN=false + +DOCKER_COMPOSE_NAME="docker-compose" +DOCKER_COMPOSE_CMD="" + # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -141,11 +151,15 @@ function log_delimiter_end { # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ -# ░░ PREPARATIONS ░░ +# ░░ FUNCTIONS ░░ # ░░ ░░ # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +function dry_run_enabled { + is_true "$ENABLE_DRY_RUN" +} + # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. function check_permissions { log_notice "Current user: '$(whoami)'" @@ -176,114 +190,97 @@ function validate_docker_compose_command { log_info "$version_output" } -check_permissions - -DOCKER_COMPOSE_NAME="docker-compose" # Name for Docker Compose files and path components - -DOCKER_COMPOSE_CMD=$(get_docker_compose_command) -log_debug "'${DOCKER_COMPOSE_CMD}' is used" -validate_docker_compose_command - -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -# ░░ ░░ -# ░░ ░░ -# ░░ ARGUMENTS ░░ -# ░░ ░░ -# ░░ ░░ -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ - -DEFAULT_ACTION="backup" -DEFAULT_SEARCH_DIR="/home/" -DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" -DEFAULT_EXCLUDE_DIR="tmp" - -ACTION="${DEFAULT_ACTION}" -SEARCH_DIR="${DEFAULT_SEARCH_DIR}" -BACKUP_DIR="${DEFAULT_BACKUP_DIR}" -EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" - -ENABLE_DRY_RUN=false - -while getopts ":hvdna:s:b:e" opt; do - case ${opt} in - h) - echo "It is recommended to run the script with root rights to ensure that the backups work properly." - echo - echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION [-h] [-v] [-d] [-n] [-a ACTION] [-s SEARCH_DIR] [-b BACKUP_DIR] [-e EXCLUDE_DIR] [-c]" - echo " -h Show help" - echo " -v Show version" - echo " -d Enables debug logging" - echo " -n Executes a dry run, i.e. no changes are made to the file system" - echo " -a ACTION ACTION to be performed: 'backup' or 'clean' (Default: '${DEFAULT_ACTION}')" - echo " -s SEARCH_DIR Directory to search for ${DOCKER_COMPOSE_NAME} files (Default: '${DEFAULT_SEARCH_DIR}')" - echo " -b BACKUP_DIR Destination directory for backups (Default: '${DEFAULT_BACKUP_DIR}')" - echo " -e EXCLUDE_DIR Directory to exclude from search (Default: '${DEFAULT_EXCLUDE_DIR}')" - exit 0 - ;; - v) - echo "$CONST_DOMPOSY_VERSION" - exit 0 - ;; - d) - log_debug "'-d' selected" - # shellcheck disable=SC2034 - LOG_LEVEL=7 - ;; - n) - log_debug "'-n' selected" - ENABLE_DRY_RUN=true - - # shellcheck disable=SC2034 - ENABLE_LOG_FILE=false - # shellcheck disable=SC2034 - ENABLE_JSON_LOG_FILE=false - # shellcheck disable=SC2034 - ENABLE_LOG_TO_SYSTEM=false - ;; - a) - log_debug "'-a' selected: '$OPTARG'" - ACTION="${OPTARG}" - ;; - s) - log_debug "'-s' selected: '$OPTARG'" - SEARCH_DIR="${OPTARG}" - ;; - b) - log_debug "'-b' selected: '$OPTARG'" - BACKUP_DIR="${OPTARG}" - ;; - e) - log_debug "'-e' selected: '$OPTARG'" - EXCLUDE_DIR="${OPTARG}" - ;; - \?) - log_error "Invalid option: -$OPTARG" - ;; - :) - log_error "Option -$OPTARG requires an argument!" - ;; - esac -done -shift $((OPTIND - 1)) - -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -# ░░ ░░ -# ░░ ░░ -# ░░ FUNCTIONS ░░ -# ░░ ░░ -# ░░ ░░ -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ GET ARGUMENTS ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ -function dry_run_enabled { - is_true "$ENABLE_DRY_RUN" -} +_ARG_ACTION="${DEFAULT_ACTION}" +_ARG_SEARCH_DIR="${DEFAULT_SEARCH_DIR}" +_ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" +_ARG_EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" + +function process_arguments { + while getopts ":hvdna:s:b:e" opt; do + case ${opt} in + h) + echo "It is recommended to run the script with root rights to ensure that the backups work properly." + echo + echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION" + echo + echo " -h Show help" + echo + echo " -v Show version" + echo + echo " -d Enables debug logging" + echo + echo " -n Executes a dry run, i.e. no changes are made to the file system" + echo + echo " -a [action Action to be performed: 'backup' or 'clean'" + echo " Default: '${DEFAULT_ACTION}'" + echo + echo " -s [search dir] Directory to search for ${DOCKER_COMPOSE_NAME} files" + echo " Default: '${DEFAULT_SEARCH_DIR}'" + echo + echo " -b [backup dir] Destination directory for backups" + echo " Default: '${DEFAULT_BACKUP_DIR}'" + echo + echo " -e [exclude dir] Directory to exclude from search" + echo " Default: '${DEFAULT_EXCLUDE_DIR}'" + exit 0 + ;; + v) + echo "$CONST_DOMPOSY_VERSION" + exit 0 + ;; + d) + log_debug "'-d' selected" + # shellcheck disable=SC2034 + LOG_LEVEL=7 + ;; + n) + log_debug "'-n' selected" + ENABLE_DRY_RUN=true + + # shellcheck disable=SC2034 + ENABLE_LOG_FILE=false + # shellcheck disable=SC2034 + ENABLE_JSON_LOG_FILE=false + # shellcheck disable=SC2034 + ENABLE_LOG_TO_SYSTEM=false + ;; + a) + log_debug "'-a' selected: '$OPTARG'" + _ARG_ACTION="${OPTARG}" + ;; + s) + log_debug "'-s' selected: '$OPTARG'" + _ARG_SEARCH_DIR="${OPTARG}" + ;; + b) + log_debug "'-b' selected: '$OPTARG'" + _ARG_BACKUP_DIR="${OPTARG}" + ;; + e) + log_debug "'-e' selected: '$OPTARG'" + _ARG_EXCLUDE_DIR="${OPTARG}" + ;; + \?) + log_error "Invalid option: -$OPTARG" + ;; + :) + log_error "Option -$OPTARG requires an argument!" + ;; + esac + done + shift $((OPTIND - 1)) -function get_vars { log_delimiter_start 1 "VARIABLES" - log_notice "Action: '${ACTION}'" - log_notice "Search dir: '${SEARCH_DIR}'" - log_notice "Backup dir: '${BACKUP_DIR}'" - log_notice "Exclude dir: '${EXCLUDE_DIR}'" + log_notice "Action: '${_ARG_ACTION}'" + log_notice "Search dir: '${_ARG_SEARCH_DIR}'" + log_notice "Backup dir: '${_ARG_BACKUP_DIR}'" + log_notice "Exclude dir: '${_ARG_EXCLUDE_DIR}'" log_delimiter_end 1 "VARIABLES" } @@ -301,10 +298,10 @@ function contains_trailing_slash { } function prepare_search_dir { - local search_dir="$SEARCH_DIR" + local search_dir="$_ARG_SEARCH_DIR" if ! contains_trailing_slash "$search_dir"; then search_dir="${search_dir}/"; fi - if directory_not_exists "$search_dir"; then log_error "The specified search directory '$SEARCH_DIR' could not be found"; fi + if directory_not_exists "$search_dir"; then log_error "The specified search directory '$_ARG_SEARCH_DIR' could not be found"; fi local absolute_search_dir absolute_search_dir=$(realpath "$search_dir") @@ -315,11 +312,11 @@ function prepare_search_dir { fi log_debug_var "prepare_search_dir" "search_dir" - SEARCH_DIR="$search_dir" + _ARG_SEARCH_DIR="$search_dir" } function prepare_backup_dir { - local backup_dir="$BACKUP_DIR" + local backup_dir="$_ARG_BACKUP_DIR" if ! contains_trailing_slash "$backup_dir"; then backup_dir="${backup_dir}/"; fi @@ -344,7 +341,7 @@ function prepare_backup_dir { fi log_debug_var "prepare_backup_dir" "final_backup_dir" - BACKUP_DIR="$final_backup_dir" + _ARG_BACKUP_DIR="$final_backup_dir" } # ╔═════════════════════╦══════════════════════╗ @@ -384,7 +381,7 @@ function find_docker_compose_files { local docker_compose_files="" for name in "${docker_compose_file_names[@]}"; do - files=$(find "$SEARCH_DIR" -path "*/${EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) + files=$(find "$_ARG_SEARCH_DIR" -path "*/${_ARG_EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) if is_var_not_empty "$files"; then docker_compose_files+="$files"$'\n'; fi done @@ -425,7 +422,7 @@ function create_backup_file_for_single_docker_compose_project { file_simple_dirname=$(basename "$(dirname "$file")") log_debug_var "create_backup_file_for_single_docker_compose_project" "file_simple_dirname" - final_backup_dir="$BACKUP_DIR" + final_backup_dir="$_ARG_BACKUP_DIR" local tar_file tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" @@ -523,7 +520,7 @@ function backup_docker_compose_projects { docker_compose_files=$(find_docker_compose_files) if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${SEARCH_DIR}'. Cannot perform backup." + log_error "No ${DOCKER_COMPOSE_NAME} files found in '${_ARG_SEARCH_DIR}'. Cannot perform backup." else log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi @@ -534,7 +531,7 @@ function backup_docker_compose_projects { backup_single_docker_compose_project "$file" done <<<"$docker_compose_files" - local final_backup_dir="$BACKUP_DIR" + local final_backup_dir="$_ARG_BACKUP_DIR" log_info "'${final_backup_dir}'..." if dry_run_enabled; then log_dry_run "ls -larth $final_backup_dir"; else log_notice "$(ls -larth "$final_backup_dir")"; fi @@ -592,7 +589,7 @@ function clean_docker_environment { # ╚═════════════════════╩══════════════════════╝ function perform_action { - local action=$ACTION + local action=$_ARG_ACTION log_debug_var "perform_action" "action" case $action in @@ -616,13 +613,20 @@ function perform_action { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -log_info "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." +print_colored_text "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." "cyan" "regular" if dry_run_enabled; then log_warn "Dry run is enabled!"; fi +check_permissions + +DOCKER_COMPOSE_CMD=$(get_docker_compose_command) +log_debug "'${DOCKER_COMPOSE_CMD}' is used" +validate_docker_compose_command + log_notice "Current directory: '$(pwd)'" -get_vars +process_arguments + show_docker_info perform_action show_docker_info From 1477730b6cf22768b10b95b2e80492fc6cffe768 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 00:58:20 +0100 Subject: [PATCH 34/54] Support long options, Closes #11 --- src/domposy.bash | 155 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 109 insertions(+), 46 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index c152191..f1809f2 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -202,45 +202,81 @@ _ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" _ARG_EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" function process_arguments { - while getopts ":hvdna:s:b:e" opt; do - case ${opt} in - h) + local arg_which_is_processed="" + local message_with_help_information="Use '-h' or '--help' for more information." + + function _validate_if_value_is_short_argument { + local value="$1" + if [[ "$value" == "-"* && "$value" != "--"* ]]; then + log_error "'${arg_which_is_processed}': Invalid value ('$value')! Value must not start with '-'." + fi + } + + function _validate_if_value_is_long_argument { + local value="$1" + if [[ "$value" == "--"* ]]; then + log_error "'${arg_which_is_processed}': Invalid value ('$value')! Value must not start with '--'." + fi + } + + function _validate_if_value_is_argument { + local value="$1" + _validate_if_value_is_short_argument "$value" + _validate_if_value_is_long_argument "$value" + } + + local note_for_valid_action_for_backup="Note: '-a, --action' should be used before this, otherwise it has no effect" + local is_action_backup_used=false + function _is_used_without_action_backup { + if is_false "$is_action_backup_used"; then + log_warn "'$arg_which_is_processed': Should only be used if '-a, --action' is used before and set to 'backup', e.g. '... -a backup $arg_which_is_processed ...'." + fi + } + + while [[ $# -gt 0 ]]; do + case $1 in + -h | --help) echo "It is recommended to run the script with root rights to ensure that the backups work properly." echo echo "Usage: (sudo) $CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION" echo - echo " -h Show help" + echo " -h, --help Show help" echo - echo " -v Show version" + echo " -v, --version Show version" echo - echo " -d Enables debug logging" + echo " -d, --debug Enables debug logging" echo - echo " -n Executes a dry run, i.e. no changes are made to the file system" + echo " -n, --dry-run Executes a dry run, i.e. no changes are made to the file system" echo - echo " -a [action Action to be performed: 'backup' or 'clean'" - echo " Default: '${DEFAULT_ACTION}'" + echo " -a, --action [action] Action to be performed" + echo " {backup,clean}" + echo " Default: '${DEFAULT_ACTION}'" echo - echo " -s [search dir] Directory to search for ${DOCKER_COMPOSE_NAME} files" - echo " Default: '${DEFAULT_SEARCH_DIR}'" + echo " --search-dir [search dir] Directory to search for ${DOCKER_COMPOSE_NAME} files" + echo " $note_for_valid_action_for_backup" + echo " Default: '${DEFAULT_SEARCH_DIR}'" echo - echo " -b [backup dir] Destination directory for backups" - echo " Default: '${DEFAULT_BACKUP_DIR}'" + echo " --backup-dir [backup dir] Destination directory for backups" + echo " $note_for_valid_action_for_backup" + echo " Default: '${DEFAULT_BACKUP_DIR}'" echo - echo " -e [exclude dir] Directory to exclude from search" - echo " Default: '${DEFAULT_EXCLUDE_DIR}'" + echo " --exclude-dir [exclude dir] Directory to exclude from search" + echo " $note_for_valid_action_for_backup" + echo " Default: '${DEFAULT_EXCLUDE_DIR}'" exit 0 ;; - v) + -v | --version) echo "$CONST_DOMPOSY_VERSION" exit 0 ;; - d) - log_debug "'-d' selected" + -d | --debug) + log_debug "'$1' selected" + # shellcheck disable=SC2034 LOG_LEVEL=7 ;; - n) - log_debug "'-n' selected" + -n | --dry-run) + log_debug "'$1' selected" ENABLE_DRY_RUN=true # shellcheck disable=SC2034 @@ -250,38 +286,65 @@ function process_arguments { # shellcheck disable=SC2034 ENABLE_LOG_TO_SYSTEM=false ;; - a) - log_debug "'-a' selected: '$OPTARG'" - _ARG_ACTION="${OPTARG}" + -a | --action) + log_debug "'$1' selected" + arg_which_is_processed="$1" + shift + _validate_if_value_is_argument "$1" + case $1 in + backup) + is_action_backup_used=true + ;; + clean) ;; + *) + log_error "Invalid action: '$1'. $message_with_help_information" + ;; + esac + _ARG_ACTION="$1" + log_debug_var "process_arguments" "_ARG_ACTION" ;; - s) - log_debug "'-s' selected: '$OPTARG'" - _ARG_SEARCH_DIR="${OPTARG}" - ;; - b) - log_debug "'-b' selected: '$OPTARG'" - _ARG_BACKUP_DIR="${OPTARG}" + --search-dir) + log_debug "'$1' selected" + arg_which_is_processed="$1" + _is_used_without_action_backup + shift + _validate_if_value_is_argument "$1" + + if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi + + _ARG_SEARCH_DIR="$1" + log_debug_var "process_arguments" "_ARG_SEARCH_DIR" ;; - e) - log_debug "'-e' selected: '$OPTARG'" - _ARG_EXCLUDE_DIR="${OPTARG}" + --backup-dir) + log_debug "'$1' selected" + arg_which_is_processed="$1" + _is_used_without_action_backup + shift + _validate_if_value_is_argument "$1" + + if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi + + _ARG_BACKUP_DIR="$1" + log_debug_var "process_arguments" "_ARG_BACKUP_DIR" ;; - \?) - log_error "Invalid option: -$OPTARG" + --exclude-dir) + log_debug "'$1' selected" + arg_which_is_processed="$1" + _is_used_without_action_backup + shift + _validate_if_value_is_argument "$1" + + if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you do not want to exclude any directory do not use this option."; fi + + _ARG_EXCLUDE_DIR="$1" + log_debug_var "process_arguments" "_ARG_EXCLUDE_DIR" ;; - :) - log_error "Option -$OPTARG requires an argument!" + *) + log_error "Invalid argument: '$1'. $message_with_help_information" ;; esac + shift done - shift $((OPTIND - 1)) - - log_delimiter_start 1 "VARIABLES" - log_notice "Action: '${_ARG_ACTION}'" - log_notice "Search dir: '${_ARG_SEARCH_DIR}'" - log_notice "Backup dir: '${_ARG_BACKUP_DIR}'" - log_notice "Exclude dir: '${_ARG_EXCLUDE_DIR}'" - log_delimiter_end 1 "VARIABLES" } # ╔═════════════════════╦══════════════════════╗ @@ -625,7 +688,7 @@ validate_docker_compose_command log_notice "Current directory: '$(pwd)'" -process_arguments +process_arguments "$@" show_docker_info perform_action From 0758a21be92eafd1399ab2fe300f0e0c11081d10 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 01:03:45 +0100 Subject: [PATCH 35/54] Fix issues with handling specific options --- src/domposy.bash | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index f1809f2..cbe2744 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -263,10 +263,16 @@ function process_arguments { echo " --exclude-dir [exclude dir] Directory to exclude from search" echo " $note_for_valid_action_for_backup" echo " Default: '${DEFAULT_EXCLUDE_DIR}'" + + # shellcheck disable=SC2034 + ENABLE_SUMMARY_ON_EXIT=false exit 0 ;; -v | --version) echo "$CONST_DOMPOSY_VERSION" + + # shellcheck disable=SC2034 + ENABLE_SUMMARY_ON_EXIT=false exit 0 ;; -d | --debug) @@ -285,6 +291,8 @@ function process_arguments { ENABLE_JSON_LOG_FILE=false # shellcheck disable=SC2034 ENABLE_LOG_TO_SYSTEM=false + # shellcheck disable=SC2034 + ENABLE_SUMMARY_ON_EXIT=false ;; -a | --action) log_debug "'$1' selected" @@ -678,6 +686,8 @@ function perform_action { print_colored_text "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." "cyan" "regular" +process_arguments "$@" + if dry_run_enabled; then log_warn "Dry run is enabled!"; fi check_permissions @@ -688,8 +698,6 @@ validate_docker_compose_command log_notice "Current directory: '$(pwd)'" -process_arguments "$@" - show_docker_info perform_action show_docker_info From e322f858f2645f1b3ee638b3ca3109eea6b3baa4 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 01:08:23 +0100 Subject: [PATCH 36/54] Suppress console output when sourcing logger script and remove console output when script has started --- src/domposy.bash | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index cbe2744..c028450 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -70,7 +70,7 @@ ORIGINAL_LOGGER_SCRIPT_PATH=$(find_bin_script "$LOGGER") || } # shellcheck source=/dev/null -source "$ORIGINAL_LOGGER_SCRIPT_PATH" || +source "$ORIGINAL_LOGGER_SCRIPT_PATH" >/dev/null 2>&1 || { echo "Critical: Unable to source logger script '$ORIGINAL_LOGGER_SCRIPT_PATH'. Exiting..." exit 1 @@ -684,8 +684,6 @@ function perform_action { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -print_colored_text "'$CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION' has started." "cyan" "regular" - process_arguments "$@" if dry_run_enabled; then log_warn "Dry run is enabled!"; fi From 4a10929b6667030c6127dc5ced0963ab4b2706e6 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 01:17:40 +0100 Subject: [PATCH 37/54] Bugfix for global variables --- src/domposy.bash | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index c028450..460017d 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -5,16 +5,6 @@ CONST_DOMPOSY_VERSION="2.0.0" -DEFAULT_ACTION="backup" -DEFAULT_SEARCH_DIR="/home/" -DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" -DEFAULT_EXCLUDE_DIR="tmp" - -ENABLE_DRY_RUN=false - -DOCKER_COMPOSE_NAME="docker-compose" -DOCKER_COMPOSE_CMD="" - # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -148,6 +138,24 @@ function log_delimiter_end { log_delimiter "$1" "$2" "<" false } +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ VARIABLES ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +DEFAULT_ACTION="backup" +DEFAULT_SEARCH_DIR="/home/" +DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" +DEFAULT_EXCLUDE_DIR="tmp" + +ENABLE_DRY_RUN=false + +DOCKER_COMPOSE_NAME="docker-compose" +DOCKER_COMPOSE_CMD="" + # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ From 333f3379c0c5516b33b375c14a55d9c63ab56c23 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 01:34:26 +0100 Subject: [PATCH 38/54] Bugfixes --- src/domposy.bash | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 460017d..e388516 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -383,7 +383,7 @@ function prepare_search_dir { if directory_not_exists "$search_dir"; then log_error "The specified search directory '$_ARG_SEARCH_DIR' could not be found"; fi local absolute_search_dir - absolute_search_dir=$(realpath "$search_dir") + absolute_search_dir="$(realpath "$search_dir")/" if is_var_not_equal "$search_dir" "$absolute_search_dir"; then log_info "Replace search directory '${search_dir}' with the absolute path '${absolute_search_dir}'..." @@ -412,7 +412,7 @@ function prepare_backup_dir { fi local absolute_backup_dir - absolute_backup_dir=$(realpath "$final_backup_dir") + absolute_backup_dir="$(realpath "$final_backup_dir")/" if is_var_not_equal "$final_backup_dir" "$absolute_backup_dir"; then log_info "Replace backup directory '${final_backup_dir}' with the absolute path '${absolute_backup_dir}'..." @@ -678,9 +678,6 @@ function perform_action { clean) clean_docker_environment ;; - *) - log_error "Invalid action: '${action}'" - ;; esac } From bbef2e631d42383dbff206ebe6b672df3a124772 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 17:00:32 +0100 Subject: [PATCH 39/54] Refactoring --- src/domposy.bash | 328 +++++++++++++++++++++++------------------------ 1 file changed, 157 insertions(+), 171 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index e388516..6565db4 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -8,7 +8,7 @@ CONST_DOMPOSY_VERSION="2.0.0" # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ -# ░░ SOURCING HELPER ░░ +# ░░ GENERAL UTILS ░░ # ░░ ░░ # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ @@ -46,7 +46,7 @@ function find_bin_script { # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ -# ░░ LOGGING HELPER ░░ +# ░░ LOGGING ░░ # ░░ ░░ # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ @@ -146,28 +146,59 @@ function log_delimiter_end { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -DEFAULT_ACTION="backup" -DEFAULT_SEARCH_DIR="/home/" -DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" -DEFAULT_EXCLUDE_DIR="tmp" - ENABLE_DRY_RUN=false DOCKER_COMPOSE_NAME="docker-compose" DOCKER_COMPOSE_CMD="" +DEFAULT_ACTION="backup" + +DEFAULT_SEARCH_DIR="/home/" +DEFAULT_EXCLUDE_DIR="tmp" +DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" + # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ -# ░░ FUNCTIONS ░░ +# ░░ UTILS ░░ # ░░ ░░ # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -function dry_run_enabled { +function is_dry_run_enabled { is_true "$ENABLE_DRY_RUN" } +function contains_trailing_slash { + if is_var_not_empty "$1" && [[ "${1: -1}" == "/" ]]; then + return 0 + fi + return 1 +} + +function check_file_creation { + local file=$1 + log_debug_var "check_file_creation" "file" + + if is_dry_run_enabled; then + log_dry_run "ls -larth $file" + else + if file_exists "$file"; then + log_info "File created: '$file'" + else + log_error "File creation failed: '$file'" + fi + fi +} + +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ LOGIC ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. function check_permissions { log_notice "Current user: '$(whoami)'" @@ -180,24 +211,6 @@ function check_permissions { fi } -# Returns the Docker Compose command. So whether 'docker-compose' or 'docker compose'. -function get_docker_compose_command { - if command -v docker-compose &>/dev/null; then - echo "docker-compose" - elif docker compose version &>/dev/null; then - echo "docker compose" - else - log_error "Neither 'docker-compose' nor 'docker compose' command found. Is it installed?" - fi -} - -# Validates whether the docker compose command can also be executed by determining the version. -function validate_docker_compose_command { - local version_output - version_output=$($DOCKER_COMPOSE_CMD version 2>&1) || log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" - log_info "$version_output" -} - # ╔═════════════════════╦══════════════════════╗ # ║ ║ # ║ GET ARGUMENTS ║ @@ -205,9 +218,10 @@ function validate_docker_compose_command { # ╚═════════════════════╩══════════════════════╝ _ARG_ACTION="${DEFAULT_ACTION}" + _ARG_SEARCH_DIR="${DEFAULT_SEARCH_DIR}" -_ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" _ARG_EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" +_ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" function process_arguments { local arg_which_is_processed="" @@ -264,13 +278,13 @@ function process_arguments { echo " $note_for_valid_action_for_backup" echo " Default: '${DEFAULT_SEARCH_DIR}'" echo - echo " --backup-dir [backup dir] Destination directory for backups" - echo " $note_for_valid_action_for_backup" - echo " Default: '${DEFAULT_BACKUP_DIR}'" - echo echo " --exclude-dir [exclude dir] Directory to exclude from search" echo " $note_for_valid_action_for_backup" echo " Default: '${DEFAULT_EXCLUDE_DIR}'" + echo + echo " --backup-dir [backup dir] Destination directory for backups" + echo " $note_for_valid_action_for_backup" + echo " Default: '${DEFAULT_BACKUP_DIR}'" # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=false @@ -301,6 +315,8 @@ function process_arguments { ENABLE_LOG_TO_SYSTEM=false # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=false + + log_warn "Dry run is enabled!" ;; -a | --action) log_debug "'$1' selected" @@ -331,29 +347,29 @@ function process_arguments { _ARG_SEARCH_DIR="$1" log_debug_var "process_arguments" "_ARG_SEARCH_DIR" ;; - --backup-dir) + --exclude-dir) log_debug "'$1' selected" arg_which_is_processed="$1" _is_used_without_action_backup shift _validate_if_value_is_argument "$1" - if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi + if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you do not want to exclude any directory do not use this option."; fi - _ARG_BACKUP_DIR="$1" - log_debug_var "process_arguments" "_ARG_BACKUP_DIR" + _ARG_EXCLUDE_DIR="$1" + log_debug_var "process_arguments" "_ARG_EXCLUDE_DIR" ;; - --exclude-dir) + --backup-dir) log_debug "'$1' selected" arg_which_is_processed="$1" _is_used_without_action_backup shift _validate_if_value_is_argument "$1" - if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you do not want to exclude any directory do not use this option."; fi + if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi - _ARG_EXCLUDE_DIR="$1" - log_debug_var "process_arguments" "_ARG_EXCLUDE_DIR" + _ARG_BACKUP_DIR="$1" + log_debug_var "process_arguments" "_ARG_BACKUP_DIR" ;; *) log_error "Invalid argument: '$1'. $message_with_help_information" @@ -363,66 +379,6 @@ function process_arguments { done } -# ╔═════════════════════╦══════════════════════╗ -# ║ ║ -# ║ DIRECTORIES ║ -# ║ ║ -# ╚═════════════════════╩══════════════════════╝ - -function contains_trailing_slash { - if is_var_not_empty "$1" && [[ "${1: -1}" == "/" ]]; then - return 0 - fi - return 1 -} - -function prepare_search_dir { - local search_dir="$_ARG_SEARCH_DIR" - - if ! contains_trailing_slash "$search_dir"; then search_dir="${search_dir}/"; fi - if directory_not_exists "$search_dir"; then log_error "The specified search directory '$_ARG_SEARCH_DIR' could not be found"; fi - - local absolute_search_dir - absolute_search_dir="$(realpath "$search_dir")/" - - if is_var_not_equal "$search_dir" "$absolute_search_dir"; then - log_info "Replace search directory '${search_dir}' with the absolute path '${absolute_search_dir}'..." - search_dir="$absolute_search_dir" - fi - - log_debug_var "prepare_search_dir" "search_dir" - _ARG_SEARCH_DIR="$search_dir" -} - -function prepare_backup_dir { - local backup_dir="$_ARG_BACKUP_DIR" - - if ! contains_trailing_slash "$backup_dir"; then backup_dir="${backup_dir}/"; fi - - local final_backup_dir - final_backup_dir="${backup_dir}$(date +"%Y-%m-%d")/" - - if directory_not_exists "$final_backup_dir"; then - if dry_run_enabled; then - log_dry_run "mkdir -p $final_backup_dir" - else - mkdir -p "$final_backup_dir" || log_error "Backup directory '$final_backup_dir' could not be created" - log_notice "Backup directory '$final_backup_dir' was created" - fi - fi - - local absolute_backup_dir - absolute_backup_dir="$(realpath "$final_backup_dir")/" - - if is_var_not_equal "$final_backup_dir" "$absolute_backup_dir"; then - log_info "Replace backup directory '${final_backup_dir}' with the absolute path '${absolute_backup_dir}'..." - final_backup_dir="$absolute_backup_dir" - fi - - log_debug_var "prepare_backup_dir" "final_backup_dir" - _ARG_BACKUP_DIR="$final_backup_dir" -} - # ╔═════════════════════╦══════════════════════╗ # ║ ║ # ║ DOCKER ║ @@ -445,13 +401,33 @@ function show_docker_info { log_delimiter_end 1 "DOCKER INFO" } -# ╔═════════════════════╦══════════════════════╗ -# ║ ║ -# ║ FILES ║ -# ║ ║ -# ╚═════════════════════╩══════════════════════╝ +# Returns the Docker Compose command. So whether 'docker-compose' or 'docker compose'. +function get_docker_compose_command { + if command -v docker-compose &>/dev/null; then + echo "docker-compose" + elif docker compose version &>/dev/null; then + echo "docker compose" + else + log_error "Neither 'docker-compose' nor 'docker compose' command found. Is it installed?" + fi +} + +function _set_docker_compose_cmd { + DOCKER_COMPOSE_CMD=$(get_docker_compose_command) + log_debug "'${DOCKER_COMPOSE_CMD}' is used" + + local version_output + version_output=$($DOCKER_COMPOSE_CMD version 2>&1) || log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" + log_info "$version_output" +} function find_docker_compose_files { + local search_dir="$1" + log_debug_var "find_docker_compose_files" "search_dir" + + local exclude_dir="$2" + log_debug_var "find_docker_compose_files" "exclude_dir" + local docker_compose_file_names=( "${DOCKER_COMPOSE_NAME}.yml" "${DOCKER_COMPOSE_NAME}.yaml" @@ -460,28 +436,13 @@ function find_docker_compose_files { local docker_compose_files="" for name in "${docker_compose_file_names[@]}"; do - files=$(find "$_ARG_SEARCH_DIR" -path "*/${_ARG_EXCLUDE_DIR}/*" -prune -o -name "$name" -print 2>/dev/null) + files=$(find "$search_dir" -path "*/${exclude_dir}/*" -prune -o -name "$name" -print 2>/dev/null) if is_var_not_empty "$files"; then docker_compose_files+="$files"$'\n'; fi done echo "$docker_compose_files" } -function check_file_creation { - local file=$1 - log_debug_var "check_file_creation" "file" - - if dry_run_enabled; then - log_dry_run "ls -larth $file" - else - if file_exists "$file"; then - log_info "File created: '$file'" - else - log_error "File creation failed: '$file'" - fi - fi -} - # ╔═════════════════════╦══════════════════════╗ # ║ ║ # ║ BACKUP ║ @@ -490,7 +451,10 @@ function check_file_creation { # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. function create_backup_file_for_single_docker_compose_project { - local file=$1 + local backup_dir="$1" + log_debug_var "create_backup_file_for_single_docker_compose_project" "backup_dir" + + local file=$2 log_debug_var "create_backup_file_for_single_docker_compose_project" "file" local file_dir @@ -498,23 +462,21 @@ function create_backup_file_for_single_docker_compose_project { log_debug_var "create_backup_file_for_single_docker_compose_project" "file_dir" local file_simple_dirname - file_simple_dirname=$(basename "$(dirname "$file")") + file_simple_dirname=$(basename "$file_dir") log_debug_var "create_backup_file_for_single_docker_compose_project" "file_simple_dirname" - final_backup_dir="$_ARG_BACKUP_DIR" - local tar_file tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" local gz_file="${tar_file}.gz" - local tar_file_with_backup_dir="${final_backup_dir}${tar_file}" - local gz_file_with_backup_dir="${final_backup_dir}${gz_file}" + local tar_file_with_backup_dir="${backup_dir}${tar_file}" + local gz_file_with_backup_dir="${backup_dir}${gz_file}" log_message_part_for_undoing_file_creations="Skipping further backup actions and undoing file creations." log_info "TAR..." - if dry_run_enabled; then + if is_dry_run_enabled; then log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." else tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || @@ -527,7 +489,7 @@ function create_backup_file_for_single_docker_compose_project { check_file_creation "$tar_file_with_backup_dir" log_info "GZIP..." - if dry_run_enabled; then + if is_dry_run_enabled; then log_dry_run "gzip $tar_file_with_backup_dir" else gzip "$tar_file_with_backup_dir" || @@ -540,14 +502,17 @@ function create_backup_file_for_single_docker_compose_project { check_file_creation "$gz_file_with_backup_dir" log_notice ">>> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." - log_notice ">>> To navigate to the backup folder: 'cd ${final_backup_dir}'" + log_notice ">>> To navigate to the backup directory: 'cd ${backup_dir}'" log_notice ">>> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" log_notice ">>> To undo gzip: '(sudo) gunzip ${gz_file}'" log_notice ">>> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } function backup_single_docker_compose_project { - local file=$1 + local backup_dir="$1" + log_debug_var "backup_single_docker_compose_project" "backup_dir" + + local file=$2 log_debug_var "backup_single_docker_compose_project" "file" local file_dir @@ -555,7 +520,7 @@ function backup_single_docker_compose_project { log_debug_var "backup_single_docker_compose_project" "file_dir" local file_simple_dirname - file_simple_dirname=$(basename "$(dirname "$file")") + file_simple_dirname=$(basename "$file_dir") log_debug_var "backup_single_docker_compose_project" "file_simple_dirname" log_delimiter_start 2 "'${file}'" @@ -565,12 +530,12 @@ function backup_single_docker_compose_project { function down { log_info "DOWN ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_notice "$($DOCKER_COMPOSE_CMD down)"; fi + if is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_notice "$($DOCKER_COMPOSE_CMD down)"; fi } function up { log_info "UP ('${file_simple_dirname}')..." - if dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi + if is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi } local is_running=false @@ -583,7 +548,7 @@ function backup_single_docker_compose_project { if $is_running; then down; else log_notice "Skip 'down' because it is not running"; fi - create_backup_file_for_single_docker_compose_project "$file" + create_backup_file_for_single_docker_compose_project "$backup_dir" "$file" if $is_running; then up; else log_notice "Skip 'up' because it was not running"; fi @@ -591,29 +556,65 @@ function backup_single_docker_compose_project { } function backup_docker_compose_projects { + local search_dir="$1" + local exclude_dir="$2" + local backup_dir="$3" + + function prepare_search_dir { + if ! contains_trailing_slash "$search_dir"; then search_dir="${search_dir}/"; fi + + if directory_not_exists "$search_dir"; then log_error "The specified search directory '$search_dir' could not be found"; fi + + local absolute_search_dir + absolute_search_dir="$(realpath "$search_dir")/" + + if is_var_not_equal "$search_dir" "$absolute_search_dir"; then search_dir="$absolute_search_dir"; fi + } + + function prepare_backup_dir { + if ! contains_trailing_slash "$backup_dir"; then backup_dir="${backup_dir}/"; fi + + backup_dir="${backup_dir}$(date +"%Y-%m-%d")/" + + if directory_not_exists "$backup_dir"; then + if is_dry_run_enabled; then + log_dry_run "mkdir -p $backup_dir" + else + mkdir -p "$backup_dir" || log_error "Backup directory '$backup_dir' could not be created" + log_notice "Backup directory '$backup_dir' was created" + fi + fi + + local absolute_backup_dir + absolute_backup_dir="$(realpath "$backup_dir")/" + + if is_var_not_equal "$backup_dir" "$absolute_backup_dir"; then backup_dir="$absolute_backup_dir"; fi + } + log_delimiter_start 1 "BACKUP" prepare_search_dir + log_debug_var "backup_docker_compose_projects" "search_dir" + log_debug_var "backup_docker_compose_projects" "exclude_dir" local docker_compose_files - docker_compose_files=$(find_docker_compose_files) + docker_compose_files=$(find_docker_compose_files "$search_dir" "$exclude_dir") if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${_ARG_SEARCH_DIR}'. Cannot perform backup." + log_error "No ${DOCKER_COMPOSE_NAME} files found in '${search_dir}'. Cannot perform backup." else log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi prepare_backup_dir + log_debug_var "backup_docker_compose_projects" "backup_dir" while IFS= read -r file; do - backup_single_docker_compose_project "$file" + backup_single_docker_compose_project "$backup_dir" "$file" done <<<"$docker_compose_files" - local final_backup_dir="$_ARG_BACKUP_DIR" - - log_info "'${final_backup_dir}'..." - if dry_run_enabled; then log_dry_run "ls -larth $final_backup_dir"; else log_notice "$(ls -larth "$final_backup_dir")"; fi + log_info "'${backup_dir}'..." + if is_dry_run_enabled; then log_dry_run "ls -larth $backup_dir"; else log_notice "$(ls -larth "$backup_dir")"; fi log_delimiter_end 1 "BACKUP" } @@ -644,13 +645,13 @@ function clean_docker_environment { log_delimiter_start 2 "REMOVE" log_notice "Removing non-running containers..." - if dry_run_enabled; then log_dry_run "docker container prune -f"; else log_notice "$(docker container prune -f)"; fi + if is_dry_run_enabled; then log_dry_run "docker container prune -f"; else log_notice "$(docker container prune -f)"; fi log_notice "Removing unused docker images..." - if dry_run_enabled; then log_dry_run "docker image prune -f"; else log_notice "$(docker image prune -f)"; fi + if is_dry_run_enabled; then log_dry_run "docker image prune -f"; else log_notice "$(docker image prune -f)"; fi log_notice "Removing unused volumes..." - if dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_notice "$(docker volume prune -f)"; fi + if is_dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_notice "$(docker volume prune -f)"; fi log_delimiter_end 2 "REMOVE" } @@ -661,26 +662,6 @@ function clean_docker_environment { log_delimiter_end 1 "CLEAN" } -# ╔═════════════════════╦══════════════════════╗ -# ║ ║ -# ║ ACTIONS ║ -# ║ ║ -# ╚═════════════════════╩══════════════════════╝ - -function perform_action { - local action=$_ARG_ACTION - log_debug_var "perform_action" "action" - - case $action in - backup) - backup_docker_compose_projects - ;; - clean) - clean_docker_environment - ;; - esac -} - # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -691,18 +672,23 @@ function perform_action { process_arguments "$@" -if dry_run_enabled; then log_warn "Dry run is enabled!"; fi - check_permissions -DOCKER_COMPOSE_CMD=$(get_docker_compose_command) -log_debug "'${DOCKER_COMPOSE_CMD}' is used" -validate_docker_compose_command +_set_docker_compose_cmd log_notice "Current directory: '$(pwd)'" show_docker_info -perform_action + +case $_ARG_ACTION in +backup) + backup_docker_compose_projects "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" + ;; +clean) + clean_docker_environment + ;; +esac + show_docker_info exit 0 From 0b367598e10c60812cf2de7c0bc97a428af8067f Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 17:07:52 +0100 Subject: [PATCH 40/54] Bugfix --- src/domposy.bash | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 6565db4..428ba69 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -423,10 +423,7 @@ function _set_docker_compose_cmd { function find_docker_compose_files { local search_dir="$1" - log_debug_var "find_docker_compose_files" "search_dir" - local exclude_dir="$2" - log_debug_var "find_docker_compose_files" "exclude_dir" local docker_compose_file_names=( "${DOCKER_COMPOSE_NAME}.yml" @@ -454,7 +451,7 @@ function create_backup_file_for_single_docker_compose_project { local backup_dir="$1" log_debug_var "create_backup_file_for_single_docker_compose_project" "backup_dir" - local file=$2 + local file="$2" log_debug_var "create_backup_file_for_single_docker_compose_project" "file" local file_dir @@ -512,7 +509,7 @@ function backup_single_docker_compose_project { local backup_dir="$1" log_debug_var "backup_single_docker_compose_project" "backup_dir" - local file=$2 + local file="$2" log_debug_var "backup_single_docker_compose_project" "file" local file_dir From 1a014f2d55b129a888bcf378a949cf66be0e6670 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 17:41:24 +0100 Subject: [PATCH 41/54] Add underscore for private functions --- src/domposy.bash | 92 ++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 428ba69..a29911d 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -165,7 +165,7 @@ DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_back # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -function is_dry_run_enabled { +function _is_dry_run_enabled { is_true "$ENABLE_DRY_RUN" } @@ -180,7 +180,7 @@ function check_file_creation { local file=$1 log_debug_var "check_file_creation" "file" - if is_dry_run_enabled; then + if _is_dry_run_enabled; then log_dry_run "ls -larth $file" else if file_exists "$file"; then @@ -200,7 +200,7 @@ function check_file_creation { # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. -function check_permissions { +function _check_permissions { log_notice "Current user: '$(whoami)'" if [[ $(id -u) -ne 0 ]]; then if groups "$(whoami)" | grep -q '\bdocker\b'; then @@ -223,7 +223,7 @@ _ARG_SEARCH_DIR="${DEFAULT_SEARCH_DIR}" _ARG_EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" _ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" -function process_arguments { +function _process_arguments { local arg_which_is_processed="" local message_with_help_information="Use '-h' or '--help' for more information." @@ -333,7 +333,7 @@ function process_arguments { ;; esac _ARG_ACTION="$1" - log_debug_var "process_arguments" "_ARG_ACTION" + log_debug_var "_process_arguments" "_ARG_ACTION" ;; --search-dir) log_debug "'$1' selected" @@ -345,7 +345,7 @@ function process_arguments { if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi _ARG_SEARCH_DIR="$1" - log_debug_var "process_arguments" "_ARG_SEARCH_DIR" + log_debug_var "_process_arguments" "_ARG_SEARCH_DIR" ;; --exclude-dir) log_debug "'$1' selected" @@ -357,7 +357,7 @@ function process_arguments { if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you do not want to exclude any directory do not use this option."; fi _ARG_EXCLUDE_DIR="$1" - log_debug_var "process_arguments" "_ARG_EXCLUDE_DIR" + log_debug_var "_process_arguments" "_ARG_EXCLUDE_DIR" ;; --backup-dir) log_debug "'$1' selected" @@ -369,7 +369,7 @@ function process_arguments { if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi _ARG_BACKUP_DIR="$1" - log_debug_var "process_arguments" "_ARG_BACKUP_DIR" + log_debug_var "_process_arguments" "_ARG_BACKUP_DIR" ;; *) log_error "Invalid argument: '$1'. $message_with_help_information" @@ -421,7 +421,7 @@ function _set_docker_compose_cmd { log_info "$version_output" } -function find_docker_compose_files { +function _find_docker_compose_files { local search_dir="$1" local exclude_dir="$2" @@ -447,20 +447,20 @@ function find_docker_compose_files { # ╚═════════════════════╩══════════════════════╝ # Creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. -function create_backup_file_for_single_docker_compose_project { +function _create_backup_file_for_single_docker_compose_project { local backup_dir="$1" - log_debug_var "create_backup_file_for_single_docker_compose_project" "backup_dir" + log_debug_var "_create_backup_file_for_single_docker_compose_project" "backup_dir" local file="$2" - log_debug_var "create_backup_file_for_single_docker_compose_project" "file" + log_debug_var "_create_backup_file_for_single_docker_compose_project" "file" local file_dir file_dir=$(dirname "$file") - log_debug_var "create_backup_file_for_single_docker_compose_project" "file_dir" + log_debug_var "_create_backup_file_for_single_docker_compose_project" "file_dir" local file_simple_dirname file_simple_dirname=$(basename "$file_dir") - log_debug_var "create_backup_file_for_single_docker_compose_project" "file_simple_dirname" + log_debug_var "_create_backup_file_for_single_docker_compose_project" "file_simple_dirname" local tar_file tar_file="$(date +"%Y-%m-%d_%H-%M-%S")_backup_${file_simple_dirname}.tar" @@ -473,7 +473,7 @@ function create_backup_file_for_single_docker_compose_project { log_message_part_for_undoing_file_creations="Skipping further backup actions and undoing file creations." log_info "TAR..." - if is_dry_run_enabled; then + if _is_dry_run_enabled; then log_dry_run "tar -cpf $tar_file_with_backup_dir -C $file_dir ." else tar -cpf "$tar_file_with_backup_dir" -C "$file_dir" . || @@ -486,7 +486,7 @@ function create_backup_file_for_single_docker_compose_project { check_file_creation "$tar_file_with_backup_dir" log_info "GZIP..." - if is_dry_run_enabled; then + if _is_dry_run_enabled; then log_dry_run "gzip $tar_file_with_backup_dir" else gzip "$tar_file_with_backup_dir" || @@ -505,34 +505,34 @@ function create_backup_file_for_single_docker_compose_project { log_notice ">>> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } -function backup_single_docker_compose_project { +function _backup_single_docker_compose_project { local backup_dir="$1" - log_debug_var "backup_single_docker_compose_project" "backup_dir" + log_debug_var "_backup_single_docker_compose_project" "backup_dir" local file="$2" - log_debug_var "backup_single_docker_compose_project" "file" + log_debug_var "_backup_single_docker_compose_project" "file" local file_dir file_dir=$(dirname "$file") - log_debug_var "backup_single_docker_compose_project" "file_dir" + log_debug_var "_backup_single_docker_compose_project" "file_dir" local file_simple_dirname file_simple_dirname=$(basename "$file_dir") - log_debug_var "backup_single_docker_compose_project" "file_simple_dirname" + log_debug_var "_backup_single_docker_compose_project" "file_simple_dirname" log_delimiter_start 2 "'${file}'" cd "${file_dir}" || log_error "Failed to change directory to '${file_dir}'" log_notice "Changed directory to '$(pwd)'" - function down { + function _down { log_info "DOWN ('${file_simple_dirname}')..." - if is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_notice "$($DOCKER_COMPOSE_CMD down)"; fi + if _is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD down"; else log_notice "$($DOCKER_COMPOSE_CMD down)"; fi } - function up { + function _up { log_info "UP ('${file_simple_dirname}')..." - if is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi + if _is_dry_run_enabled; then log_dry_run "$DOCKER_COMPOSE_CMD up -d"; else log_notice "$($DOCKER_COMPOSE_CMD up -d)"; fi } local is_running=false @@ -543,16 +543,16 @@ function backup_single_docker_compose_project { log_debug "Docker-Compose project '${file}' is not running" fi - if $is_running; then down; else log_notice "Skip 'down' because it is not running"; fi + if $is_running; then _down; else log_notice "Skip 'down' because it is not running"; fi - create_backup_file_for_single_docker_compose_project "$backup_dir" "$file" + _create_backup_file_for_single_docker_compose_project "$backup_dir" "$file" - if $is_running; then up; else log_notice "Skip 'up' because it was not running"; fi + if $is_running; then _up; else log_notice "Skip 'up' because it was not running"; fi log_delimiter_end 2 "'${file}'" } -function backup_docker_compose_projects { +function _backup_docker_compose_projects { local search_dir="$1" local exclude_dir="$2" local backup_dir="$3" @@ -574,7 +574,7 @@ function backup_docker_compose_projects { backup_dir="${backup_dir}$(date +"%Y-%m-%d")/" if directory_not_exists "$backup_dir"; then - if is_dry_run_enabled; then + if _is_dry_run_enabled; then log_dry_run "mkdir -p $backup_dir" else mkdir -p "$backup_dir" || log_error "Backup directory '$backup_dir' could not be created" @@ -591,11 +591,11 @@ function backup_docker_compose_projects { log_delimiter_start 1 "BACKUP" prepare_search_dir - log_debug_var "backup_docker_compose_projects" "search_dir" - log_debug_var "backup_docker_compose_projects" "exclude_dir" + log_debug_var "_backup_docker_compose_projects" "search_dir" + log_debug_var "_backup_docker_compose_projects" "exclude_dir" local docker_compose_files - docker_compose_files=$(find_docker_compose_files "$search_dir" "$exclude_dir") + docker_compose_files=$(_find_docker_compose_files "$search_dir" "$exclude_dir") if [ -z "$docker_compose_files" ]; then log_error "No ${DOCKER_COMPOSE_NAME} files found in '${search_dir}'. Cannot perform backup." @@ -604,14 +604,14 @@ function backup_docker_compose_projects { fi prepare_backup_dir - log_debug_var "backup_docker_compose_projects" "backup_dir" + log_debug_var "_backup_docker_compose_projects" "backup_dir" while IFS= read -r file; do - backup_single_docker_compose_project "$backup_dir" "$file" + _backup_single_docker_compose_project "$backup_dir" "$file" done <<<"$docker_compose_files" log_info "'${backup_dir}'..." - if is_dry_run_enabled; then log_dry_run "ls -larth $backup_dir"; else log_notice "$(ls -larth "$backup_dir")"; fi + if _is_dry_run_enabled; then log_dry_run "ls -larth $backup_dir"; else log_notice "$(ls -larth "$backup_dir")"; fi log_delimiter_end 1 "BACKUP" } @@ -623,7 +623,7 @@ function backup_docker_compose_projects { # ╚═════════════════════╩══════════════════════╝ function clean_docker_environment { - function process_preview { + function _process_preview { log_delimiter_start 2 "PREVIEW" log_info "Listing non-running containers..." @@ -638,24 +638,24 @@ function clean_docker_environment { log_delimiter_end 2 "PREVIEW" } - function process_remove { + function _process_remove { log_delimiter_start 2 "REMOVE" log_notice "Removing non-running containers..." - if is_dry_run_enabled; then log_dry_run "docker container prune -f"; else log_notice "$(docker container prune -f)"; fi + if _is_dry_run_enabled; then log_dry_run "docker container prune -f"; else log_notice "$(docker container prune -f)"; fi log_notice "Removing unused docker images..." - if is_dry_run_enabled; then log_dry_run "docker image prune -f"; else log_notice "$(docker image prune -f)"; fi + if _is_dry_run_enabled; then log_dry_run "docker image prune -f"; else log_notice "$(docker image prune -f)"; fi log_notice "Removing unused volumes..." - if is_dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_notice "$(docker volume prune -f)"; fi + if _is_dry_run_enabled; then log_dry_run "docker volume prune -f"; else log_notice "$(docker volume prune -f)"; fi log_delimiter_end 2 "REMOVE" } log_delimiter_start 1 "CLEAN" - process_preview - process_remove + _process_preview + _process_remove log_delimiter_end 1 "CLEAN" } @@ -667,9 +667,9 @@ function clean_docker_environment { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -process_arguments "$@" +_process_arguments "$@" -check_permissions +_check_permissions _set_docker_compose_cmd @@ -679,7 +679,7 @@ show_docker_info case $_ARG_ACTION in backup) - backup_docker_compose_projects "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" + _backup_docker_compose_projects "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" ;; clean) clean_docker_environment From b07f3346d6ef937c07dcddeadec035902cef49a8 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 17:51:27 +0100 Subject: [PATCH 42/54] Improve comments --- src/domposy.bash | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index a29911d..4721c97 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -169,6 +169,7 @@ function _is_dry_run_enabled { is_true "$ENABLE_DRY_RUN" } +# Checks if a var contains a trailing slash. function contains_trailing_slash { if is_var_not_empty "$1" && [[ "${1: -1}" == "/" ]]; then return 0 @@ -176,6 +177,7 @@ function contains_trailing_slash { return 1 } +# Checks if a directory exists. function check_file_creation { local file=$1 log_debug_var "check_file_creation" "file" @@ -199,7 +201,6 @@ function check_file_creation { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -# Checks whether the user has root rights and if not, whether he is at least added to the 'docker' group. function _check_permissions { log_notice "Current user: '$(whoami)'" if [[ $(id -u) -ne 0 ]]; then @@ -385,6 +386,7 @@ function _process_arguments { # ║ ║ # ╚═════════════════════╩══════════════════════╝ +# Shows Docker information like disk usage, running containers, images etc. function show_docker_info { log_delimiter_start 1 "DOCKER INFO" log_info "docker system df..." @@ -622,6 +624,7 @@ function _backup_docker_compose_projects { # ║ ║ # ╚═════════════════════╩══════════════════════╝ +# Cleans the Docker environment by removing non-running containers, unused images and volumes. function clean_docker_environment { function _process_preview { log_delimiter_start 2 "PREVIEW" @@ -668,9 +671,7 @@ function clean_docker_environment { # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ _process_arguments "$@" - _check_permissions - _set_docker_compose_cmd log_notice "Current directory: '$(pwd)'" From 4f235d1f64ced455a6df2f180c2fad001fc245e8 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 18:01:38 +0100 Subject: [PATCH 43/54] Renamings --- src/domposy.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 4721c97..b225f49 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -214,7 +214,7 @@ function _check_permissions { # ╔═════════════════════╦══════════════════════╗ # ║ ║ -# ║ GET ARGUMENTS ║ +# ║ ARGUMENTS ║ # ║ ║ # ╚═════════════════════╩══════════════════════╝ @@ -404,7 +404,7 @@ function show_docker_info { } # Returns the Docker Compose command. So whether 'docker-compose' or 'docker compose'. -function get_docker_compose_command { +function get_docker_compose_cmd { if command -v docker-compose &>/dev/null; then echo "docker-compose" elif docker compose version &>/dev/null; then @@ -415,7 +415,7 @@ function get_docker_compose_command { } function _set_docker_compose_cmd { - DOCKER_COMPOSE_CMD=$(get_docker_compose_command) + DOCKER_COMPOSE_CMD=$(get_docker_compose_cmd) log_debug "'${DOCKER_COMPOSE_CMD}' is used" local version_output From 33eadc36c0035a5c18c0aeee05e9b2b8cdd79c61 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 19:54:57 +0100 Subject: [PATCH 44/54] Add comments and use declare --- src/domposy.bash | 161 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 119 insertions(+), 42 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index b225f49..edc9843 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -1,9 +1,68 @@ #!/usr/bin/env bash +# # # # # # # # # # # # # # # # # # # # # # # # # +# _ # +# | | # +# __| | ___ _ __ ___ _ __ ___ ___ _ _ # +# / _` |/ _ \| '_ ` _ \| '_ \ / _ \/ __| | | | # +# | (_| | (_) | | | | | | |_) | (_) \__ \ |_| | # +# \__,_|\___/|_| |_| |_| .__/ \___/|___/\__, | # +# | | __/ | # +# |_| |___/ # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # + +# DISCLAIMER: +# Not POSIX conform! +# +# # DESCRIPTION: # This script simplifies your Docker Compose management. +# +# It can be sourced with: +# +# ```bash +# source /domposy.bash >/dev/null 2>&1 +# ``` +# +# `` should be replaced by the actual path where 'domposy' is located. +# +# But it is recommended to run it directly: +# +# e.g.: +# ```bash +# ./domposy.bash -a backup --search-dir /data/container/ --exclude-dir git --backup-dir /mnt/backups/ +# ``` +# +# To summarize briefly: +# - It shows Docker information like disk usage, running containers, images etc. +# - It creates a backup of a Docker Compose project folder by packing the files into a tar archive and then compressing them. +# - It cleans the Docker environment by removing non-running containers, unused images and volumes. -CONST_DOMPOSY_VERSION="2.0.0" +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ LICENSE ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +# simbashlog: +# https://github.com/fuchs-fabian/simbashlog/blob/main/LICENSE +# +# domposy: +# https://github.com/fuchs-fabian/domposy/blob/main/LICENSE + +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ METADATA ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +declare -rx CONST_DOMPOSY_VERSION="2.0.0" +declare -rx CONST_DOMPOSY_NAME="domposy" # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ @@ -51,18 +110,19 @@ function find_bin_script { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -LOGGER="simbashlog" +declare -rx CONST_LOGGER_NAME="simbashlog" -ORIGINAL_LOGGER_SCRIPT_PATH=$(find_bin_script "$LOGGER") || +CONST_ORIGINAL_LOGGER_SCRIPT_PATH=$(find_bin_script "$CONST_LOGGER_NAME") || { - echo "Critical: Unable to resolve logger script '$LOGGER'. Exiting..." + echo "Critical: Unable to resolve logger script '$CONST_LOGGER_NAME'. Exiting..." exit 1 } +declare -rx CONST_ORIGINAL_LOGGER_SCRIPT_PATH # shellcheck source=/dev/null -source "$ORIGINAL_LOGGER_SCRIPT_PATH" >/dev/null 2>&1 || +source "$CONST_ORIGINAL_LOGGER_SCRIPT_PATH" >/dev/null 2>&1 || { - echo "Critical: Unable to source logger script '$ORIGINAL_LOGGER_SCRIPT_PATH'. Exiting..." + echo "Critical: Unable to source logger script '$CONST_ORIGINAL_LOGGER_SCRIPT_PATH'. Exiting..." exit 1 } @@ -146,16 +206,16 @@ function log_delimiter_end { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -ENABLE_DRY_RUN=false +declare -x ENABLE_DRY_RUN=false -DOCKER_COMPOSE_NAME="docker-compose" -DOCKER_COMPOSE_CMD="" +declare -rx CONST_DOCKER_COMPOSE_NAME="docker-compose" +declare -x DOCKER_COMPOSE_CMD="" -DEFAULT_ACTION="backup" +declare -rx CONST_DEFAULT_ACTION="backup" -DEFAULT_SEARCH_DIR="/home/" -DEFAULT_EXCLUDE_DIR="tmp" -DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" +declare -rx CONST_DEFAULT_SEARCH_DIR="/home/" +declare -rx CONST_DEFAULT_EXCLUDE_DIR="tmp" +declare -rx CONST_DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ @@ -218,11 +278,11 @@ function _check_permissions { # ║ ║ # ╚═════════════════════╩══════════════════════╝ -_ARG_ACTION="${DEFAULT_ACTION}" +declare -x _ARG_ACTION="${CONST_DEFAULT_ACTION}" -_ARG_SEARCH_DIR="${DEFAULT_SEARCH_DIR}" -_ARG_EXCLUDE_DIR="${DEFAULT_EXCLUDE_DIR}" -_ARG_BACKUP_DIR="${DEFAULT_BACKUP_DIR}" +declare -x _ARG_SEARCH_DIR="${CONST_DEFAULT_SEARCH_DIR}" +declare -x _ARG_EXCLUDE_DIR="${CONST_DEFAULT_EXCLUDE_DIR}" +declare -x _ARG_BACKUP_DIR="${CONST_DEFAULT_BACKUP_DIR}" function _process_arguments { local arg_which_is_processed="" @@ -273,19 +333,19 @@ function _process_arguments { echo echo " -a, --action [action] Action to be performed" echo " {backup,clean}" - echo " Default: '${DEFAULT_ACTION}'" + echo " Default: '${CONST_DEFAULT_ACTION}'" echo - echo " --search-dir [search dir] Directory to search for ${DOCKER_COMPOSE_NAME} files" + echo " --search-dir [search dir] Directory to search for ${CONST_DOCKER_COMPOSE_NAME} files" echo " $note_for_valid_action_for_backup" - echo " Default: '${DEFAULT_SEARCH_DIR}'" + echo " Default: '${CONST_DEFAULT_SEARCH_DIR}'" echo echo " --exclude-dir [exclude dir] Directory to exclude from search" echo " $note_for_valid_action_for_backup" - echo " Default: '${DEFAULT_EXCLUDE_DIR}'" + echo " Default: '${CONST_DEFAULT_EXCLUDE_DIR}'" echo echo " --backup-dir [backup dir] Destination directory for backups" echo " $note_for_valid_action_for_backup" - echo " Default: '${DEFAULT_BACKUP_DIR}'" + echo " Default: '${CONST_DEFAULT_BACKUP_DIR}'" # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=false @@ -420,7 +480,7 @@ function _set_docker_compose_cmd { local version_output version_output=$($DOCKER_COMPOSE_CMD version 2>&1) || log_error "Failed to execute '$DOCKER_COMPOSE_CMD version'. Error: $version_output" - log_info "$version_output" + log_debug "$version_output" } function _find_docker_compose_files { @@ -428,8 +488,8 @@ function _find_docker_compose_files { local exclude_dir="$2" local docker_compose_file_names=( - "${DOCKER_COMPOSE_NAME}.yml" - "${DOCKER_COMPOSE_NAME}.yaml" + "${CONST_DOCKER_COMPOSE_NAME}.yml" + "${CONST_DOCKER_COMPOSE_NAME}.yaml" ) local docker_compose_files="" @@ -502,7 +562,7 @@ function _create_backup_file_for_single_docker_compose_project { log_notice ">>> Backup created. You can download '${gz_file_with_backup_dir}' e.g. with FileZilla." log_notice ">>> To navigate to the backup directory: 'cd ${backup_dir}'" - log_notice ">>> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" + log_notice ">>> To move the file: '(sudo) mv ${gz_file} /my/dir/for/${CONST_DOCKER_COMPOSE_NAME}-projects/${file_simple_dirname}/'" log_notice ">>> To undo gzip: '(sudo) gunzip ${gz_file}'" log_notice ">>> To unpack the tar file: '(sudo) tar -xpf ${tar_file}'" } @@ -600,9 +660,9 @@ function _backup_docker_compose_projects { docker_compose_files=$(_find_docker_compose_files "$search_dir" "$exclude_dir") if [ -z "$docker_compose_files" ]; then - log_error "No ${DOCKER_COMPOSE_NAME} files found in '${search_dir}'. Cannot perform backup." + log_error "No ${CONST_DOCKER_COMPOSE_NAME} files found in '${search_dir}'. Cannot perform backup." else - log_notice "${DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" + log_notice "${CONST_DOCKER_COMPOSE_NAME} files: "$'\n'"${docker_compose_files}" fi prepare_backup_dir @@ -670,23 +730,40 @@ function clean_docker_environment { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -_process_arguments "$@" -_check_permissions -_set_docker_compose_cmd - -log_notice "Current directory: '$(pwd)'" +function execute_backup { + local search_dir="$1" + local exclude_dir="$2" + local backup_dir="$3" -show_docker_info + _backup_docker_compose_projects "$search_dir" "$exclude_dir" "$backup_dir" +} -case $_ARG_ACTION in -backup) - _backup_docker_compose_projects "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" - ;; -clean) +function execute_clean { clean_docker_environment - ;; -esac +} + +if is_current_script_sourced; then + print_colored_text "'$CONST_DOMPOSY_NAME' detected that it was sourced." "cyan" "regular" + _set_docker_compose_cmd +else + _process_arguments "$@" + _check_permissions + _set_docker_compose_cmd + + log_notice "Current directory: '$(pwd)'" + + show_docker_info + + case $_ARG_ACTION in + backup) + execute_backup "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" + ;; + clean) + execute_clean + ;; + esac -show_docker_info + show_docker_info +fi exit 0 From a13e2fae174ba9456ccb7240456678fed6d27659 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 20:04:13 +0100 Subject: [PATCH 45/54] Undo source detection --- src/domposy.bash | 53 ++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index edc9843..658be7d 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -19,15 +19,7 @@ # DESCRIPTION: # This script simplifies your Docker Compose management. # -# It can be sourced with: -# -# ```bash -# source /domposy.bash >/dev/null 2>&1 -# ``` -# -# `` should be replaced by the actual path where 'domposy' is located. -# -# But it is recommended to run it directly: +# It is not intended to be sourced, but to be run directly. # # e.g.: # ```bash @@ -735,6 +727,10 @@ function execute_backup { local exclude_dir="$2" local backup_dir="$3" + log_debug_var "execute_backup" "search_dir" + log_debug_var "execute_backup" "exclude_dir" + log_debug_var "execute_backup" "backup_dir" + _backup_docker_compose_projects "$search_dir" "$exclude_dir" "$backup_dir" } @@ -742,28 +738,23 @@ function execute_clean { clean_docker_environment } -if is_current_script_sourced; then - print_colored_text "'$CONST_DOMPOSY_NAME' detected that it was sourced." "cyan" "regular" - _set_docker_compose_cmd -else - _process_arguments "$@" - _check_permissions - _set_docker_compose_cmd - - log_notice "Current directory: '$(pwd)'" - - show_docker_info - - case $_ARG_ACTION in - backup) - execute_backup "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" - ;; - clean) - execute_clean - ;; - esac +_process_arguments "$@" +_check_permissions +_set_docker_compose_cmd + +log_notice "Current directory: '$(pwd)'" + +show_docker_info + +case $_ARG_ACTION in +backup) + execute_backup "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" + ;; +clean) + execute_clean + ;; +esac - show_docker_info -fi +show_docker_info exit 0 From b99e911dbbeef9c617c2aa0b477bac42ce821564 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 20:06:26 +0100 Subject: [PATCH 46/54] Add underscore for private functions --- src/domposy.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 658be7d..989dc3b 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -677,7 +677,7 @@ function _backup_docker_compose_projects { # ╚═════════════════════╩══════════════════════╝ # Cleans the Docker environment by removing non-running containers, unused images and volumes. -function clean_docker_environment { +function _clean_docker_environment { function _process_preview { log_delimiter_start 2 "PREVIEW" @@ -735,7 +735,7 @@ function execute_backup { } function execute_clean { - clean_docker_environment + _clean_docker_environment } _process_arguments "$@" From 059e5e885f30c804485fe11c372467c616bdc437 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Sun, 10 Nov 2024 20:11:33 +0100 Subject: [PATCH 47/54] Fix comment --- src/domposy.bash | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index 989dc3b..c000c46 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -94,13 +94,11 @@ function find_bin_script { return 1 } -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -# ░░ ░░ -# ░░ ░░ -# ░░ LOGGING ░░ -# ░░ ░░ -# ░░ ░░ -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ LOGGING ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ declare -rx CONST_LOGGER_NAME="simbashlog" From 1a749edfb3f78df99f9df1d0ca0d784d78c8a546 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 00:32:40 +0100 Subject: [PATCH 48/54] Improve setup.bash --- setup.bash | 226 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 162 insertions(+), 64 deletions(-) diff --git a/setup.bash b/setup.bash index c662ee0..784ef3c 100644 --- a/setup.bash +++ b/setup.bash @@ -1,27 +1,68 @@ #!/usr/bin/env bash -APP_NAME="domposy" -APP_BRANCH_NAME="v2" +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ VARIABLES ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -LOGGER_NAME="simbashlog" -LOGGER_BRANCH_NAME="main" +declare -r CONST_APP_NAME="domposy" +declare -r CONST_APP_BRANCH_NAME="v2" -# NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'! -REPO_URLS=( - "https://github.com/fuchs-fabian/${LOGGER_NAME}.git" - "https://github.com/fuchs-fabian/${APP_NAME}.git" -) +declare -r CONST_LOGGER_NAME="simbashlog" +declare -r CONST_LOGGER_BRANCH_NAME="v1.1.3" + +declare -r CONST_GLOBAL_DOWNLOAD_PATH="/opt" +declare -r CONST_GLOBAL_BIN_PATH="/usr/bin" -DEPENDENCIES=( +declare -r CONST_USER_DOWNLOAD_PATH="$HOME/.local/share" +declare -r CONST_USER_BIN_PATH="$HOME/.local/bin" + +declare -r CONST_DEPENDENCIES=( "git" - "rsync" + "docker" + "tar" + "gzip" +) + +# NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'! +declare -r CONST_REPO_URLS=( + "https://github.com/fuchs-fabian/${CONST_LOGGER_NAME}.git" + "https://github.com/fuchs-fabian/${CONST_APP_NAME}.git" ) -OPT_DIR="/opt" +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ LOGIC ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +function get_app_name_from_repo_url { + basename "$1" .git +} + +function is_root { + [[ $(id -u) -eq 0 ]] +} + +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ INSTALLATION ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ function install { + local download_path="" + local bin_path="" + + echo "Current user: '$(whoami)'" + function check_dependencies { - for dependency in "${DEPENDENCIES[@]}"; do + for dependency in "${CONST_DEPENDENCIES[@]}"; do echo "Checking if '$dependency' is available..." command -v "$dependency" >/dev/null 2>&1 || { @@ -32,85 +73,142 @@ function install { done } - function get_app_name_from_repo_url { - basename "$1" .git + function create_directory { + local directory="$1" + + if [ ! -d "$directory" ]; then + echo "Creating directory '$directory'..." + mkdir -p "$directory" + fi + } + + function set_paths { + if is_root; then + download_path="$CONST_GLOBAL_DOWNLOAD_PATH" + bin_path="$CONST_GLOBAL_BIN_PATH" + else + echo "You are not root." + echo "The installation will be only available for the current user." + echo "To install it globally, please run this script as root (sudo)." + + read -r -p "Do you want to continue? [Y/n] " choice + if [[ "$choice" =~ ^[Yy]$ ]]; then + download_path="$CONST_USER_DOWNLOAD_PATH" + bin_path="$CONST_USER_BIN_PATH" + + create_directory "$CONST_USER_DOWNLOAD_PATH" + create_directory "$CONST_USER_BIN_PATH" + else + echo "Installation aborted." + exit 1 + fi + fi + + echo "Donwload path: '$download_path'" + echo "Bin path: '$bin_path'" } - read -r -p "Do you want to install the $APP_NAME globally ('g') or just for the current user ('u')? (g/u): " bin_dir_choice - if [[ "$bin_dir_choice" =~ ^[Gg]$ ]]; then - # Symlink in /usr/bin for all users - bin_path="/usr/bin" - else - # Symlink in ~/bin for the current user - bin_path="$HOME/bin" - fi + check_dependencies + set_paths - for repo_url in "${REPO_URLS[@]}"; do + for repo_url in "${CONST_REPO_URLS[@]}"; do + local app_name app_name=$(get_app_name_from_repo_url "$repo_url") - app_dir="$OPT_DIR/$app_name" - if [ -d "$app_dir" ]; then - echo "$app_name is already downloaded." + local app_path="$download_path/$app_name" + + if [ -d "$app_path" ]; then + echo "'$app_name' is already downloaded." else - read -r -p "$app_name is not downloaded. Do you want to download it? (y/n): " choice - if [[ "$choice" =~ ^[Yy]$ ]]; then - echo "Cloning $app_name..." + echo "Downloading '$app_name'..." + + if [[ "$app_name" == "$CONST_LOGGER_NAME" ]]; then + echo "Cloning '$CONST_LOGGER_NAME' from branch '$CONST_LOGGER_BRANCH_NAME'..." + git clone --branch "$CONST_LOGGER_BRANCH_NAME" "$repo_url" "$app_path" - if [[ "$app_name" == "$APP_NAME" ]]; then - git clone --branch "$APP_BRANCH_NAME" "$repo_url" "$app_dir" - elif [[ "$app_name" == "$LOGGER_NAME" ]]; then - git clone --branch "$LOGGER_BRANCH_NAME" "$repo_url" "$app_dir" - else - git clone "$repo_url" "$app_dir" - fi + elif [[ "$app_name" == "$CONST_APP_NAME" ]]; then + echo "Cloning '$CONST_APP_NAME' from branch '$CONST_APP_BRANCH_NAME'..." + git clone --branch "$CONST_APP_BRANCH_NAME" "$repo_url" "$app_path" + + else + echo "Cloning $app_name..." + git clone "$repo_url" "$app_path" fi fi - if [ -f "$app_dir/src/${app_name}.bash" ]; then - chmod +x "$app_dir/src/${app_name}.bash" + local original_app_path="$app_path/src/${app_name}.bash" - echo "Creating symlink for $app_name..." - ln -sf "$app_dir/src/${app_name}.bash" "$bin_path/$app_name" - echo "$app_name has been installed and is now executable." + if [ -f "$original_app_path" ]; then + echo "Making '$app_name' executable..." + chmod +x "$original_app_path" + + echo "Creating symlink for '$app_name'..." + ln -sf "$original_app_path" "$bin_path/$app_name" + echo "'$app_name' has been installed and is now executable." if [ -n "$(command -v "$app_name")" ]; then "$app_name" -v - echo "$app_name is working." + echo "'$app_name' is working." else - echo "$app_name is not working." + echo "'$app_name' is not working." fi else - echo "The file ${app_name}.bash was not found in the directory $app_dir/src." + echo "The file '${app_name}.bash' was not found in '$app_path/src'." fi done } +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ UNINSTALLATION ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + function uninstall { - local bin_paths=( - "/usr/bin" - "$HOME/bin" - ) - - for repo_url in "${REPO_URLS[@]}"; do - app_name=$(basename "$repo_url" .git) - app_dir="$OPT_DIR/$app_name" - - for bin_path in "${bin_paths[@]}"; do - if [ -L "$bin_path/$app_name" ]; then - echo "Removing symlink for $app_name in $bin_path..." - rm "$bin_path/$app_name" - fi - done + local download_path="" + local bin_path="" + + function set_paths { + if is_root; then + download_path="$CONST_GLOBAL_DOWNLOAD_PATH" + bin_path="$CONST_GLOBAL_BIN_PATH" + else + download_path="$CONST_USER_DOWNLOAD_PATH" + bin_path="$CONST_USER_BIN_PATH" + fi + + echo "Donwload path: '$download_path'" + echo "Bin path: '$bin_path'" + } - if [ -d "$app_dir" ]; then - echo "Removing directory $app_dir..." - rm -rf "$app_dir" + set_paths + + for repo_url in "${CONST_REPO_URLS[@]}"; do + app_name=$(get_app_name_from_repo_url "$repo_url") + app_path="$download_path/$app_name" + + if [ -L "$bin_path/$app_name" ]; then + echo "Removing symlink for '$app_name' in '$bin_path'..." + rm "$bin_path/$app_name" + fi + + if [ -d "$app_path" ]; then + echo "Removing directory '$app_path'..." + rm -rf "$app_path" else - echo "$app_name is not installed." + echo "'$app_name' is not installed." fi done } +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ MAIN ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + case "$1" in install) install From 7df1a59558504bfd876ca8c03058abcde6a02566 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 18:18:15 +0100 Subject: [PATCH 49/54] Remove wrapper functions --- src/domposy.bash | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index c000c46..de3f151 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -604,7 +604,7 @@ function _backup_single_docker_compose_project { log_delimiter_end 2 "'${file}'" } -function _backup_docker_compose_projects { +function backup_docker_compose_projects { local search_dir="$1" local exclude_dir="$2" local backup_dir="$3" @@ -643,8 +643,8 @@ function _backup_docker_compose_projects { log_delimiter_start 1 "BACKUP" prepare_search_dir - log_debug_var "_backup_docker_compose_projects" "search_dir" - log_debug_var "_backup_docker_compose_projects" "exclude_dir" + log_debug_var "backup_docker_compose_projects" "search_dir" + log_debug_var "backup_docker_compose_projects" "exclude_dir" local docker_compose_files docker_compose_files=$(_find_docker_compose_files "$search_dir" "$exclude_dir") @@ -656,7 +656,7 @@ function _backup_docker_compose_projects { fi prepare_backup_dir - log_debug_var "_backup_docker_compose_projects" "backup_dir" + log_debug_var "backup_docker_compose_projects" "backup_dir" while IFS= read -r file; do _backup_single_docker_compose_project "$backup_dir" "$file" @@ -675,7 +675,7 @@ function _backup_docker_compose_projects { # ╚═════════════════════╩══════════════════════╝ # Cleans the Docker environment by removing non-running containers, unused images and volumes. -function _clean_docker_environment { +function clean_docker_environment { function _process_preview { log_delimiter_start 2 "PREVIEW" @@ -720,22 +720,6 @@ function _clean_docker_environment { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -function execute_backup { - local search_dir="$1" - local exclude_dir="$2" - local backup_dir="$3" - - log_debug_var "execute_backup" "search_dir" - log_debug_var "execute_backup" "exclude_dir" - log_debug_var "execute_backup" "backup_dir" - - _backup_docker_compose_projects "$search_dir" "$exclude_dir" "$backup_dir" -} - -function execute_clean { - _clean_docker_environment -} - _process_arguments "$@" _check_permissions _set_docker_compose_cmd @@ -746,10 +730,10 @@ show_docker_info case $_ARG_ACTION in backup) - execute_backup "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" + backup_docker_compose_projects "$_ARG_SEARCH_DIR" "$_ARG_EXCLUDE_DIR" "$_ARG_BACKUP_DIR" ;; clean) - execute_clean + clean_docker_environment ;; esac From 14abde05235bcd49019dd5be3aad378649a43ca7 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 18:57:18 +0100 Subject: [PATCH 50/54] Add option log-dir and notifier --- src/domposy.bash | 116 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 31 deletions(-) diff --git a/src/domposy.bash b/src/domposy.bash index de3f151..c51e367 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -56,6 +56,35 @@ declare -rx CONST_DOMPOSY_VERSION="2.0.0" declare -rx CONST_DOMPOSY_NAME="domposy" +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ VARIABLES ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +declare -x ENABLE_DRY_RUN=false +declare -x DOCKER_COMPOSE_CMD="" + +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +# ░░ ░░ +# ░░ ░░ +# ░░ CONSTANTS ░░ +# ░░ ░░ +# ░░ ░░ +# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ + +declare -rx CONST_DOCKER_COMPOSE_NAME="docker-compose" + +declare -rx CONST_DEFAULT_ACTION="backup" + +declare -rx CONST_DEFAULT_SEARCH_DIR="/home/" +declare -rx CONST_DEFAULT_EXCLUDE_DIR="tmp" +declare -rx CONST_DEFAULT_BACKUP_DIR="/tmp/${CONST_DOMPOSY_NAME}/backups/" + +declare -rx CONST_DEFAULT_LOG_DIR="/tmp/${CONST_DOMPOSY_NAME}/logs/" + # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -123,7 +152,7 @@ ENABLE_JSON_LOG_FILE=false # shellcheck disable=SC2034 ENABLE_LOG_TO_SYSTEM=false # shellcheck disable=SC2034 -LOG_DIR="/tmp/simbashlogs/" +LOG_DIR="$CONST_DEFAULT_LOG_DIR" # shellcheck disable=SC2034 ENABLE_SIMPLE_LOG_DIR_STRUCTURE=true # shellcheck disable=SC2034 @@ -143,6 +172,10 @@ SHOW_CURRENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING="simple_without_file_ext # shellcheck disable=SC2034 ENABLE_PARENT_SCRIPT_NAME_IN_CONSOLE_OUTPUTS_FOR_LOGGING=false # shellcheck disable=SC2034 +SIMBASHLOG_NOTIFIER="" +# shellcheck disable=SC2034 +SIMBASHLOG_NOTIFIER_CONFIG_PATH="" +# shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=true function log_debug_var { @@ -188,25 +221,6 @@ function log_delimiter_end { log_delimiter "$1" "$2" "<" false } -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ -# ░░ ░░ -# ░░ ░░ -# ░░ VARIABLES ░░ -# ░░ ░░ -# ░░ ░░ -# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ - -declare -x ENABLE_DRY_RUN=false - -declare -rx CONST_DOCKER_COMPOSE_NAME="docker-compose" -declare -x DOCKER_COMPOSE_CMD="" - -declare -rx CONST_DEFAULT_ACTION="backup" - -declare -rx CONST_DEFAULT_SEARCH_DIR="/home/" -declare -rx CONST_DEFAULT_EXCLUDE_DIR="tmp" -declare -rx CONST_DEFAULT_BACKUP_DIR="/tmp/${CONST_SIMPLE_SCRIPT_NAME_WITHOUT_FILE_EXTENSION}_backups/" - # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ # ░░ ░░ @@ -219,6 +233,12 @@ function _is_dry_run_enabled { is_true "$ENABLE_DRY_RUN" } +function _disable_notifier { + log_debug "Notifier is disabled" + # shellcheck disable=SC2034 + SIMBASHLOG_NOTIFIER="" +} + # Checks if a var contains a trailing slash. function contains_trailing_slash { if is_var_not_empty "$1" && [[ "${1: -1}" == "/" ]]; then @@ -298,6 +318,11 @@ function _process_arguments { _validate_if_value_is_long_argument "$value" } + function _log_error_if_value_is_empty { + local value="$1" + if is_var_empty "$value"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default value do not use this option."; fi + } + local note_for_valid_action_for_backup="Note: '-a, --action' should be used before this, otherwise it has no effect" local is_action_backup_used=false function _is_used_without_action_backup { @@ -323,19 +348,26 @@ function _process_arguments { echo echo " -a, --action [action] Action to be performed" echo " {backup,clean}" - echo " Default: '${CONST_DEFAULT_ACTION}'" + echo " Default: '$CONST_DEFAULT_ACTION'" echo - echo " --search-dir [search dir] Directory to search for ${CONST_DOCKER_COMPOSE_NAME} files" + echo " --search-dir [search dir] Directory to search for $CONST_DOCKER_COMPOSE_NAME files" echo " $note_for_valid_action_for_backup" - echo " Default: '${CONST_DEFAULT_SEARCH_DIR}'" + echo " Default: '$CONST_DEFAULT_SEARCH_DIR'" echo echo " --exclude-dir [exclude dir] Directory to exclude from search" echo " $note_for_valid_action_for_backup" - echo " Default: '${CONST_DEFAULT_EXCLUDE_DIR}'" + echo " Default: '$CONST_DEFAULT_EXCLUDE_DIR'" echo echo " --backup-dir [backup dir] Destination directory for backups" echo " $note_for_valid_action_for_backup" - echo " Default: '${CONST_DEFAULT_BACKUP_DIR}'" + echo " Default: '$CONST_DEFAULT_BACKUP_DIR'" + echo + echo " --log-dir [log dir] Directory for log files" + echo " Default: '$CONST_DEFAULT_LOG_DIR'" + echo + echo " --notifier [notifier] '$CONST_SIMBASHLOG_NAME' notifier ($CONST_SIMBASHLOG_NOTIFIERS_GITHUB_LINK)" + echo " Important: The notifier must be correctly installed" + echo " Default: none" # shellcheck disable=SC2034 ENABLE_SUMMARY_ON_EXIT=false @@ -392,8 +424,7 @@ function _process_arguments { _is_used_without_action_backup shift _validate_if_value_is_argument "$1" - - if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi + _log_error_if_value_is_empty "$1" _ARG_SEARCH_DIR="$1" log_debug_var "_process_arguments" "_ARG_SEARCH_DIR" @@ -404,8 +435,7 @@ function _process_arguments { _is_used_without_action_backup shift _validate_if_value_is_argument "$1" - - if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you do not want to exclude any directory do not use this option."; fi + _log_error_if_value_is_empty "$1" _ARG_EXCLUDE_DIR="$1" log_debug_var "_process_arguments" "_ARG_EXCLUDE_DIR" @@ -416,12 +446,33 @@ function _process_arguments { _is_used_without_action_backup shift _validate_if_value_is_argument "$1" - - if is_var_empty "$1"; then log_error "'$arg_which_is_processed': Value must not be empty. If you want to use the default directory do not use this option."; fi + _log_error_if_value_is_empty "$1" _ARG_BACKUP_DIR="$1" log_debug_var "_process_arguments" "_ARG_BACKUP_DIR" ;; + --log-dir) + log_debug "'$1' selected" + arg_which_is_processed="$1" + shift + _validate_if_value_is_argument "$1" + _log_error_if_value_is_empty "$1" + + # shellcheck disable=SC2034 + LOG_DIR="$1" + log_debug_var "_process_arguments" "LOG_DIR" + ;; + --notifier) + log_debug "'$1' selected" + arg_which_is_processed="$1" + shift + _validate_if_value_is_argument "$1" + _log_error_if_value_is_empty "$1" + + # shellcheck disable=SC2034 + SIMBASHLOG_NOTIFIER="$1" + log_debug_var "_process_arguments" "SIMBASHLOG_NOTIFIER" + ;; *) log_error "Invalid argument: '$1'. $message_with_help_information" ;; @@ -721,6 +772,9 @@ function clean_docker_environment { # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ _process_arguments "$@" + +if _is_dry_run_enabled; then _disable_notifier; fi + _check_permissions _set_docker_compose_cmd From b400d0c743b3b2bfe48d08bb15eb58f246be5f8c Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 19:10:44 +0100 Subject: [PATCH 51/54] Add option for system logging --- src/domposy.bash | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/domposy.bash b/src/domposy.bash index c51e367..fb2a2b7 100644 --- a/src/domposy.bash +++ b/src/domposy.bash @@ -83,7 +83,7 @@ declare -rx CONST_DEFAULT_SEARCH_DIR="/home/" declare -rx CONST_DEFAULT_EXCLUDE_DIR="tmp" declare -rx CONST_DEFAULT_BACKUP_DIR="/tmp/${CONST_DOMPOSY_NAME}/backups/" -declare -rx CONST_DEFAULT_LOG_DIR="/tmp/${CONST_DOMPOSY_NAME}/logs/" +declare -rx CONST_DEFAULT_LOG_DIR="/tmp/logs/" # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ # ░░ ░░ @@ -365,6 +365,10 @@ function _process_arguments { echo " --log-dir [log dir] Directory for log files" echo " Default: '$CONST_DEFAULT_LOG_DIR'" echo + echo " --enable-system-logging Enables logging to the system" + echo " Important: Only logs warnings or more severe messages" + echo " Default: false" + echo echo " --notifier [notifier] '$CONST_SIMBASHLOG_NAME' notifier ($CONST_SIMBASHLOG_NOTIFIERS_GITHUB_LINK)" echo " Important: The notifier must be correctly installed" echo " Default: none" @@ -462,6 +466,11 @@ function _process_arguments { LOG_DIR="$1" log_debug_var "_process_arguments" "LOG_DIR" ;; + --enable-system-logging) + log_debug "'$1' selected" + # shellcheck disable=SC2034 + ENABLE_LOG_TO_SYSTEM=true + ;; --notifier) log_debug "'$1' selected" arg_which_is_processed="$1" From e76f45880d581db5dd75921349e0eb86210c50d6 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 21:10:25 +0100 Subject: [PATCH 52/54] Update setup.bash and README.md --- README.md | 103 ++++++++++++++++++++--- setup.bash | 241 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 249 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index efd426f..f11de3e 100644 --- a/README.md +++ b/README.md @@ -68,17 +68,102 @@ The whole thing is still at an early stage of development and can therefore lead ## Getting Started -The easiest way is to download and run the setup.bash script. +The easiest way is to download and run the [`setup.bash`](./setup.bash) script. - +> If you want to install it globally, you need root rights (sudo)!\ +> Otherwise, it will be only installed for the current user! + +The following command will download the script, make it executable, install the script and then delete it: + +```bash +wget -q -O setup.bash https://raw.githubusercontent.com/fuchs-fabian/domposy/refs/heads/main/setup.bash && \ +chmod +x setup.bash && \ +./setup.bash install && \ +rm setup.bash +``` + +Then you can use the `domposy` command: + +```plain +It is recommended to run the script with root rights to ensure that the backups work properly. + +Usage: (sudo) domposy + + -h, --help Show help + + -v, --version Show version + + -d, --debug Enables debug logging + + -n, --dry-run Executes a dry run, i.e. no changes are made to the file system + + -a, --action [action] Action to be performed + {backup,clean} + Default: 'backup' + + --search-dir [search dir] Directory to search for docker-compose files + Note: '-a, --action' should be used before this, otherwise it has no effect + Default: '/home/' + + --exclude-dir [exclude dir] Directory to exclude from search + Note: '-a, --action' should be used before this, otherwise it has no effect + Default: 'tmp' + + --backup-dir [backup dir] Destination directory for backups + Note: '-a, --action' should be used before this, otherwise it has no effect + Default: '/tmp/domposy/backups/' + + --log-dir [log dir] Directory for log files + Default: '/tmp/domposy/logs/' + + --notifier [notifier] 'simbashlog' notifier (https://github.com/fuchs-fabian/simbashlog-notifiers) + Important: The notifier must be correctly installed + Default: none +``` ### Example - +```bash +domposy --action backup --search-dir /home/ --exclude-dir git --backup-dir /tmp/domposy/backups/ --log-dir /var/log/ +``` + +### Uninstall + +If you want to uninstall the script, navigate to the directory where the `setup.bash` script is located, make it executable and run it with the `uninstall` argument: + +```bash +./setup.bash uninstall +``` + +> If the script was installed globally, you need root rights (sudo) to uninstall it! + +If you can't find the `setup.bash` script anymore, you can execute the following command: + +```bash +wget -q -O setup.bash https://raw.githubusercontent.com/fuchs-fabian/domposy/refs/heads/main/setup.bash && \ +chmod +x setup.bash && \ +./setup.bash uninstall && \ +rm setup.bash +``` + +### Update + +If you want to update the script, navigate to the directory where the `setup.bash` script is located, make it executable and run it with the `update` argument: + +```bash +./setup.bash update +``` + +> If the script was installed globally, you need root rights (sudo) to update it! + +If you can't find the `setup.bash` script anymore, you can execute the following command: + +```bash +wget -q -O setup.bash https://raw.githubusercontent.com/fuchs-fabian/domposy/refs/heads/main/setup.bash && \ +chmod +x setup.bash && \ +./setup.bash update && \ +rm setup.bash +``` ## Donate with [PayPal](https://www.paypal.com/donate/?hosted_button_id=4G9X8TDNYYNKG) @@ -91,10 +176,6 @@ If you think this tool is useful and saves you a lot of work and nerves and lets Donate with PayPal -## This might also interest you - -[`esase`](https://github.com/fuchs-fabian/esase): Easy Setup And System Enhancement (Popup based Linux Bash script/tool) - --- > This repository uses [`simbashlog`](https://github.com/fuchs-fabian/simbashlog) ([LICENSE](https://github.com/fuchs-fabian/simbashlog/blob/main/LICENSE)). diff --git a/setup.bash b/setup.bash index 784ef3c..f0a7b22 100644 --- a/setup.bash +++ b/setup.bash @@ -41,12 +41,52 @@ declare -r CONST_REPO_URLS=( # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +function is_root { + [[ $(id -u) -eq 0 ]] +} + +function check_dependencies { + for dependency in "${CONST_DEPENDENCIES[@]}"; do + echo "Checking if '$dependency' is available..." + command -v "$dependency" >/dev/null 2>&1 || + { + echo "'$dependency' is not available. Please install it and try again." + exit 1 + } + echo "'$dependency' is available." + done +} + +function create_directory { + local directory="$1" + + if [ ! -d "$directory" ]; then + echo "Creating directory '$directory'..." + mkdir -p "$directory" + fi +} + function get_app_name_from_repo_url { basename "$1" .git } -function is_root { - [[ $(id -u) -eq 0 ]] +function clone_app_from_repo_url { + local repo_url="$1" + local app_name="$2" + local app_path="$3" + + if [[ "$app_name" == "$CONST_LOGGER_NAME" ]]; then + echo "Cloning '$CONST_LOGGER_NAME' from branch '$CONST_LOGGER_BRANCH_NAME'..." + git clone --branch "$CONST_LOGGER_BRANCH_NAME" "$repo_url" "$app_path" + + elif [[ "$app_name" == "$CONST_APP_NAME" ]]; then + echo "Cloning '$CONST_APP_NAME' from branch '$CONST_APP_BRANCH_NAME'..." + git clone --branch "$CONST_APP_BRANCH_NAME" "$repo_url" "$app_path" + + else + echo "Cloning $app_name..." + git clone "$repo_url" "$app_path" + fi } # ╔═════════════════════╦══════════════════════╗ @@ -59,57 +99,34 @@ function install { local download_path="" local bin_path="" - echo "Current user: '$(whoami)'" + echo + echo "Installing..." - function check_dependencies { - for dependency in "${CONST_DEPENDENCIES[@]}"; do - echo "Checking if '$dependency' is available..." - command -v "$dependency" >/dev/null 2>&1 || - { - echo "'$dependency' is not available. Please install it and try again." - exit 1 - } - echo "'$dependency' is available." - done - } + check_dependencies - function create_directory { - local directory="$1" + if is_root; then + download_path="$CONST_GLOBAL_DOWNLOAD_PATH" + bin_path="$CONST_GLOBAL_BIN_PATH" + else + echo "You are not root." + echo "The installation will be only available for the current user." + echo "To install it globally, please run this script as root (sudo)." - if [ ! -d "$directory" ]; then - echo "Creating directory '$directory'..." - mkdir -p "$directory" - fi - } + read -r -p "Do you want to continue? [Y/n] " choice + if [[ "$choice" =~ ^[Yy]$ ]]; then + download_path="$CONST_USER_DOWNLOAD_PATH" + bin_path="$CONST_USER_BIN_PATH" - function set_paths { - if is_root; then - download_path="$CONST_GLOBAL_DOWNLOAD_PATH" - bin_path="$CONST_GLOBAL_BIN_PATH" + create_directory "$CONST_USER_DOWNLOAD_PATH" + create_directory "$CONST_USER_BIN_PATH" else - echo "You are not root." - echo "The installation will be only available for the current user." - echo "To install it globally, please run this script as root (sudo)." - - read -r -p "Do you want to continue? [Y/n] " choice - if [[ "$choice" =~ ^[Yy]$ ]]; then - download_path="$CONST_USER_DOWNLOAD_PATH" - bin_path="$CONST_USER_BIN_PATH" - - create_directory "$CONST_USER_DOWNLOAD_PATH" - create_directory "$CONST_USER_BIN_PATH" - else - echo "Installation aborted." - exit 1 - fi + echo "Installation aborted." + exit 1 fi + fi - echo "Donwload path: '$download_path'" - echo "Bin path: '$bin_path'" - } - - check_dependencies - set_paths + echo "Donwload path: '$download_path'" + echo "Bin path: '$bin_path'" for repo_url in "${CONST_REPO_URLS[@]}"; do local app_name @@ -122,35 +139,38 @@ function install { else echo "Downloading '$app_name'..." - if [[ "$app_name" == "$CONST_LOGGER_NAME" ]]; then - echo "Cloning '$CONST_LOGGER_NAME' from branch '$CONST_LOGGER_BRANCH_NAME'..." - git clone --branch "$CONST_LOGGER_BRANCH_NAME" "$repo_url" "$app_path" - - elif [[ "$app_name" == "$CONST_APP_NAME" ]]; then - echo "Cloning '$CONST_APP_NAME' from branch '$CONST_APP_BRANCH_NAME'..." - git clone --branch "$CONST_APP_BRANCH_NAME" "$repo_url" "$app_path" - - else - echo "Cloning $app_name..." - git clone "$repo_url" "$app_path" - fi + clone_app_from_repo_url "$repo_url" "$app_name" "$app_path" || + { + echo "Failed to download '$app_name'." + exit 1 + } fi local original_app_path="$app_path/src/${app_name}.bash" if [ -f "$original_app_path" ]; then echo "Making '$app_name' executable..." - chmod +x "$original_app_path" + chmod +x "$original_app_path" || + { + echo "Failed to make '$app_name' executable." + exit 1 + } echo "Creating symlink for '$app_name'..." - ln -sf "$original_app_path" "$bin_path/$app_name" + ln -sf "$original_app_path" "$bin_path/$app_name" || + { + echo "Failed to create symlink for '$app_name' in '$bin_path'." + exit 1 + } + echo "'$app_name' has been installed and is now executable." if [ -n "$(command -v "$app_name")" ]; then "$app_name" -v echo "'$app_name' is working." else - echo "'$app_name' is not working." + echo "'$app_name' is not working. Aborting..." + exit 1 fi else echo "The file '${app_name}.bash' was not found in '$app_path/src'." @@ -168,37 +188,85 @@ function uninstall { local download_path="" local bin_path="" - function set_paths { - if is_root; then - download_path="$CONST_GLOBAL_DOWNLOAD_PATH" - bin_path="$CONST_GLOBAL_BIN_PATH" - else - download_path="$CONST_USER_DOWNLOAD_PATH" - bin_path="$CONST_USER_BIN_PATH" - fi + echo + echo "Uninstalling..." + + local uninstall_success=false - echo "Donwload path: '$download_path'" - echo "Bin path: '$bin_path'" - } + if is_root; then + download_path="$CONST_GLOBAL_DOWNLOAD_PATH" + bin_path="$CONST_GLOBAL_BIN_PATH" + else + download_path="$CONST_USER_DOWNLOAD_PATH" + bin_path="$CONST_USER_BIN_PATH" + fi - set_paths + echo "Donwload path: '$download_path'" + echo "Bin path: '$bin_path'" for repo_url in "${CONST_REPO_URLS[@]}"; do app_name=$(get_app_name_from_repo_url "$repo_url") - app_path="$download_path/$app_name" - - if [ -L "$bin_path/$app_name" ]; then - echo "Removing symlink for '$app_name' in '$bin_path'..." - rm "$bin_path/$app_name" - fi - if [ -d "$app_path" ]; then - echo "Removing directory '$app_path'..." - rm -rf "$app_path" + if [ -d "$download_path/$app_name" ] && [ -L "$bin_path/$app_name" ]; then + read -r -p "Do you want to uninstall '$app_name'? [Y/n] " choice + if [[ "$choice" =~ ^[Yy]$ ]]; then + echo "Uninstalling '$app_name'..." + + echo "Removing symlink for '$app_name' in '$bin_path'..." + rm "$bin_path/$app_name" || + { + echo "Failed to remove symlink for '$app_name' in '$bin_path'." + exit 1 + } + + app_path="$download_path/$app_name" + echo "Removing directory '$app_path'..." + rm -rf "$app_path" || + { + echo "Failed to remove directory '$app_path'." + exit 1 + } + + echo "'$app_name' has been uninstalled." + uninstall_success=true + else + echo "Uninstallation for '$app_name' aborted." + fi else - echo "'$app_name' is not installed." + echo "'$app_name' is not installed?" + + if is_root; then + echo "You run this script as root. Please run it as the user who installed it to uninstall it." + else + echo "You run this script as a user. Please run it as root to uninstall it globally or with the user who installed it." + fi fi done + + if [ "$uninstall_success" = true ]; then + return 0 + else + return 1 + fi +} + +# ╔═════════════════════╦══════════════════════╗ +# ║ ║ +# ║ UPDATE ║ +# ║ ║ +# ╚═════════════════════╩══════════════════════╝ + +function update { + echo + echo "Updating..." + echo "Note: The update will uninstall and install it again." + + if uninstall; then + install + else + echo "Failed to uninstall. Aborting update..." + exit 1 + fi } # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ @@ -209,6 +277,8 @@ function uninstall { # ░░ ░░ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ +echo "Current user: '$(whoami)'" + case "$1" in install) install @@ -216,8 +286,11 @@ install) uninstall) uninstall ;; +update) + update + ;; *) - echo "Usage: $0 {install|uninstall}" + echo "Usage: $0 {install|uninstall|update}" exit 1 ;; esac From 344a29703440a2ec2c44c437c24f124fd5788f36 Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 22:09:12 +0100 Subject: [PATCH 53/54] Update README.md --- README.md | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index f11de3e..4571da2 100644 --- a/README.md +++ b/README.md @@ -16,24 +16,22 @@ ### What is `domposy`? -`domposy` is a Linux Bash script that helps to backup Docker Compose containers (with bind mounts). In addition, it is possible to perform a secure Docker cleanup of resources that are no longer used. +`domposy` is a Linux Bash script that helps to backup Docker Compose containers (with bind mounts). -This involves searching for the Docker Compose folder, which then also contains the `.yml` file, the bind mounts that are important for the container itself and hopefully an `.env` file. 😜 +This involves searching for the Docker Compose folder, which then also contains the `.yml` file, the bind mounts that are important for the container itself and hopefully an `.env` file. This entire folder is then securely **tared** and then **compressed**. The backup is then located where you want it. -Timestamps are also used for the backups. Help is also provided on how the backups can be reused. All important information is also logged. If no container is started or a backup is created, all Docker Compose containers are automatically started afterwards. +Timestamps are used for the backups. Help is also provided on how the backups can be reused. All important information is logged. -> It may be important to adjust the directories here. - -Finally, it is very easy to simply backup the whole thing to a NAS that has been mounted with NFS or SMB, for example. +Finally, it is very easy to simply backup to a NAS that has been mounted with NFS or SMB, for example. > Only Docker Compose files named `docker-compose.yml` or `docker-compose.yaml` are used for search! Ideally, it has the following structure: ```plain -/ +/ │ ├── docker-compose.yml │ @@ -42,7 +40,9 @@ Ideally, it has the following structure: └── volumes/ ``` -Where the files are located is almost irrelevant. Ideally, there should be a folder containing all the services. +Where the files are located is almost irrelevant. Ideally, there should be a folder containing all the Docker Compose projects. + +In addition, it is possible to perform a secure Docker cleanup of resources that are no longer used. **Examples for Docker Compose files**: @@ -54,18 +54,6 @@ Where the files are located is almost irrelevant. Ideally, there should be a fol This script is ideal for homelab enthusiasts, but also for people who work a lot with Docker Compose files. -> However, I do not currently recommend using it productively in a company. - -### The Goal of the `domposy` Project - -It is important to create backups. Unfortunately, it is not so easy for homelab enthusiasts in particular to do this easily and to run it as a cronjob. Again and again you have to write a script yourself, which is not so easy... To remedy this, there is exactly this. It is simply simple. It would be nice if Docker itself would provide such functionality in the future for exactly the kind of use case described above. - -## ⚠️ **Disclaimer - Important!** - -The whole thing is still at an early stage of development and can therefore lead to unexpected behaviour. - -> To be used with caution. - ## Getting Started The easiest way is to download and run the [`setup.bash`](./setup.bash) script. @@ -73,7 +61,7 @@ The easiest way is to download and run the [`setup.bash`](./setup.bash) script. > If you want to install it globally, you need root rights (sudo)!\ > Otherwise, it will be only installed for the current user! -The following command will download the script, make it executable, install the script and then delete it: +The following command will download `domposy`, make it executable, install it and then delete the `setup.bash` script: ```bash wget -q -O setup.bash https://raw.githubusercontent.com/fuchs-fabian/domposy/refs/heads/main/setup.bash && \ @@ -82,7 +70,7 @@ chmod +x setup.bash && \ rm setup.bash ``` -Then you can use the `domposy` command: +Then you can use `domposy`: ```plain It is recommended to run the script with root rights to ensure that the backups work properly. @@ -165,6 +153,10 @@ chmod +x setup.bash && \ rm setup.bash ``` +#### Advanced update method + +You can simply navigate to the directory in which the script is cloned, remove the execution rights and pull it. Then you can make it executable again. + ## Donate with [PayPal](https://www.paypal.com/donate/?hosted_button_id=4G9X8TDNYYNKG) If you think this tool is useful and saves you a lot of work and nerves and lets you sleep better, then a small donation would be very nice. From 23e653bd10092a5328e5d2e80ff280914ca155fb Mon Sep 17 00:00:00 2001 From: Fabian Fuchs Date: Mon, 11 Nov 2024 22:11:00 +0100 Subject: [PATCH 54/54] Change branch names in setup.bash --- setup.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.bash b/setup.bash index f0a7b22..17ead7d 100644 --- a/setup.bash +++ b/setup.bash @@ -9,10 +9,10 @@ # ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░ declare -r CONST_APP_NAME="domposy" -declare -r CONST_APP_BRANCH_NAME="v2" +declare -r CONST_APP_BRANCH_NAME="main" declare -r CONST_LOGGER_NAME="simbashlog" -declare -r CONST_LOGGER_BRANCH_NAME="v1.1.3" +declare -r CONST_LOGGER_BRANCH_NAME="main" declare -r CONST_GLOBAL_DOWNLOAD_PATH="/opt" declare -r CONST_GLOBAL_BIN_PATH="/usr/bin"