Useful MinIO scripts

nextcloud_archive_files.sh

#!/bin/bash
set -o pipefail

#############################################
# CONFIGURATION
#############################################
NEXTCLOUD_DATA_DIR="/path/to/nextcloud/data"
MINIO_ALIAS="myminio"
MINIO_BUCKET="bucket_name"
LOG_FILE="/path/to/minio.log"

# DRY RUN (set to false to actually upload & delete)
DRY_RUN=false

# Disable MC color output to avoid ANSI codes
export MC_COLOR=off

mkdir -p "$(dirname "$LOG_FILE")"
touch "$LOG_FILE"

#############################################
# SANITIZATION FUNCTIONS
#############################################

sanitize_string() {
    printf "%s" "$1" | tr -d '\000-\037\177'
}

log() {
    local clean_msg
    clean_msg=$(sanitize_string "$1")
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $clean_msg" >> "$LOG_FILE"
}

sanitize_file_on_disk() {
    local file="$1"
    local dir base cleanbase

    dir=$(dirname "$file")
    base=$(basename "$file")
    cleanbase=$(printf "%s" "$base" | tr -d '\000-\037\177')

    if [[ "$cleanbase" != "$base" ]]; then
        mv -n -- "$file" "$dir/$cleanbase"
        log "Renamed on disk: '$file' → '$dir/$cleanbase'"
    fi

    echo "$dir/$cleanbase"
}

#############################################
# ARCHIVE PROCESS
#############################################

log "===== Starting Nextcloud archive process ====="
log "DRY_RUN=$DRY_RUN"



find "$NEXTCLOUD_DATA_DIR" \
    -xdev \
    -type f \
    -mtime +90 \
    -size +200M \
    -print0 |
while IFS= read -r -d '' file; do

    # Sanitize filename on disk
    file=$(sanitize_file_on_disk "$file")

    # Build relative path
    relative_path="${file#$NEXTCLOUD_DATA_DIR/}"
    relative_path=$(sanitize_string "$relative_path")

    target="$MINIO_ALIAS/$MINIO_BUCKET/$relative_path"

    log "Processing: '$file' → '$target'"

    if [[ "$DRY_RUN" == true ]]; then
        log "DRY-RUN: Would upload and delete '$file'"
        continue
    fi

    mc_output=$(mc cp -- "$file" "$target" 2>&1 | tr -d '\000-\037\177')
    mc_exit=$?

    if [[ $mc_exit -eq 0 ]]; then
        log "SUCCESS: Uploaded '$file'"
        rm -f -- "$file"
        log "Deleted local copy: '$file'"
    else
        log "ERROR: Upload failed for '$file'"
        log "mc error: $mc_output"
    fi

done

log "===== Completed Nextcloud archive process ====="

create_user.sh

#!/bin/bash

# MinIO Server Details
MINIO_HOST="http://MINIO_HOST:9000"  # replace with your MinIO endpoint
MINIO_ROOT_USER="########"          # replace with your root username
MINIO_ROOT_PASSWORD="#########"      # replace with your root password

# MinIO Client (mc) alias setup
MC_ALIAS="myminio"

# Check if bucket name is provided as an argument
if [ -z "$1" ]; then
    echo "Usage: $0 "
    exit 1
fi

# Bucket name passed as argument
BUCKET="$1"
USERNAME="${BUCKET}-user"
POLICY_NAME="policy-${BUCKET}"

# Set mc alias
mc alias set $MC_ALIAS $MINIO_HOST $MINIO_ROOT_USER $MINIO_ROOT_PASSWORD

# Create the user
create_user() {
    # Create user with a randomly generated secret key (or use your own password)
    USER_SECRET=$(openssl rand -base64 32)
    mc admin user add $MC_ALIAS $USERNAME $USER_SECRET

    # Create a custom policy for the bucket
    cat < /tmp/${POLICY_NAME}.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetBucketLocation",
        "s3:ListBucket"
      ],
      "Resource": "arn:aws:s3:::${BUCKET}"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject"
      ],
      "Resource": "arn:aws:s3:::${BUCKET}/*"
    }
  ]
}
EOF

    # Attach the policy to the user
    mc admin policy add $MC_ALIAS $POLICY_NAME /tmp/${POLICY_NAME}.json
    mc admin policy attach $MC_ALIAS $POLICY_NAME --user $USERNAME

    # Clean up the policy file
    rm /tmp/${POLICY_NAME}.json

    echo "User $USERNAME created with policy for bucket $BUCKET"
    echo "User credentials: ACCESSKEY: $USERNAME, SECRETKEY: $USER_SECRET"
}

# Check if the bucket exists before creating the user and policy
mc ls $MC_ALIAS/$BUCKET &> /dev/null
if [ $? -ne 0 ]; then
    echo "Bucket $BUCKET does not exist. Please create the bucket first."
    exit 1
fi

# Call the function to create the user and attach the policy
create_user