I have a bash script that needs to run as root to accomplish a task in this case it is to make the validator take a snapshot of the Helium blockchain.
I have edited the /etc/sudoers file to allow me to run this script as root. This works fine.
useraccount ALL=(ALL:ALL) NOPASSWD:/home/useraccount/validator_data/snapshotmaker.sh
The command in the script is as follows:
sudo docker exec validator miner snapshot take /var/data/$dtt
after this, the command generates a file like this '30-10-2021T233752.bin'.
My next step is to take this file and copy it to IPFS so I can share it. The command to do this is:
ipfs files cp /ipfs/$(ipfs add -Q $localfile) $ip
At the moment I get the following error:
Error: no IPFS repo found in /root/.ipfs. please run: 'ipfs init'
This is because it is trying to run it as root when the configuration is in my useraccount.
I have tried to switch accounts in the middle of the script but it seems to kill it.
So my question is how to run the IPFS command as my original user from within the root script.
My Bash Scrip:
#!/bin/bash
dt=$(date '+%d-%m-%YT%H%M%S');
dtt="${dt}.bin"
a='/var/data/'
c="${a}${dt}.bin"
echo "${c}"
sudo docker exec validator miner snapshot take /var/data/$dtt
localfile="/home/useraccount/validator_data/${dt}.bin"
echo "LocalFile: ${localfile}"
ip=" /Helium/Snapshots/2021/${dt}.bin"
echo "IPFS Location: ${ip}"
sleep 2
if [ -f "$localfile" ]; then
echo "$localfile exists."
sudo chown useraccount $localfile
whoami
su - useraccount
whoami
ipfs files cp /ipfs/$(ipfs add -Q $localfile) $ip
#ipfs files cp /ipfs/$(ipfs add -Q <local-file>) "/Helium/Snapshots/2021/<dest-name>"
else
echo "$localfile does not exist."
fi
The output is as follows:
/var/data/31-10-2021T005728.bin
ok
LocalFile: /home/useraccount/validator_data/31-10-2021T005728.bin
IPFS Location: /Helium/Snapshots/2021/31-10-2021T005728.bin
/home/useraccount/validator_data/31-10-2021T005728.bin exists.
root
And then it dies.
If I remove the su
line then I get the root issue as mentioned at the top of this post.
Hoping someone can help.