How to export Variable when using runuser - r

I am trying to run Rscript as a different user under constrained environment with a user which do not have the access to /tmp.
As the Rscript create a tmp file, it needs access to /tmp. So i thought if i can tell Rscript to change the tmp directory and user the one i specify.
In the documention it is mentioned that Rscript looks at the $TMPDIR environment variable to set its tmp directory
Below are all the failed trails to pass the environment variable.
ATTEMPT-1: runuser -l MYUSER -c "export TMPDIR=/SOME_DIR && echo $TMPDIR"
ATTEMPT-2: runuser -l MYUSER TMPDIR=/SOME_DIR -c "echo $TMPDIR"
ATTEMPT-3: runuser -l MYUSER TMPDIR=/SOME_DIR -c "echo $TMPDIR"
ATTEMPT-3: runuser -l MYUSER -c "Rscript --TMPDIR=/SOME_DIR test.r " --> Random stuff
All attempt failed.
I cannot move away from runuser .
Any help will be appreciated

I have not tried your examples but none of them could work because $TMPDIR is already replaced by the invoking shell. So, your
runuser -l MYUSER -c "export TMPDIR=/SOME_DIR && echo $TMPDIR"
becomes
runuser -l MYUSER -c "export TMPDIR=/SOME_DIR && echo "
if TMPDIR is empty or unset. Try with single quotes to avoid this:
runuser -l MYUSER -c 'export TMPDIR=/SOME_DIR && echo $TMPDIR'
should result in the output /SOME_DIR.

Related

Bash restore database to a docker container for a Wordpress + Piwik Solution

For the https://github.com/ellakcy/piwik-with-wordpress I am making a restore bash script in order to restore the backup generated from the https://github.com/ellakcy/piwik-with-wordpress/blob/master/scripts/pre-backup script
The main idea is to set a path with a tarball containing the backup and recreating the folders that volumes are mounted.
The script is the following:
#!/bin/bash
# Printing functions
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'
#Echo a string with color
cecho () # Color-echo.
# Argument $1 = message
# Argument $2 = color
{
local default_msg="No message passed."
# Doesn't really need to be a local variable.
message=${1:-$default_msg} # Defaults to default message.
color=${2:-$black} # Defaults to black, if not specified.
echo -e "$color"
echo "$message"
tput sgr0 # Reset to normal.
return
}
#Echo a string as error with color
cecho_err () # Color-echo.
# Argument $1 = message
# Argument $2 = color
{
local default_msg="No message passed."
# Doesn't really need to be a local variable.
message=${1:-$default_msg} # Defaults to default message.
color=${2:-$red} # Defaults to black, if not specified.
echo >&2 -e "$color"
echo >&2 "$message"
tput sgr0 # Reset to normal.
return
}
backup_file=${1}
cecho "Creating the correct folders" $cyan
cecho "Deleting data folder in order to recreate it" $red
sudo rm -rf ./data
mkdir ./data/
sudo chown root:root ./data/
sudo chmod 755 ./data/
if [ ! -f restore ]; then
mkdir ./restore/
fi
tar -xf ${backup_file} -C ./restore/
cecho "Restoring backup data for wordpress" $cyan
sudo mkdir ./data/wordpress
sudo chown root:root ./data/wordpress
sudo chmod 755 ./data/wordpress
sudo mv ./restore/wordpress/data/www ./data/wordpress/
sudo chown www-data:www-data ./data/wordpress/www
cecho "Restoring environment" $cyan
wordpress_env=$(tr '\n' ' ' <./restore/wordpress/env.txt)
echo ${wordpress_env}
cecho "Restoring database" $cyan
sudo mkdir ./data/wordpress/db
echo "sudo env ${wordpress_env} docker run --volume \"./data/wordpress/db\":/var/lib/mysql --volume ./restore/wordpress/db:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=\$WORDPRESS_MYSQL_ROOT_PASSWORD -e MYSQL_DATABASE=\"wordpress\" -e MYSQL_USER=\$WORDPRESS_MYSQL_USER -e MYSQL_PASSWORD=\$WORDPRESS_MYSQL_PASSWORD mariadb" > ./restore_db.sh
chmod +x ./restore_db.sh
./restore_db.sh
# rm -rf ./restore_db.sh
rm -rf ./restore
And I get this error when I try to restore the database:
docker: Error response from daemon: create ./data/wordpress/db: "./data/wordpress/db" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
See 'docker run --help'.
As you can see it generates a temporary scripts (that later will be deleted) one example of generated script is:
sudo env WORDPRESS_MYSQL_ROOT_PASSWORD=passwd WORDPRESS_MYSQL_USER=wordpress WORDPRESS_MYSQL_PASSWORD=wordpress WORDPRESS_ADMIN_USER=admin WORDPRESS_ADMIN_PASSWORD=admin WORDPRESS_URL=http://0.0.0.0:8080 docker run --volume "./data/wordpress/db":/var/lib/mysql --volume ./restore/wordpress/db:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=$WORDPRESS_MYSQL_ROOT_PASSWORD -e MYSQL_DATABASE="wordpress" -e MYSQL_USER=$WORDPRESS_MYSQL_USER -e MYSQL_PASSWORD=$WORDPRESS_MYSQL_PASSWORD mariadb
What is the best option in order to generate the correct volume data in ./data/wordpress/db that mounts on a container's /var/lib/mysql?
When we specify --volume <host_dir>:<container_dir>, host_dir must be an absolute path. If it is not an absolute path, then it considered to be the volume's name. Hence the message invalid characters for a local volume name. Try providing absolute path for the host directory.

Explain plan statement in DB2 not working using shell script

I have used the below statement in my script to get the explain plan for a stored procedure but its throwing error
a=`db2 -x "SELECT current timestamp FROM sysibm.sysdummy1"`
c=`echo $a|cut -d"." -f1-2`
time_new="'$c%'"
db2exfmt -d ${DATABASE} -e ${DATABASE} -g -l -n -s ${SCHEMA_NAME} -o SP_explain_plan_${TIMESTAMP}.out -w ${time_new} -# 0 -v %
Its not able to read the timestamp correctly. Can someone help

How do I set multiple commands to an alias for Vim

I'm trying to create an alias to run tests in my Vim. But I get this error
Not an editor command vagrant ssh -c 'cd /srv/www/wp-content/themes/project/ && phpunit'
Here's my .vimrc
command! Runtest execute "cd ~/ToMyProject | vagrant ssh -c 'cd /srv/www/wp-content/themes/project/ && phpunit'"
:execute is for evaluation of Vimscript; it appears you want to run external commands. To do so while observing the commands' output, use :!:
command! Runtest !cd ~/ToMyProject && vagrant ssh -c 'cd /srv/www/wp-content/themes/project/ && phpunit'
Alternatively, you could :call system('cd ~/ToMyProject | ...'); this would still block, but capture and return the output and exit status (which you could discard or inspect in Vimscript).

Get pid of application started by mvn exec plugin

I want to write .sh script that will kill process started by maven exec plugin. At the moment i'm trying to get PID by start time, but this approach do not guarantee that there is no some collision with other processes.
Please, could someone say whether there is an approach to get PID or to kill process started by mvn exec .
Thanks.
The best approach is to start the maven process using start-stop-daemon. I use a modified bash script from a tomcat init.d to start/stop java or maven programs as daemons. Here is a snippet. Please customize as needed:
#!/bin/sh
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start -b -u "$MAVEN_USER" -g "$MAVEN_GROUP" \
-c "$MAVEN_USER" -d "$MAVEN_HOME" -p "$MAVEN_PID" -m \
-x mvn -- exec:java -Dexec.mainClass="com.company.Application" -Dexec.classpathScope=runtime -Dexec.args="/home/user 192.168.1.1" 2>&1
status="$?"
set +a -e
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
set +e
if [ -f "$MAVEN_PID" ]; then
start-stop-daemon --stop --pidfile "$MAVEN_PID" \
--user "$MAVEN_USER" \
--retry=TERM/20/KILL/5 >/dev/null
if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $MAVEN_PID`"
log_failure_msg "Failed to stop $NAME (pid $PID)"
exit 1
fi
rm -f "$MAVEN_PID"
rm -rf "$JVM_TMP"
else
log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;

Quick RSYNC code correction

What's wrong with this code?
sudo -u replicant rsync -av -e "ssh -o 'StrictHostKeyChecking no' -i /home/replicant/.ssh/id_rsa" --exclude 'media/' --exclude 'var/' --exclude '.svn' root#$ADMIN:/var/www/ /var/www/ &> /tmp/rsync if [ $? -ne 0 ]; then
echo "date: Error rsync'ing code base from $ADMIN check /tmp/rsync" | mail -s "Rsync error!" $DEVEMAIL
echo "date: Error rsync'ing code base from $ADMIN check /tmp/rsync" >> $LOGFILE
echo "root#$ADMIN:/var/www /var/www" >> $LOGFILE
exit
fi
I keep getting this error:
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(605)
[Receiver=3.0.9]
Please help. Thanks.
Try to login directly on SSH to fix your issues, then move on to your rsync test. So start with:
ssh -o 'StrictHostKeyChecking no' -i /home/replicant/.ssh/id_rsa root#$ADMIN
Sidenotes:
don't use root for such a task
add set -eu at the start of your Bash script, so that errors will end up your script and ease debugging (for example if $ADMIN is not defined, the script will end in error)

Resources