(https://prnt.sc/106y7u5)
Does anyone know how to solve it, it seems that there is something in entrypoint.sh that is deleting the ""
Here my entrypoint.sh:
#!/bin/bash
cd /home/container
#Umod
export PATH="$PATH:$HOME/.dotnet/tools"
echo "PATH=\$PATH:\$HOME/.dotnet/tools; export PATH" >> ~/.profile
# fixes Couldn't find a valid ICU package installed on the system
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
echo "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1; export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT" >> ~/.profile
# Update Valheim Server
umod update core apps extensions --patch-available --strict --validate --prerelease
#Run Server
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
# Replace Startup Variables
MODIFIED_STARTUP=`eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g'))`
echo ":/home/container$ ${MODIFIED_STARTUP}"
# Run the Server
${MODIFIED_STARTUP}
And here my startup: [Env Var: STARTUP]
./valheim_server.x86_64 -nographics -batchmode -name "uMod CastBlack" -port 2481 -world "Dedicated" -password "umodgods" -public 1 valheim > >(sed -uE "[hidden]") & trap "[hidden]" 15; wait $!
Does anyone know how to solve it? I belive, the error is here:
# Replace Startup Variables
MODIFIED_STARTUP=`eval echo $(echo ${STARTUP} Y)`
echo ":/home/container$ ${MODIFIED_STARTUP}"
# Run the Server
${MODIFIED_STARTUP}
Related
I have some easy backup script to tar, xz and save to SMB share some folders and want to use variable ($invocation) for a part of the command.
Unfortunately, when script runs it remembers that the variable $location is empty and a result of it is no backup
(xz: /mnt/backupMatrix//2020-06-05T00-27.tar.xz: No such file or directory).
I do know why it's not working, but how to do it correctly?
How to "reload" variable $invocation (with freshly set $location) just before it's launch, WITHOUT hardcoding whole invocation to the function?
Code below.
#! /bin/bash
invocation="xz --threads=8 -z -9 -v -v - > /mnt/backupMatrix/$location/$(date +%Y-%m-%dT%H-%M).tar.xz"
function mcuser2
{
#backup mcuser2
location="mcuser2"
echo "setting var location to: $location"
echo "Stopping minecraft2.service"
sudo systemctl stop minecraft2.service
echo "Stopped minecraft2.service"
echo "Starting taring mcuser2"
# --exclude='/home/mcuser2/world'
tar cpf - --exclude='/home/mcuser2/plugins/dynmap/web/tiles' /home/mcuser2/ | $invocation
echo "Completed taring mcuser2"
echo "starting minecraft2.service"
sudo systemctl start minecraft2.service
echo "started successfully minecraft2.service"
}
case "$1" in
mcuser2)
mcuser2
;;
esac
Since you set location unconditionally in the function, the simplest approach is to move that line from the function and place it before you set invocation.
An alternative is to use more variables:
: "${XZ:=xz}"
: "${XZOPTIONS:=--threads=8 -z -9 -v -v -}"
: "${TSTAMP:=$(date +%Y-%m-%dT%H-%M)}"
: "${BASEDIR:=/mnt/backupMatrix}"
: "${TAR:=tar}"
: "${TAROPTIONS:=-cpf -}"
STARTDIR="/home/mcuser2" # $HOME?
EXCLUDE="--exclude=$STARTDIR/plugins/dynmap/web/tiles"
function mcuser2
{
#backup mcuser2
location="mcuser2"
echo "setting var location to: $location"
echo "Stopping minecraft2.service"
sudo systemctl stop minecraft2.service
echo "Stopped minecraft2.service"
echo "Starting tarring mcuser2"
$TAR $TAROPTIONS - "$EXCLUDE" "$STARTDIR" |
$XZ $XZOPTIONS > "$BASEDIR/$location/$TSTAMP.tar.xz"
echo "Completed tarring $STARTDIR"
echo "starting minecraft2.service"
sudo systemctl start minecraft2.service &&
echo "started successfully minecraft2.service"
}
Warning: untested shell scripting.
I'd probably not include the full path in the archive (GNU tar drops the leading slash anyway); I'd cd to the start directory and specify . as the directory to be backed up — possibly use -C "$STARTDIR" on the tar command line. That would require me to alter the exclude option too, I expect, using a relative path instead of an absolute path.
You could also think of using shell arrays for the options lists.
I'm not convinced I'd use a function in this script; it doesn't really seem to help very much.
I have made a script to run a backup from a local machine to a remote server. However when running this script I get the error:
/etc/run_backup: 173: /etc/run_backup: Syntax error: Unterminated quoted string
For the life of me I cannot see why I am getting this error. Below is the script (with some redactions):
#!/bin/sh
#
# this script must be run as root on machine with folders
# to be backed up.
#
# This script will attempt to back up several folders
# to a remote server which must have passwordless ssh
# access set up for the root users on the machines. If
# this is inaccessible a backup will be attempted in some
# local storage, e.g. a usb drive. Locations are defined
# below
set -ex
##### CONFIG VARIABLES #####
# backup server location
backup_server=<redacted>
# name of top level folder used to contain all backups on either
# the remote server or local backup location
backup_folder=backups
# the root folder where $backup_folder will be placed on the
# remote machine
remote_backup_storage_root=/mnt/reos-storage-2
# the root folder where $backup_folder will be placed on
# local storage if the remote machine is not available
local_backup_storage_root=/media/usbflash0
# the directory wher the results of the backup attempt by rdiff-backup
# will be placed for each folder backup, and also the where the backup
# log file generated by this script will be placed
error_report_dir=/home/pi/rdiff-backup-errors
# email to send any messages to. An email will be sent on success
# and under certain error conditions
email_receiver=<redacted>
# the location of the nextcloud data folder to backup
nc_data_dir=/opt/nextcloud-data
# root directory of the nextcloud site install to backup
nc_web_dir=/var/www/nextcloud
##### END CONFIG VARIABLES #####
# lock file will be created and used to ensure we don't try to run two
# backup processes at the same time
lockfilename=/var/run/remotebackup.pid
if test -e ${lockfilename}; then
echo "Backup is already running!"
echo "If it isn't, remove ${lockfilename} and try again."
echo "/etc/run_backup: unable to run backup as lock file ${lockfilename} is present" | mail -s "Backup Problem" ${email_receiver}
exit 3
else
echo $$ > ${lockfilename};
fi
remote_server_backup_folder=${remote_backup_storage_root}/${backup_folder}
logfile=${error_report_dir}/rdiff-backup.log
echo "[$(date "+%m%d%Y %T")] Starting backup" >> ${logfile}
# where the database will be backed up to. We don't store the date as we will
# then back up the whole nextcloud data directory using rdiff-backup onto the
# backup server which will allow us to step back in time if required
db_backup_file=${nc_data_dir}/nextcloud-db.bak
cd ${nc_web_dir}
echo "[$(date "+%m%d%Y %T")] Putting nextcloud in maintenance mode" >> ${logfile}
# turn on nextcloud maintenance mode
sudo -u www-data php occ maintenance:mode --on
echo "[$(date "+%m%d%Y %T")] Making nextcloud database dump on the server" >> ${logfile}
mysqldump -u root --single-transaction nextcloud > "${db_backup_file}"
echo "[$(date "+%m%d%Y %T")] Completed nextcloud database dump" >> ${logfile}
#nc -z ${backup_server} 22 > /dev/null
ssh -q root#${backup_server} exit
backup_machine_accessible=$?
echo "in here 0"
if [ "$backup_machine_accessible" -eq "0" ] ;
then
echo "in here 1"
backup_location="root#${backup_server}::${remote_server_backup_folder}"
echo "[$(date "+%m%d%Y %T")] Backing up to ${backup_location}" >> ${logfile}
else
# use local storage (flash drive)
echo "in here 2"
# check the USB drive is actually accesible, then make sure the
# backup directory on the drive actually exists
if test -e ${local_backup_storage_root}; then
echo "in here 3"
backup_location="${local_backup_storage_root}/${backup_folder}"
mkdir -p ${backup_location}
echo "[$(date "+%m%d%Y %T")] ${backup_server} was not accessible, backing up to ${backup_location}" >> ${logfile}
else
echo "in here 4"
echo [$(date "+%m%d%Y %T")] ${backup_server} was not accessible, and ${backup_location} was not available" >> ${logfile}
echo "${backup_server} was not accessible, and ${backup_location} was not also accessible" | mail -s "Backup Problem" ${email_receiver}
if test -e ${lockfilename}; then
rm ${lockfilename}
fi
exit 4
fi
fi
echo "out here"
# make the directory for storing errors reported during the backup process
mkdir -p ${error_report_dir}
# backup nextcloud instance
echo "[$(date "+%m%d%Y %T")] Starting nextcloud web backup" >> ${logfile}
rdiff-backup ${nc_web_dir} ${backup_location}/nextcloud/ 2> ${error_report_dir}/rdiff-backup-nextcloud-web_$(hostname).txt
# backup nextcloud user data
echo "[$(date "+%m%d%Y %T")] Starting nextcloud data backup" >> ${logfile}
rdiff-backup \
--ssh-no-compression \
--exclude "${nc_data_dir}/data/.opcache" \
--exclude "${nc_data_dir}/data/access.log" \
--exclude "${nc_data_dir}/data/error.log" \
--exclude "${nc_data_dir}/data/nextcloud.log" \
--exclude "${nc_data_dir}/data/access.log" \
${nc_data_dir} ${backup_location}/nextcloud-data/ 2> ${error_report_dir}/nextcloud-data-and-db_$(hostname).txt
# turn off nextcloud maintenance mode
cd ${nc_web_dir};
echo "[$(date "+%m%d%Y %T")] Turning off nextcloud maintnenance mode" >> ${logfile}
sudo -u www-data php occ maintenance:mode --off
# backup /etc which includes letsencrypt, fstab
echo "[$(date "+%m%d%Y %T")] /etc backup" >> ${logfile}
rdiff-backup --ssh-no-compression /etc/ ${backup_location}/etc_$(hostname)/ 2> ${error_report_dir}/rdiff-backup-etc_$(hostname).txt
# backup root
echo "[$(date "+%m%d%Y %T")] /root backup" >> ${logfile}
rdiff-backup --ssh-no-compression /root/ ${backup_location}/root_$(hostname)/ 2> ${error_report_dir}/rdiff-backup-root_$(hostname).txt
# backup home directories
echo "[$(date "+%m%d%Y %T")] /home backup" >> ${logfile}
rdiff-backup --ssh-no-compression /home/ ${backup_location}/home_$(hostname) 2> ${error_report_dir}/rdiff-backup-home_$(hostname).txt
echo "/etc/run_backup: backup of $(hostname) appears to have run successfully, or at least reached the end of the script without errors" | mail -s "Backup Report" ${email_receiver}
# Remove lock file and end script
#
if test -e ${lockfilename}; then
rm ${lockfilename}
else
echo "[$(date "+%m%d%Y %T")] Could not remove lock file ${lockfilename}!" >> ${logfile}
echo "/etc/run_backup was unable to remove lock file ${lockfilename} at end of the script as it did not exist" | mail -s "Backup Problem" ${email_receiver}
fi
And the output I get when running it is the following:
# /etc/run_backup
+ backup_server=<redacted>
+ backup_folder=backups
+ remote_backup_storage_root=/mnt/reos-storage-2
+ local_backup_storage_root=/media/usbflash0
+ error_report_dir=/home/pi/rdiff-backup-errors
+ email_receiver=<redacted>
+ nc_data_dir=/opt/nextcloud-data
+ nc_web_dir=/var/www/nextcloud
+ lockfilename=/var/run/remotebackup.pid
+ test -e /var/run/remotebackup.pid
+ echo 13057
+ remote_server_backup_folder=/mnt/reos-storage-2/backups
+ logfile=/home/pi/rdiff-backup-errors/rdiff-backup.log
+ date +%m%d%Y %T
+ echo [09162019 11:30:06] Starting backup
+ db_backup_file=/opt/nextcloud-data/nextcloud-db.bak
+ cd /var/www/nextcloud
+ date +%m%d%Y %T
+ echo [09162019 11:30:06] Putting nextcloud in maintenance mode
+ sudo -u www-data php occ maintenance:mode --on
Maintenance mode already enabled
+ date +%m%d%Y %T
+ echo [09162019 11:30:07] Making nextcloud database dump on the server
+ mysqldump -u root --single-transaction nextcloud
+ date +%m%d%Y %T
+ echo [09162019 11:30:12] Completed nextcloud database dump
+ ssh -q root#ed-mh-pi01.reoptimizesystems.com exit
+ backup_machine_accessible=0
+ echo in here 0
in here 0
/etc/run_backup: 173: /etc/run_backup: Syntax error: Unterminated quoted string
can anyone explain the error? It seems to me like it is something to do with the if statement if [ "$backup_machine_accessible" -eq "0" ] ;, but I can't see any problem with this.
I have macOS 10.11.6. The JAVA_HOME is set in the .bash_profile as
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
From command line, I can see it
>echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home
But, the JAVA_HOME is empty, when I run the following script
#!/bin/sh
SHELL=/bin/sh
export SHELL
echo $JAVA_HOME
if [ -n "$JAVA_HOME" ]; then
#do something
else
echo JAVA_HOME is empty
fi
Even if I use #!/bin/bash in my script, the JAVA_HOME is still empty.
TL;DR
Depending on how you run your scripts, you may need to put your exports into ~/.bashrc for interative shells, or directly into your script or crontab so that it doesn't rely on exports from your user's shell.
Bash Startup Files
Simplistically, the ~/.bash_profile script is sourced for login shells (e.g. /bin/bash -l), while the ~/.bashrc file is sourced for interactive shell (e.g. /bin/bash -i). It's actually a bit more complicated than that, but the above should get you pointed in the right direction.
See Also
https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html#Invoking-Bash
https://www.freebsd.org/cgi/man.cgi?query=bash#INVOCATION
man 1 bash
I configure mine like this:
# configure Java http://www.jenv.be/
# install Java: brew cask install java, brew cask install java7
# set global default: setJavaGlobal 1.7, jenv global 1.7
# set local folder default: jenv local 1.8
#
JENV_HOME=$HOME/.jenv
export PATH=.:$PATH:$JENV_HOME/bin
eval "$(jenv init -)"
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"
alias jenv_set_java_home='export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"'
And some related shell methods I use in my .bash_profile :
setJavaGlobal() {
jenv global $1;
jenv_set_java_home
echoJavaSetup
}
setJavaLocal() {
jenv local $1;
jenv_set_java_home
echoJavaSetup
}
echoJavaSetup() {
echo --------------------
echo NEW JAVA SETUP:
echo " PATH: $PATH"
export JAVA_VERSION=`java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'`
echo " JAVA: $JAVA_VERSION, $JAVA_HOME"
jenv versions
echo --------------------
}
removeJavaLocal() {
rm -rf ./.java-version
}
showJava() {
echo --------------------
echo EXISTING JAVA SETUP:
echo " PATH: $PATH"
export JAVA_VERSION=`java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'`
echo " JAVA: $JAVA_VERSION, $JAVA_HOME"
jenv versions
if [ -f ./.java-version ]; then
echo "Using Java LOCAL DEFAULT. Not using global default! Run command 'removeJavaLocal' to change to global default."
fi
echo --------------------
}
I'm trying to install a cronjob to run a bash shell script on a relative's machine. They will run the install and I can't access it remotely yet (that's what my script is for - but that's not the issue here). I use kdialog to request their root password and then want to use that to sudo various commands. My code below is failing by a) revealing the root p/w on the terminal and b) failing to pipe it to the various sudos. Help?
#!/bin/bash
kdialog --password "Please enter your root password to install theCronScript.sh and set up cron"
# Sanity checks =========================================╕
if test -z "$BASH" ; then
printf "$SCRIPT:$LINENO: please run this script with the BASH shell\n">&2
exit 192
fi
#========================================================╛
# Global variables=======================================╕
PW="$?"
THISDIR="$(pwd)"
GETIPFILE='theCronScript.sh'
CRONPERIOD='/15 * * * * '
TARGETCRONDIR='/etc/cron.hourly'
#========================================================╛
echo "hi"
# txt file exists check =================================╕
echo "Checking:"
if [ ! -f "$THISDIR/$GETIPFILE" ]; then #there's no file to install
kdialog --msgbox "I cannot find $GETIPFILE to upload\nPlease check attachments in recent e-mails from Greg and download $GETIPFILE to $THISDIR"
exit
else
if [ -f "$TARGETCRONDIR/$GETIPFILE" ]; then #the target already exists
kdialog --title "Replace or Keep" --warningyesno "A similar file already exists.\n Do you want to replace it (recommended)?\n(The original file will be saved with a different name _OLD)"
if [ $? = 0 ]; then # rename, then replace the existing file
#echo $PW is probably unneccessary beyond the first use but just in case...
RNGETIPFILE=$GETIPFILE'_OLD'
echo $PW | sudo -S mv $TARGETCRONDIR/$GETIPFILE $TARGETCRONDIR/$RNGETIPFILE #rename original file
echo $PW | sudo -S cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE #
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
else # Don't replace, exit
exit
fi
else # Nothing to replace. Just copy it in
echo $PW | sudo -S "cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE" #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE # make sure it's executable
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
fi
fi
exit 0
#========================================================╛
One option is to ask for the password directly in the sudo command via an external GUI. From the sudo manpage:
-A, --askpass
Normally, if sudo requires a password, it will read it from the user's terminal. If the -A (askpass) option is specified, a (possibly graphical)
helper program is executed to read the user's password and output the password to the standard output. If the SUDO_ASKPASS environment variable is
set, it specifies the path to the helper program. Otherwise, if sudo.conf(5) contains a line specifying the askpass program, that value will be
used. For example:
# Path to askpass helper program
Path askpass /usr/X11R6/bin/ssh-askpass
If no askpass program is available, sudo will exit with an error.
if sudo is caching credentials, it will only ask for this password once. One way I use to get this cached would be, having no side effects other than caching the password:
export SUDO_ASKPASS=/usr/bin/ssh-askpass
sudo --askpass true
Depending on what distro you're running, ssh-askpass may be somewhere else. There is an example on StackExchange on how you might use kdialog to get the password for the sudo askpass. For reference, here is the script:
$ cat myaskpass.sh
#!/bin/bash
kdialog --password "Please enter your password: "
exit 0
And how you would use it:
export SUDO_ASKPASS=/path/to/myaskpass.sh
sudo --askpass true
Ive recently been introduced to bash scripting... So, Ive used my advanced theft course to throw together the attached script. it runs... and exits with "/xxx/ not mounted. You are not root! I have rdiff-backup and sshfs installed and working. The commands work fine on their own on the commandline, but in the script, well... Can you guys take a look and lemme know? PS I copied a LOT of this from scripts I found here and a few other places.
<code>
#!/bin/bash
# Version 1.5
# Prior to running this make sure you have ssh-keygen -t rsa to generate a key, then
# ssh username#target "mkdir .ssh/;chmod 700 .ssh"
# scp .ssh/id_rsa.pub username#target:.ssh/authorized_keys
#
# then check you can login and accept the ssh key
# ssh username#target "ls -la"
#
# Key things to remember, no spaces in pathnames, and try to use full paths (beginning with / )
#
# variables determine backup criteria
DATESTAMP=`date +%d%m%y`
USERNAME=username #remote site username here
TARGET=remote.ip.here #add the ip v4 address of the target
INCLUDES=/path/to/file/includes.txt #this is a txt file containing a list of directories you want backed up
EXCLUDES="**" #this is a list of files etc you want to skip
BACKUPLOG=/path/to/logfile/in/home/backuplog${DATESTAMP}.txt
OLDERTHAN=20W #change 20 to reflect how far back you want backups to exist
# to activate old backup expiry, uncomment the line below
#RMARGS=" --force --remove-older-than ${OLDERTHAN}"
TARGETMAIL="yourmailaddress#your.domain"
HOSTNAME=`hostname` #Dont change this!
TMPDIR=/backups Change this to the source folder
TARGETFOLDER=/backups change this to the TARGET folder
ARGS=" -v0 --terminal-verbosity 0 --exclude-special-files --exclude-other-filesystems --no-compression -v6"
# detecting distro and setting the correct path
if [ -e /etc/debian_version ];then
NICE=/usr/bin/nice
elif [ -e /etc/redhat-release ];then
NICE=/bin/nice
fi
if [ -e /tmp/backup.lock ];then
exit 0
fi
touch /tmp/backup.lock
touch -a ${BACKUPLOG}
cd /
/bin/mkdir -p ${TMPDIR}
/usr/bin/sshfs -o idmap=user -o ${USERNAME}#${TARGET}:/${TARGETFOLDER} ${TMPDIR} &>${BACKUPLOG}
# if you get errors mounting this then try
# mknod /dev/fuse -m 0666 c 10 229
for ITEMI in ${INCLUDES} ; do
ARGS="${ARGS} --include ${ITEMI} "
done
for ITEME in ${EXCLUDES} ; do
ARGS="${ARGS} --exclude-regexp '${ITEME}' "
done
# the --exclude ** / is a hack because it wont by default do multiple dirs, so use --include for all dirs then exclude everything else and sync / - if you dont understand dont worry
# ref: http://www.mail-archive.com/rdiff-backup-users#nongnu.org/msg00311.html
#echo /usr/bin/rdiff-backup ${ARGS} --exclude \'**\' / ${TMPDIR}/ &&
cat ${INCLUDES} | while read DIR; do
${NICE} -19 /usr/bin/rdiff-backup --exclude '**' ${DIR} ${TMPDIR}/ &>${BACKUPLOG}
if [ $? != 0 ]; then
echo "System Backup Failed" | mutt -s "Backup Log: System Backup Failed, Log attached!" -a ${BACKUPLOG} ${TARGETMAIL}
exit 1;
fi
done
#${NICE} -19 /usr/bin/rdiff-backup ${ARGS} --exclude '**' / ${TMPDIR}/ &>${BACKUPLOG} &&
echo Removing backups older than ${RMARGS}
${NICE} -19 /usr/bin/rdiff-backup -v0 --terminal-verbosity 0 ${RMARGS} ${TMPDIR}/ &>${BACKUPLOG}
/bin/umount ${TMPDIR} && /bin/rm -rf ${TMPDIR}/ &>${BACKUPLOG}
echo "System Backup Run" | mutt -s "Backup Log: System Backup Done!" -a ${BACKUPLOG} ${TARGETMAIL}
rm /tmp/backup.lock
rm ${BACKUPLOG}
Sorry, cannot paste, couldnt attach... BLIKSEM!
Thanks for ANY input... One HELL of a learning curve!!!
Regards,
B.