qfdk

qfdk

喜欢碎碎念的小 🐭, 对开源情有独钟, 会说 🇫🇷, 喜欢折腾 “黑科技”, 徒步是日常
github

Trying to create an automatic pinning service for IPFS.

Introduction#

After using IPFS for about two weeks, I gradually understood the operation principle of IPFS. At the beginning, I had no understanding of this concept when using Planet, which resulted in frequent unresponsiveness of my blog and even timeouts when accessing the public network gateway. I always thought that this service was not very stable.

Solution - filebase#

First, you need more friends to help pin your content. It is understood that pinning only pins the current CID, so if the content is changed, it still needs to be pinned. Planet has made some updates, and we can use filebase to help us with this. After publishing an article on Planet, you should wait a little longer so that filebase can pin your article on the Internet. Because IPFS has a garbage collection feature, resources that are not accessed frequently will become invalid, so pinning is still very important.

Personal solution#

The idea is as follows: download and install IPFS, set IPFS as a system service so that it starts automatically every time the computer starts. Also, add IPFS pinning to the scheduled tasks to let IPFS automatically handle the pinning operation for us.
So far, I have installed IPFS on my own server and made some modifications to qfdka.eth. Now, several of my servers have IPFS installed, including two domestic servers, two German servers, and even a US server. I hope to automate this process, so I wrote a one-click script:

image

It looks pretty good, but there is still room for modification. Here is a part of it. If it helps, please give it a thumbs up.

#!/bin/bash
FILE_NAME="kubo_v0.16.0_linux-amd64.tar.gz"
DOMAIN="qfdka.eth"
ls /usr/local/bin/ipfs >/dev/null

# Check if IPFS is installed
if [ $? -ne 0 ]; then
    echo -e "💡 Downloading IPFS ${FILE_NAME}"
    cd /tmp
    wget https://dist.ipfs.tech/kubo/v0.16.0/$FILE_NAME
    tar -xvzf $FILE_NAME
    cd kubo

    echo -e "💡 Running official installation script"
    sudo bash install.sh >/dev/null
    echo -e "✅ Official installation script executed"

    # Install system service
    echo -e "📪 Installing system service"
    cat >/lib/systemd/system/ipfs.service <<EOF
[Unit]
Description=IPFS
[Service]
ExecStart=/usr/local/bin/ipfs daemon
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    echo -e "📪 Initializing IPFS"
    ipfs init >/dev/null
    systemctl enable ipfs
    echo -e "✅ IPFS service installation completed"
else
    echo -e "✅ IPFS is already installed"
fi

echo -e "💭 Optimizing IPFS configuration"
systemctl stop ipfs >/dev/null
sysctl -w net.core.rmem_max=2500000
ipfs config Addresses.Gateway /ip4/127.0.0.1/tcp/8088
# Switch to low power mode
# ipfs config profile apply server >/dev/null
ipfs config profile apply lowpower >/dev/null
systemctl start ipfs
echo -e "✅ IPFS optimization completed"

# Pinning operation
echo -e "💡 Performing PIN operation, starting in 5 seconds..."
sleep 5
ipfs pin add /ipns/$DOMAIN
echo -e "✅ PIN operation successful"

echo -e "📪 Setting up scheduled PIN task"
if [ -f /var/spool/cron/crontabs/root ]; then
    CRONTABLE=$(cat /var/spool/cron/crontabs/root | grep $DOMAIN | wc -l)
    if [ $CRONTABLE -eq 1 ]; then
        echo -e "✅ Task already exists"
    else
        echo "*/2 * * * * ipfs pin add /ipns/$DOMAIN" >>/var/spool/cron/crontabs/root
        service cron reload
        echo -e "✅ Scheduled PIN task set successfully"
    fi
else
    echo "*/2 * * * * ipfs pin add /ipns/$DOMAIN" >>/var/spool/cron/crontabs/root
    service cron reload
    echo -e "✅ Scheduled PIN task set successfully"
fi

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.