scrobble.life
#ipfs

Just a script I made to back up IPFS files into my personal Kubo.

As most people who know me are aware, I enjoy making experiments with Hive and IPFS aiming at free data distribution in decentralized ways.

#!/bin/bash

HIVE_RPC="https://api.hive.blog"
IPFS_API="http://127.0.0.1:5001"
MFS_DIR="/hive"
LAST_FILE="/home/fernando/Documentos/last.txt"

MAX_JOBS=100
TTL=10


if [ -f "$LAST_FILE" ]; then
    LAST=$(<"$LAST_FILE")
else
    LAST=$(curl -s "$HIVE_RPC" \
        -H "Content-Type: application/json" \
        -d '{
            "jsonrpc":"2.0",
            "method":"condenser_api.get_dynamic_global_properties",
            "params":[],
            "id":1
        }' \
        | jq -r '.result.head_block_number')
    echo "$LAST" > "$LAST_FILE"
fi

while true; do
    HEAD=$(curl -s "$HIVE_RPC" \
        -H "Content-Type: application/json" \
        -d '{
            "jsonrpc":"2.0",
            "method":"condenser_api.get_dynamic_global_properties",
            "params":[],
            "id":1
        }' \
        | jq -r '.result.head_block_number')

    echo "Head block: $HEAD"
    curl -X POST -s "$IPFS_API/api/v0/files/mkdir?arg=$MFS_DIR&parents=true"
    if (( LAST < HEAD - 100 )); then
        TTL=60
    else
        TTL=3600
    fi
    while [ "$LAST" -lt "$HEAD" ]; do
        echo "Last block: $LAST | HTTP Jobs: $(pgrep -af "pin/add|files/cp" | wc -l) | $((HEAD - LAST)) behind"
        echo "$LAST" > "$LAST_FILE"
        LAST=$((LAST+1))
        BLOCK=$(curl -s "$HIVE_RPC" \
            -H "Content-Type: application/json" \
            -d "{
                \"jsonrpc\":\"2.0\",
                \"method\":\"block_api.get_block\",
                \"params\":{\"block_num\":$LAST},
                \"id\":2
            }")
        echo "$BLOCK" |
        jq -r '.. | strings' |
        grep -Eo '(Qm[1-9A-HJ-NP-Za-km-z]{44}|bafy[a-z0-9]{20,})' |
        sort -u |
        while read -r CID; do
            echo "Found CID: $CID"
            while [ "$(pgrep -af "pin/add|files/cp" | wc -l)" -ge "$MAX_JOBS" ]; do
                sleep 0.1
                echo -e "\e[1A\e[KLast block: $LAST | HTTP Jobs: $(pgrep -af "pin/add|files/cp" | wc -l) | $((HEAD - LAST)) behind"
            done
            FILE="$(date +%s%N)"
            curl --max-time "$TTL" -X POST -s "$IPFS_API/api/v0/pin/add?arg=$CID" &&
            curl --max-time "$TTL" -X POST -s "$IPFS_API/api/v0/files/cp?arg=/ipfs/$CID&arg=$MFS_DIR/$FILE" &
        done
        sleep 0.1
    done
    sleep 3
done

If you trust my capability to run a stable and updated Hive Witness...

Vote for me as your Hive Witness


If you want to support Brazilian creators...

Vote curator @perfilbrasil for Hive Witness

Delegations to @perfilbrasil are rewarded here.

Comments

No comments yet — be the first.

Just a script I made to back up IPFS files into my personal Kubo. — scrobble.life