diff --git a/backups/backup_postgres.sh b/backups/backup_postgres.sh new file mode 100755 index 0000000000000000000000000000000000000000..228fef63c3884e2f16b4a97e8af92ce4f2eeaecd --- /dev/null +++ b/backups/backup_postgres.sh @@ -0,0 +1,189 @@ +#!/usr/bin/env bash +### + # Creates a compressed backup of the provided PostgreSQL database. + # + # + # Required Args: + # * Database User + # * Database Password + # * Database Name + # + # Optional Kwargs: + # * Database Ip - Ip address for PostgreSQL server. Defaults to 127.0.0.1 if not provided. + # * Accepted Kwargs: database_ip / db_ip / ip + # * Database Port - Port for PostgreSQL server. Defaults to 3306 if not provided. + # * Accepted Kwargs: database_port / db_port / port + # * Backup Folder - Folder to backup database to. + # * Accepted Kwargs: backup_folder / backup_location / backup_path + # + # Optional Flags: + # * Force - Force script to execute without confirmation of provided args. + # * Accepted Flags: f / force + # * Delete Previous Backups - Clear backups folder of all files that match corresponding database, prior to backup. + # * Accepted Flags: d / delete_previous_backups / delete_prev_backups / delete_prev + # + # + # https://git.brandon-rodriguez.com/scripts/bash/server_management + # Version 1.0 + ## + + +# Define possible flag values for script. +possible_flags=("-f" "-force" "-d" "-delete_previous_backups" "-delete_prev_backups" "-delete_prev") + + +# Import utils script. +. $(dirname ${0})/../utils.sh + + +# Stop on error. +set -e + + +# Global Variables. +db_user="" +db_pass="" +db_name="" +db_ip="127.0.0.1" +db_port="5432" +get_absolute_path "./" +backup_folder=${return_value} +current_date=`date +%Y-%m-%d` + + +### + # Script entry point. + ## +function main () { + + # Check for provided args. + validate_args + + # Confirm args if force flag was not provided. + if [[ ! ${flags[@]} =~ "f" && ! ${flags[@]} =~ "force" ]] + then + confirm_args + fi + + backup_db +} + + + +### + # Validate provided script args. + ## +function validate_args () { + + # Check for minimum number of args. + if [[ ${#args[@]} < 3 ]] + then + echo -e "{$text_red}Missing one or more args. Require at minimum [ db_user db_password db_name ].${text_reset}" + exit 0 + fi + + # Grab args. + db_user="${args[0]}" + db_pass="${args[1]}" + db_name="${args[2]}" + + # Check for database ip kwarg. + if [[ "${!kwargs[@]}" =~ "ip" ]] + then + db_ip="${kwargs[ip]}" + + elif [[ "${!kwargs[@]}" =~ "db_ip" ]] + then + db_ip="${kwargs[db_ip]}" + + elif [[ "${!kwargs[@]}" =~ "database_ip" ]] + then + db_ip="${kwargs[database_ip]}" + fi + + # Check for database port kwarg. + if [[ "${!kwargs[@]}" =~ "port" ]] + then + db_port="${kwargs[port]}" + + elif [[ "${!kwargs[@]}" =~ "db_port" ]] + then + db_port="${kwargs[db_port]}" + + elif [[ "${!kwargs[@]}" =~ "database_port" ]] + then + db_port="${kwargs[database_port]}" + fi + + # Check for backup folder kwarg. + if [[ "${!kwargs[@]}" =~ "backup_folder" ]] + then + get_absolute_path "${kwargs[backup_folder]}" + backup_folder="${return_value}" + + elif [[ "${!kwargs[@]}" =~ "backup_location" ]] + then + get_absolute_path "${kwargs[backup_location]}" + backup_folder="${return_value}" + + elif [[ "${!kwargs[@]}" =~ "backup_path" ]] + then + get_absolute_path "${kwargs[backup_path]}" + backup_folder="${return_value}" + fi + + # Validate backup folder. + if [[ ! -d ${backup_folder} ]] + then + echo -e "${text_red}Provided backup location is not a folder, or does not exist.${text_reset}" + echo -e "${text_red}Please verify the location exists, and that the location points to a folder, not a file.${text_reset}" + exit 0 + fi +} + + +### + # Prompts user to confirm input args. + # Skipped if the -f flag is provided. + ## +function confirm_args () { + + echo "" + echo "Found provided args:" + echo " db_user: ${db_user}" + echo " db_name: ${db_name}" + echo " db_ip: ${db_ip}" + echo " db_port: ${db_port}" + echo " backup_folder: ${backup_folder}" + echo "" + + get_user_confirmation "Is this correct?" + if [[ ${return_value} == false ]] + then + echo -e "${text_orange}Please correct provided args and rerun script.${text_reset}" + exit 0 + fi +} + + + +### + # Logic to backup database. + ## +function backup_db () { + + # Check for "delete previous backups" flags. + if [[ ${flags[@]} =~ "d" || ${flags[@]} =~ "delete_previous_backups" || ${flags[@]} =~ "delete_prev_backups" || ${flags[@]} =~ "delete_prev" ]] + then + echo "clearing out backups at ${backup_folder}/${db_name}__*" + + fi + + # Execute backup. + pg_dump --dbname=postgresql://${db_user}:${db_pass}@${db_ip}:${db_port}/${db_name} | gzip > "${backup_folder}/${db_name}__${current_date}.sql.gz" + echo -e "${text_blue}PostgreSQL database contents backed up to \"${backup_location}/${db_name}__${current_date}.sql.gz\".${text_reset}" + +} + + +main diff --git a/readme.md b/readme.md index fec397e9c85bb346b5b3a83d99389017dca9d7c5..5d770fa20aaa584d78b72ce66c7c20d647f8193a 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,10 @@ # Bash - Server Management Scripts +## Version +Repo version: 1.1.0 + + ## Description Various scripts to help with management of servers. Most centered around data backups and related maintenance.