centos 7 backup cron issue - cron

I have strange issue: wher i run bash script via su user /path/script.sh all work fine, but when same script running via cron, it's use all free memory and make down both servers (from and to) plus ignoring --bwlimit (i see it in control pannel of VDS).
in cron:
* 10 * * * gituser /path/script.sh
my sciprt:
#!/bin/bash
cd /backup
now=$(date +"%Y_%m_%d")
#mysql
mysqldump --all-databases > "$now.sql"
sshpass -p 'pass' rsync --bwlimit=5000 -rz "$now.sql"
sftpuser#ip:/backup/"$now.sql"
#rm -f "$now.sql"
#for test archive
> /home/bitrix/www/testscript
echo "$now" > /path/testscript
#site files
tar cf "$now.tar" /path/. --exclude=/path/./upload --exclude=/path/./wp-content --ignore-failed-read
split --bytes=500MB "$now.tar" "$now.tar.part"
sshpass -p 'pass' rsync --bwlimit=5000 -rz "$now.tar.part"* sftpuser#ip:/backup/
#rm -f "$now.tar"
#upload files
sshpass -p 'pass' rsync --bwlimit=5000 -rz /path/upload/ sftpuser#ip:/backup/upload/
sshpass -p 'pass' rsync --bwlimit=5000 -rz /path/ sftpuser#ip:/backup/wp-content/

toruble was in cron time (* in minutes so its start every new minut)

Related

What causes multiple Mails when using Cron with Bash Script

I've made a little bash script to backup my nextcloud files including my database from my ubuntu 18.04 server. I want the backup to be executed every day. When the job is done I want to reseive one mail if the job was done (additional if it was sucessful or not). With the current script I reseive almost 20 mails and I can't figure out why. Any ideas?
My cronjob looks like this:
* 17 * * * "/root/backup/"backup.sh >/dev/null 2>&1
My bash script
#!/usr/bin/env bash
LOG="/user/backup/backup.log"
exec > >(tee -i ${LOG})
exec 2>&1
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --on
mysqldump --single-transaction -h localhost -u db_user --password='PASSWORD' nextcloud_db > /BACKUP/DB/NextcloudDB_`date +"%Y%m%d"`.sql
cd /BACKUP/DB
ls -t | tail -n +4 | xargs -r rm --
rsync -avx --delete /var/www/nextcloud/ /BACKUP/nextcloud_install/
rsync -avx --delete --exclude 'backup' /var/nextcloud_data/ /BACKUP/nextcloud_data/
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --off
echo "###### Finished backup on $(date) ######"
mail -s "BACKUP" name#domain.com < ${LOG}
Are you sure about the CRON string? For me this means "At every minute past hour 17".
Should be more like 0 17 * * *, right?

why one linux command is working inside conatiner but not in entrpoint.sh

I am using ubuntu 16.04 OS as VM.
While creating container, i have some commands in entrpoint.sh which is not working or behaving as expected but the same command is working when i am manually running inside the container, to be precise below is my simple linux cp command which recursively copy from source to destination and also unzip command.
In my entrypoint.sh I have three commands :
cd /tmp/localization/Tpae7610
unzip \*.zip
cp -r /tmp/localization/Tpae7610/* /home/db2inst1/maximo/
Last two commands are not working when container starts, when I say it's not working it means it is not giving any error but not copying the source contents to destinations as expected also it is not unzipping the .zip files
NOTE: But same command is working as expected when i manually run inside the container.
entrypoint.sh
#!/bin/bash
sysctl -w kernel.shmmni=1024
sysctl -w kernel.shmall=2097152
sysctl -w kernel.msgmnb=65536
sysctl -w kernel.msgmax=65536
sysctl -w kernel.msgmni=4096
sysctl -w kernel.shmmax=4294967296
#set -e
#
# Initialize DB2 instance in a Docker container
#
# # Authors:
# *
#
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
mkdir -p /db2fs
chown db2inst1:db2iadm1 /db2fs
chmod -R 755 /db2fs
#cp /tmp/maxinst.sh /home/db2inst1/maximo/Maximo-7.6-CD/tools/maximo/maxinst.sh
if [ -z "$DB2INST1_PASSWORD" ]; then
echo ""
echo >&2 'error: DB2INST1_PASSWORD not set'
echo >&2 'Did you forget to add -e DB2INST1_PASSWORD=... ?'
exit 1
else
echo "db2inst1:$DB2INST1_PASSWORD" | chpasswd
fi
if [ -z "$LICENSE" ];then
echo ""
echo >&2 'error: LICENSE not set'
echo >&2 "Did you forget to add '-e LICENSE=accept' ?"
exit 1
fi
if [ "${LICENSE}" != "accept" ];then
echo ""
echo >&2 "error: LICENSE not set to 'accept'"
echo >&2 "Please set '-e LICENSE=accept' to accept License before use the DB2 software contained in this image."
exit 1
fi
if [[ $1 = "db2start" ]]; then
echo "Performing botc database start"
if [ ! -d /db2fs/db2inst1 ]; then
echo "Database location does not exist, creating database"
chown -R db2inst1:db2iadm1 /maxdb7605
chown -R db2inst1:db2iadm1 /home/db2inst1/maximo
find /maxdb7605 -type d -exec chmod 755 \{\} \;
find /maxdb7605 -type f -exec chmod 644 \{\} \;
cd /home/db2inst1/maximo
#unzip -o tools.zip && rm tools.zip
#unzip -o applications.zip && rm applications.zip
set -x
cd /home/db2inst1/maximo/tools
if [ ! -f java ]; then
ln -s /home/db2inst1/sqllib/java java
fi
su - db2inst1 <<EOH
db2start
db2 create database maxdb76 on /db2fs dbpath on /db2fs using codeset UTF-8 territory us pagesize 32 K
db2 connect to maxdb76
db2 create bufferpool MAXBUFFPOOL pagesize 32K
db2 grant connect on database to user maximo
db2 GRANT DBADM,SECADM, CREATETAB,BINDADD,CONNECT,CREATE_NOT_FENCED_ROUTINE,IMPLICIT_SCHEMA,LOAD,CREATE_EXTERNAL_ROUTINE,QUIESCE_CONNECT ON DATABASE TO USER maximo
db2 GRANT USAGE on WORKLOAD SYSDEFAULTUSERWORKLOAD TO USER maximo;
db2 create schema maximo authorization maximo
db2 create regular tablespace MAXDATA pagesize 32k managed by automatic storage extentsize 16 overhead 12.67 prefetchsize 16 transferrate 0.18 bufferpool MAXBUFFPOOL dropped table recovery on NO FILE SYSTEM CACHING
db2 grant use of tablespace MAXDATA to user maximo
db2 update db cfg using LOGFILSIZ 5000
db2 update db cfg using LOGPRIMARY 50
db2 update db cfg using LOGSECOND 50
db2 connect reset
db2stop force
db2start
cd /maxdb7605
db2set DB2CODEPAGE=1208
db2 connect to maxdb76
db2 -t -f /maxdb7605/dbschema.sql
db2 -t -f /maxdb7605/dev_grants.sql
db2move maxdb76 LOAD -u maximo -p maximo -l lobs
db2 connect to maxdb76 user maximo using maximo
db2 -x "select 'values nextval for MAXIMO.',sequencename,';' from maxsequence" > /maxdb7605/sequence_update.sql
db2 -t -f /maxdb7605/sequence_update.sql
db2 connect reset
EOH
rm -rf /maxdb7605
set +x
nohup /usr/sbin/sshd -D 2>&1 > /dev/null &
cd /home/db2inst1/maximo/tools/maximo
chmod +x TDToolkit.sh
chmod +x updatedb.sh
dos2unix TDToolkit.sh
dos2unix updatedb.sh
./updatedb.sh
export JAVA_HOME=/opt/ibm/java-x86_64-70
export JRE_HOME=/opt//home/db2inst1/maximoibm/java-x86_64-70/jre
export PATH=${JAVA_HOME}/bin:$PATH
cd /
cd /tmp/localization/Tpae7610
unzip \*.zip
cp -a /tmp/localization/Tpae7610/* /home/db2inst1/maximo/
cd /tmp/localization/Lightning7604
unzip \*.zip
cp -a /tmp/localization/Lightning7604/* /home/db2inst1/maximo/
cd /tmp/localization/BOTC7610
unzip \*.zip
cp -a /tmp/localization/BOTC7610/* /home/db2inst1/maximo/
cd /tmp
#remove localization folder from tmp folder
rm -rf localization
cd /home/db2inst1/maximo/tools/maximo
#./TDToolkit.sh -addlangPT -useexpander
#./TDToolkit.sh -addlangJA -useexpander
#./TDToolkit.sh -addlangDE -useexpander
#./TDToolkit.sh -addlangIT -useexpander
#./TDToolkit.sh -addlangFR -useexpander
#./TDToolkit.sh -addlangES -useexpander
#./TDToolkit.sh -pmpupdatenxtgenui -useexpander
# ./TDToolkit.sh -pmpupdatez_botc -useexpander
chmod -R 777 /home/db2inst1/maximo/tools/maximo/log
#healthcheck looks for this file to indicate the container is initialized
touch /tmp/container_started
while true; do sleep 1000; done
exec "/bin/bash"
#statements
else
su - db2inst1 <<EOH
db2start
db2 catalog db maxdb76 on /db2fs
db2 terminate
db2 connect to maxdb76
EOH
touch /tmp/container_started
while true; do sleep 1000; done
exec "/bin/bash"
fi
sleep 10
fi
Either the files under the directory /tmp/localization/Tpae7610/ are not having permissions.
Try the command cp -v ( verbose will show the file copied)
Comment the rm -rf localization in the script. Then debug the script.

Nested bash command quote issues

I have an app that sends commands to bash like so:
/bin/bash -c "<command goes here>"
This works great but I have hit a problem with a slightly more complicated command. This command grabs a tar from an SSH server, shows a progress bar with pv and then saves it to a local user's directory.
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
Running this command on the command line manually works great but I can't for the life of me work out how to pass this as an argument to /bin/bash/.
I have tried:
/bin/bash -c "su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser"
And various combinations using different syntax but I am just guessing as I don't really understand why it's not working.
I have broken it down to a simpler example and realised it works if inner command uses single quote like this simple example that gets the home path:
bash -c "su -c 'cd ~ && pwd' localuser"
but trying that on larger command causes it to fail:
/bin/bash -c "su -c 'ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar' localuser"
It says no passwd entry for user /home so the command is getting broken up I guess buy the nested single quotes but im not sure how to fix this.
I tried putting double quotes outside the single quotes:
/bin/bash -c "su -c 'ssh -p 1234 remoteuser#123.123.123.123 "'cd /home/ && tar -cf - remoteuser/'" | pv > /home/staging/localuser/staging.tar' localuser"
But then it says it can't find the directory. It looks like I just need to tweak the command a bit but I can't figure it out, can anyone help?
It is a matter of quoting in the right way. There is more than one way to do this. I find double quotes easier to work with for this case:
echo "su -c \"ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar\" localuser"
Prints:
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
Which I think is what you are looking for. That is, escaping with \" any double quotes inside the outer double quotes. So try:
/bin/bash -c "su -c \"ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar\" localuser"
This is where here-documents come in handy:
bash <<'END'
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
END
Note the -c option has been removed: bash will read the commands from stdin.

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Update bash script, file check, how?

#!/bin/sh
LOCAL=/var/local
TMP=/var/tmp
URL=http://um10.eset.com/eset_upd
USER=""
PASSWD=""
WGET="wget --user=$USER --password=$PASSWD -t 15 -T 15 -N -nH -nd -q"
UPDATEFILE="update.ver"
cd $LOCAL
CMD="$WGET $URL/$UPDATEFILE"
eval "$CMD" || exit 1;
if [ -n "`file $UPDATEFILE|grep -i rar`" ]; then
(
cd $TMP
rm -f $TMP/$UPDATEFILE
unrar x $LOCAL/$UPDATEFILE ./
)
UPDATEFILE=$TMP/$UPDATEFILE
URL=`echo $URL|sed -e s:/eset_upd::`
fi
TMPFILE=$TMP/nod32tmpfile
grep file=/ $UPDATEFILE|tr -d \\r > $TMPFILE
FILELIST=`cut -c 6- $TMPFILE`
rm -f $TMPFILE
echo "Downloading updates..."
for FILE in $FILELIST; do
CMD="$WGET \"$URL$FILE\""
eval "$CMD"
done
cp $UPDATEFILE $LOCAL/update.ver
perl -i -pe 's/\/download\/\S+\/(\S+\.nup)/\1/g' $LOCAL/update.ver
echo "Done."
So I have this code to download definitions for my antivirus. The only problem is that, it downloads all files everytime i run script. Is it possible to implement some sort file checking ?, let's say for example,
"if that file is present and have same filesize skip it"
Bash Linux
The -nc argument to wget will not re-fetch files that already exist. It is, however, not compatible with the -N switch. So you'll have to change your WGET line to:
WGET="wget --user=$USER --password=$PASSWD -t 15 -T 15 -nH -nd -q -nc"

Resources