forked from toolshed/abra-bash
		
	Add shared backup helpers
This commit is contained in:
		
							
								
								
									
										86
									
								
								abra
									
									
									
									
									
								
							
							
						
						
									
										86
									
								
								abra
									
									
									
									
									
								
							@ -3,6 +3,7 @@
 | 
			
		||||
GIT_URL="https://git.autonomic.zone/coop-cloud/"
 | 
			
		||||
ABRA_DIR="${ABRA_DIR:-$HOME/.abra}"
 | 
			
		||||
ABRA_VERSION="0.4.1"
 | 
			
		||||
ABRA_BACKUP_DIR="${ABRA_BACKUP_DIR:-$ABRA_DIR/backups}"
 | 
			
		||||
 | 
			
		||||
#######################################
 | 
			
		||||
# Global help
 | 
			
		||||
@ -345,6 +346,22 @@ debug() {
 | 
			
		||||
  echo "$(tput setaf 13)DEBUG: $*$(tput sgr0)"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# 3wc: temporarily disable debug and verbose
 | 
			
		||||
 | 
			
		||||
silence() {
 | 
			
		||||
  # FIXME 3wc: required otherwise we get debug output in the password
 | 
			
		||||
  _abra___debug="$abra___debug"
 | 
			
		||||
  _abra___verbose="$abra___verbose"
 | 
			
		||||
  abra___verbose="false"
 | 
			
		||||
  abra___debug="false"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unsilence() {
 | 
			
		||||
  # FIXME 3wc: required otherwise we get debug output in the password
 | 
			
		||||
  abra___verbose="$_abra___verbose"
 | 
			
		||||
  abra___debug="$_abra___debug"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
###### Default settings
 | 
			
		||||
 | 
			
		||||
if [ -z "$COMPOSE_FILE" ]; then
 | 
			
		||||
@ -480,7 +497,7 @@ load_instance_env() {
 | 
			
		||||
  APP_DIR="$ABRA_DIR/apps/$TYPE"
 | 
			
		||||
  export DOCKER_CONTEXT="$SERVER"
 | 
			
		||||
  info "Setting DOCKER_CONTEXT=$DOCKER_CONTEXT"
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  export DOMAIN
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -777,6 +794,73 @@ sub_app_restore (){
 | 
			
		||||
  $FUNCTION "$abra__backup_file_"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
###### backup utility functions
 | 
			
		||||
 | 
			
		||||
# Usage: _abra_backup_dir service:/path/to/src
 | 
			
		||||
_abra_backup_dir() {
 | 
			
		||||
  {
 | 
			
		||||
    abra__src_="$1"
 | 
			
		||||
    abra__dst_="-"
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  # shellcheck disable=SC2154
 | 
			
		||||
  FILENAME="$ABRA_BACKUP_DIR/${abra__app_}_$(basename "$1")_$(date +%F).tar.gz"
 | 
			
		||||
 | 
			
		||||
  debug "Copying '$1' to '$FILENAME'"
 | 
			
		||||
 | 
			
		||||
  silence
 | 
			
		||||
  sub_app_cp | gzip > "$FILENAME"
 | 
			
		||||
  success "Backed up '$1' to $FILENAME"
 | 
			
		||||
  unsilence
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
_abra_backup_db_prep() {
 | 
			
		||||
  # shellcheck disable=SC2034
 | 
			
		||||
  abra__service_="$1"
 | 
			
		||||
  # 3wc: necessary because $abra__service_ won't be set if we're coming from
 | 
			
		||||
  # `abra_backup`, i.e. `abra app ... backup --all`
 | 
			
		||||
 | 
			
		||||
  # What's the name of the Docker secret? Default to db_root_password
 | 
			
		||||
  DB_PASSWORD_NAME=${4:-db_root_password}
 | 
			
		||||
 | 
			
		||||
  debug "Looking up secret '$DB_PASSWORD_NAME'"
 | 
			
		||||
  silence
 | 
			
		||||
  DB_PASSWORD="$(sub_app_run cat "/run/secrets/$DB_PASSWORD_NAME")"
 | 
			
		||||
  unsilence
 | 
			
		||||
 | 
			
		||||
  # 3wc: strip newline \r from variable
 | 
			
		||||
  DB_PASSWORD="${DB_PASSWORD//$'\015'}"
 | 
			
		||||
 | 
			
		||||
  # shellcheck disable=SC2154
 | 
			
		||||
  FILENAME="$ABRA_BACKUP_DIR/${abra__app_}_$(date +%F).sql.gz"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# usage: _abra_backup_postgres <service> <database> [<user> <secret name>]
 | 
			
		||||
_abra_backup_postgres() {
 | 
			
		||||
  _abra_backup_db_prep "$@"
 | 
			
		||||
 | 
			
		||||
  debug "Running pg_dump to '$FILENAME'"
 | 
			
		||||
 | 
			
		||||
  silence
 | 
			
		||||
  # shellcheck disable=SC2034
 | 
			
		||||
  PGPASSWORD="$DB_PASSWORD"
 | 
			
		||||
  sub_app_run pg_dump -U "${3:-postgres}" "$2" | gzip > "$FILENAME"
 | 
			
		||||
  unsilence
 | 
			
		||||
 | 
			
		||||
  success "Backed up '$abra__service_:$2' to '$FILENAME'"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
_abra_backup_mysql() {
 | 
			
		||||
  _abra_backup_db_prep "$@"
 | 
			
		||||
 | 
			
		||||
  silence
 | 
			
		||||
  # shellcheck disable=SC2086
 | 
			
		||||
  sub_app_run mysqldump -u root -p"${DB_PASSWORD}" "$2" | gzip > "$FILENAME"
 | 
			
		||||
  unsilence
 | 
			
		||||
 | 
			
		||||
  success "Backed up '$abra__service_:$2' to $FILENAME"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
###### .. app deploy
 | 
			
		||||
help_app_deploy (){
 | 
			
		||||
  echo "abra [options] app <app> deploy [--skip-check]
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user