diff --git a/docker-backup.sh b/docker-backup.sh index b6e6d6c..92ca595 100644 --- a/docker-backup.sh +++ b/docker-backup.sh @@ -1,17 +1,44 @@ #!/bin/bash +function usage() { + cat < $2" + fi +} + +function copyVolumes() { + echo "Copying volumes..." + local volumes=($(find "$1/volumes" -maxdepth 1 -mindepth 1 -type d)) + for volume in "${volumes[@]}"; do + local dest="${volume#"$1"/volumes*}" + copyIfNotDebug "$volume" "$dest" + done +} + +function copyStacks() { + echo "Copying stacks..." + local stacks=($(find "$1/stacks" -maxdepth 1 -mindepth 1 -type d)) + for stack in "${stacks[@]}"; do + local dest="${stack#"$1"/stacks*}" + copyIfNotDebug "$stack" "$dest" + done +} + +function run() { # stop services. stopServices @@ -89,4 +120,56 @@ function main() { cleanBackups } +function restore() { + # Parse the backup to use, if an argument is passed in use that as the source + # for the backup, otherwise use the last backup. + local archive="$1" + if [ -z "$archive" ]; then + echo "Backup not specified using latest backup..." + archive=$(find /backups -maxdepth 1 -mindepth 1 -name "*.tar.gz" | sort -nr | head -1) + fi + + local dir="${archive%*.tar.gz}" + + # unzip the archive. + echo "Unzipping backup: $archive" + tar -xvf "$archive" + + # copy stacks and volumes to their locations. + copyVolumes "$dir" + copyStacks "$dir" + + # cleanup + rm -rf "$dir" + + echo "Restarting services..." + startServices + +} + +# ================================================== +# MAIN +# ================================================== + +function main() { + + case "$1" in + -h | --help) + usage && return 0 + ;; + --version) + echo "$version" && return 0 + ;; + restart) + startServices && return 0 + ;; + restore) + shift && restore "$@" && return 0 + ;; + *) + run "$@" && return 0 + ;; + esac +} + main "$@" diff --git a/docker-restart.sh b/docker-restart.sh deleted file mode 100644 index 955dfae..0000000 --- a/docker-restart.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -# Restart all stacks / services located in '/etc/komodo/stacks'. - -stackdir="/etc/komodo/stacks" -dirs=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d)) - -for dir in "${dirs[@]}"; do - cd "$dir" && docker compose up -d -done diff --git a/docker-restore.sh b/docker-restore.sh deleted file mode 100644 index a9d588e..0000000 --- a/docker-restore.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -debug=${DEBUG:-} - -function copyIfNotDebug { - if [ -z "$debug" ]; then - cp -r "$1" "$2" - else - echo "$1 -> $2" - fi -} - -function copyVolumes() { - echo "Copying volumes..." - local volumes=($(find "$1/volumes" -maxdepth 1 -mindepth 1 -type d)) - for volume in "${volumes[@]}"; do - local dest="${volume#"$1"/volumes*}" - copyIfNotDebug "$volume" "$dest" - done -} - -function copyStacks() { - echo "Copying stacks..." - local stacks=($(find "$1/stacks" -maxdepth 1 -mindepth 1 -type d)) - for stack in "${stacks[@]}"; do - local dest="${stack#"$1"/stacks*}" - copyIfNotDebug "$stack" "$dest" - done -} - -function main() { - local archive="$1" - local dir="${archive%*.tar.gz}" - - # unzip the archive. - tar -xvf "$archive" - - # copy stacks and volumes to their locations. - copyVolumes "$dir" - copyStacks "$dir" - - # cleanup - rm -rf "$dir" -} - -main "$@" diff --git a/install.sh b/install.sh index b25e8d0..84b7b7d 100644 --- a/install.sh +++ b/install.sh @@ -5,21 +5,24 @@ systemdir="/etc/systemd/system" bindir="/usr/local/bin" function downloadScripts() { - local scripts=( - "docker-backup" + local script="docker-backup" + + local scriptsToRemove=( "docker-restore" "docker-restart" ) - for script in "${scripts[@]}"; do - # set the destination of the script. - local dest="$bindir/$script" - # Remove the file if it exists. - [ -f "$dest" ] && rm -f "$dest" - # download the script to the destination. - wget "$baseurl/$script.sh" -O "$dest" - # make the script executable. - chmod +x "$dest" + # set the destination of the script. + local dest="$bindir/$script" + # Remove the file if it exists. + [ -f "$dest" ] && rm -f "$dest" + # download the script to the destination. + wget "$baseurl/$script.sh" -O "$dest" + # make the script executable. + chmod +x "$dest" + + for script in "${scriptsToRemove[@]}"; do + [ -f "$script" ] && rm -f "$script" done }