← Back
Editing: updateazur.sh
#!/bin/bash # ============================================================================= # update-wp-backups.sh # # USAGE: # ./update-wp-backups.sh <NEW_AZURE_STORAGE_ACCOUNT> # # DESCRIPTION: # Step 1 - Authenticates the server to Azure using a Service Principal # (App Registration credentials stored as env vars or prompted). # Step 2 - Replaces the AZURE_STORAGE_ACCOUNT value in all # wp-backup-*.sh scripts under /usr/local/bin/. # # PREREQUISITES: # - Azure CLI (az) installed # - Service Principal with appropriate permissions on the storage account # # ENVIRONMENT VARIABLES (set these before running, or script will prompt): # AZURE_APP_CLIENT_ID - The Service Principal Application (client) ID # AZURE_APP_CLIENT_SECRET - The Service Principal client secret # AZURE_TENANT_ID - Your Azure Active Directory tenant ID # ============================================================================= set -euo pipefail # ─── Colours ────────────────────────────────────────────────────────────────── RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' # No Colour log_info() { echo -e "${CYAN}[INFO]${NC} $*"; } log_success() { echo -e "${GREEN}[OK]${NC} $*"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; } # ============================================================================= # ARGUMENT VALIDATION # ============================================================================= if [[ $# -lt 1 ]]; then log_error "Missing required argument: NEW_AZURE_STORAGE_ACCOUNT" echo "" echo " Usage: $0 <NEW_AZURE_STORAGE_ACCOUNT>" echo " Example: $0 mynewstorageaccount" exit 1 fi NEW_AZURE_STORAGE_ACCOUNT="$1" # ============================================================================= # STEP 1 — AZURE SERVICE PRINCIPAL AUTHENTICATION # ============================================================================= echo "" echo "============================================================" echo " STEP 1: Azure Service Principal Authentication" echo "============================================================" # Check az CLI is available if ! command -v az &>/dev/null; then log_error "Azure CLI (az) is not installed. Install it from: https://docs.microsoft.com/cli/azure/install-azure-cli" exit 1 fi # Read credentials from env vars, or prompt securely if [[ -z "${AZURE_APP_CLIENT_ID:-}" ]]; then read -rp " Enter Azure App (client) ID: " AZURE_APP_CLIENT_ID fi if [[ -z "${AZURE_APP_CLIENT_SECRET:-}" ]]; then read -rsp " Enter Azure client secret: " AZURE_APP_CLIENT_SECRET echo "" fi if [[ -z "${AZURE_TENANT_ID:-}" ]]; then read -rp " Enter Azure Tenant ID: " AZURE_TENANT_ID fi # Validate none are empty after prompting if [[ -z "$AZURE_APP_CLIENT_ID" || -z "$AZURE_APP_CLIENT_SECRET" || -z "$AZURE_TENANT_ID" ]]; then log_error "All three credentials are required (client ID, secret, tenant ID)." exit 1 fi log_info "Logging in to Azure as Service Principal..." if az login \ --service-principal \ --username "$AZURE_APP_CLIENT_ID" \ --password "$AZURE_APP_CLIENT_SECRET" \ --tenant "$AZURE_TENANT_ID" \ --output none 2>&1; then log_success "Azure authentication successful." else log_error "Azure login failed. Check your credentials and try again." exit 1 fi # Optional: verify access to the target storage account log_info "Verifying access to storage account '${NEW_AZURE_STORAGE_ACCOUNT}'..." if az storage account show \ --name "$NEW_AZURE_STORAGE_ACCOUNT" \ --output none 2>/dev/null; then log_success "Storage account '${NEW_AZURE_STORAGE_ACCOUNT}' is accessible." else log_warn "Could not verify storage account '${NEW_AZURE_STORAGE_ACCOUNT}'." log_warn "This may be a permissions issue, or the account name may be wrong." log_warn "Continuing anyway — the script replacement will still proceed." fi # ============================================================================= # STEP 2 — UPDATE AZURE_STORAGE_ACCOUNT IN WP BACKUP SCRIPTS # ============================================================================= echo "" echo "============================================================" echo " STEP 2: Update AZURE_STORAGE_ACCOUNT in wp-backup scripts" echo "============================================================" log_info "Target storage account : ${NEW_AZURE_STORAGE_ACCOUNT}" log_info "Scanning : /usr/local/bin/wp-backup-*.sh" echo "" UPDATED=0 SKIPPED=0 ERRORS=0 # Build the sed pattern to match ANY current value, not just a hardcoded one # This replaces: AZURE_STORAGE_ACCOUNT="<anything>" SEARCH_PATTERN='AZURE_STORAGE_ACCOUNT="[^"]*"' REPLACE_VALUE="AZURE_STORAGE_ACCOUNT=\"${NEW_AZURE_STORAGE_ACCOUNT}\"" shopt -s nullglob # Don't error if glob matches nothing SCRIPTS=(/usr/local/bin/wp-backup-*.sh) if [[ ${#SCRIPTS[@]} -eq 0 ]]; then log_warn "No wp-backup-*.sh scripts found in /usr/local/bin/. Nothing to update." exit 0 fi for script in "${SCRIPTS[@]}"; do if [[ ! -f "$script" ]]; then log_warn "Not a regular file, skipping: $script" ((SKIPPED++)) || true continue fi if grep -qE "$SEARCH_PATTERN" "$script"; then # Get the current value for logging CURRENT_VAL=$(grep -oE "$SEARCH_PATTERN" "$script" | head -1 | sed 's/AZURE_STORAGE_ACCOUNT=//') if sed -i -E "s|${SEARCH_PATTERN}|${REPLACE_VALUE}|g" "$script"; then log_success "Updated : $(basename "$script") (was ${CURRENT_VAL})" ((UPDATED++)) || true else log_error "Failed to update: $script" ((ERRORS++)) || true fi else log_warn "Skipped : $(basename "$script") (AZURE_STORAGE_ACCOUNT not found)" ((SKIPPED++)) || true fi done # ============================================================================= # SUMMARY # ============================================================================= echo "" echo "============================================================" echo " SUMMARY" echo "============================================================" echo -e " ${GREEN}Updated : ${UPDATED}${NC}" echo -e " ${YELLOW}Skipped : ${SKIPPED}${NC}" [[ $ERRORS -gt 0 ]] && echo -e " ${RED}Errors : ${ERRORS}${NC}" echo "" if [[ $ERRORS -gt 0 ]]; then log_error "Completed with errors." exit 1 else log_success "Done." fi
Save File
Cancel