Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-17b-128e-instruct· MetaHugging Face LMArena Human Preference 140K

explain this code [code]

  1. User

    explain this code

    #!/data/data/com.termux/files/usr/bin/bash
    
    HOST="borgbackup.location.example.com"
    HOSTNAME="cplus7pro"
    TARGET="user@${HOST}:/mnt/backup/${HOSTNAME}"
    
    export BORG_PASSPHRASE="<your-chosen-passphrase>"
    
    declare -a TERMUX_NOTIFICATIONS=()
    TERMUX_NOTIFICATION_ID="borgbackup-${HOSTNAME}"
    
    set -o pipefail
    
    cleanup() {
      for notification in "${TERMUX_NOTIFICATIONS[@]}"; do
        termux-notification-remove "$notification"
      done
      termux-wake-unlock
    }
    
    ##
    # Send a notification to the user.
    #
    # Usage: echo "message" | notify persist identifier [options...]
    #
    # If persist is 0, the notification will be removed when the script exits.
    # Otherwise, it will be kept (e.g. for warning or error messages).
    #
    # The identifier can be used to overwrite a previous notification with the same
    # identifier. This can be useful for progress messages.
    #
    # Further options are those supported by termux-notification. The message must
    # be passed on stdin.
    notify() {
      local persist=$1
      shift
      local id="$1"
      shift
      local -a args=("--group" "${TERMUX_NOTIFICATION_ID}" "--id" "$id")
    
      if termux-notification "${args[@]}" "$@"; then
        if [ "$persist" -eq 0 ]; then
          TERMUX_NOTIFICATIONS+=("$id")
        fi
      fi
    }
    
    msg() {
      echo "***" "$@"
    }
    
    info() {
      msg "INFO:" "$@"
      termux-toast -s "$*"
    }
    
    warn() {
      msg "WARN:" "$@"
      echo "Warning:" "$@" | \
        notify 1 failure \
          --title "borgbackup" \
          --alert-once \
          --priority low
    }
    
    err() {
      msg "ERROR:" "$@"
      echo "Error:" "$@" | \
        notify 1 failure \
          --title "borgbackup" \
          --alert-once \
          --priority high
      exit 1
    }
    
    prepare() {
      if ! termux-wifi-connectioninfo | grep "supplicant_state" | grep -q "COMPLETED"; then
        warn "WiFi not connected, not performing backup"
        return 1
      fi
      if ! ping -w 10 -c 3 "$HOST" >/dev/null; then
        warn "Failed to ping target $HOST"
        return 1
      fi
    }
    
    backup() {
      local -a flags=()
    
      # enable interactive output
      if [ -t 0 ] && [ -t 1 ]; then
        flags+=('--stats' '--progress' '--list')
      fi
    
      info "Starting backup"
      ionice -c 3 \
        nice -n20 \
        borg create \
        --noatime \
        --compression='lz4' \
        --exclude-caches \
        --exclude='fm:/storage/emulated/0/*/.thumbnails' \
        --exclude='pp:/storage/emulated/0/Android/data' \
        --exclude='pp:/storage/emulated/0/Android/obb' \
        "${flags[@]}" \
        "${TARGET}::${HOSTNAME}-{utcnow:%Y-%m-%dT%H:%M:%S}" \
        /storage/emulated/0/ \
        /data/data/com.termux/files/home
    }
    
    prune() {
      local -a flags=()
    
      # enable interactive output
      if [ -t 0 ] && [ -t 1 ]; then
        flags+=('--stats' '--list')
      fi
    
      info "Pruning old backups..."
      borg prune \
        --prefix="${HOSTNAME}-" \
        --keep-within=14d \
        --keep-daily=31 \
        --keep-weekly=$((6 * 4)) \
        --keep-monthly=$(( 2 * 12 )) \
        "${flags[@]}" \
        "${TARGET}"
    }
    
    # Run once per day, unless BORGBACKUP_FORCE=1
    MARKER_FILE=~/.borgbackup-"${HOSTNAME}-$(date +%Y-%m-%d)"
    if [ "${BORGBACKUP_FORCE:-0}" -eq 0 ]; then
      if [ "$(date +%H)" -lt 4 ]; then
        echo "Backup not yet due, waiting..."
        exit 0
      elif [ -f "$MARKER_FILE" ]; then
        echo "Backup already ran today"
        exit 0
      fi
    fi
    
    if ! prepare; then
      info "Server connectivity or charging status does not meet expectations, skipping backup."
      exit 1
    fi
    rm -f ~/.borgbackup-"${HOSTNAME}"-*
    touch "$MARKER_FILE"
    
    trap "cleanup" EXIT
    termux-wake-lock
    notify 0 progress \
      --alert-once \
      --ongoing \
      --priority low \
      --title "borgbackup" \
      --content "Running backup for ${HOSTNAME}"
    
    if ! backup; then
      err "Backup failed, aborting!"
    fi
    if ! prune; then
      warn "Pruning failed. Continuing anyway."
    fi
    
    info "Backup finished successfully"
    
  2. Meta Assistant

    Overview

    This is a Bash script designed to run on Android using Termux, a terminal emulator and Linux environment. The script performs a backup of the device's data using BorgBackup, a deduplicating backup tool.

    Configuration

    The script is configured with the following variables:

    • HOST: The hostname or IP address of the BorgBackup server.
    • HOSTNAME: The hostname of the device being backed up.
    • TARGET: The target location for the backup on the BorgBackup server.
    • BORG_PASSPHRASE: The passphrase for the BorgBackup repository.

    Functions

    The script defines several functions:

    cleanup()

    • Removes any notifications created by the script.
    • Unlocks the device's wake lock.

    notify()

    • Sends a notification to the user using Termux's notification system.
    • Takes three arguments: persist, id, and options.
    • If persist is 0, the notification will be removed when the script exits.
    • id is used to identify the notification and can be used to overwrite previous notifications.

    msg(), info(), warn(), err()

    • These functions are used to log messages at different levels:
      • msg(): Logs a message with a prefix.
      • info(): Logs an informational message and displays a toast notification.
      • warn(): Logs a warning message and displays a notification.
      • err(): Logs an error message, displays a notification, and exits the script.

    prepare()

    • Checks if the device is connected to WiFi and can ping the BorgBackup server.
    • Returns 1 if the checks fail.

    backup()

    • Performs a BorgBackup backup of the device's data.
    • Uses the borg create command with various options, including compression and exclusion rules.

    prune()

    • Prunes old backups on the BorgBackup server using the borg prune command.
    • Uses various retention policies to determine which backups to keep.

    Main Script

    The script performs the following steps:

    1. Checks if the backup is due to run based on the BORGBACKUP_FORCE environment variable and the current time.
    2. If the backup is not due, exits the script.
    3. Calls the prepare() function to check the device's connectivity and charging status.
    4. If the prepare() function fails, exits the script.
    5. Creates a marker file to indicate that the backup has run.
    6. Sets up a trap to call the cleanup() function when the script exits.
    7. Acquires a wake lock to prevent the device from sleeping during the backup.
    8. Calls the backup() function to perform the backup.
    9. If the backup fails, calls the err() function to log an error and exit.
    10. Calls the prune() function to prune old backups.
    11. If the pruning fails, calls the warn() function to log a warning.
    12. Logs a success message and exits the script.

    Notes

    • The script uses Termux's notification system to display notifications to the user.
    • The script uses BorgBackup's deduplicating features to reduce the size of the backup.
    • The script uses various exclusion rules to exclude certain files and directories from the backup.
    • The script uses retention policies to determine which backups to keep on the BorgBackup server.