#!/bin/bash function usage() { cat < $2" fi } function restoreVolumes() { 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 restoreStacks() { 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 # Create a temporary directory. local temp="/$today" mkdir "$temp" # copy stack configuration. backupStacks "$temp" # copy stack configuration. backupVolumes "$temp" # Create a tar to backup. backup "$temp" # Restart services. startServices # Remove backups older than 7 days. 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. restoreVolumes "$dir" restoreStacks "$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 "$@"