shell bash script - upload a big tar file, retry? - linux

I use the following shell script code to upload a big .tar file. Sometimes it happens that the server can´t resolve the domain to the ip or the other server isn´t available. So I wan´t it to retry some times if it didn´t work. How can I do this? I couldn´t find something for this on the internet.
ftp -inv << EOF
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
quit
EOF
Edit:
Sorry I have no real experience with shell, how would this look like ?
FTP_SUCCESS_MSG="226 Transfer complete"
while [fgrep "$FTP_SUCCESS_MSG" $FTPLOG]
do
FTPLOG=/temp/ftplogfile
ftp -inv <<! > $FTPLOG
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
close
quit
!
fi
exit 0
sleep 10s
else
echo "Upload completed"
done

Adapting from your code and the one in Getting exit status code from 'ftp' command in linux shell I made this:
#!/bin/bash
FTP_SUCCESS_MSG="226 Transfer complete"
FTPLOG=/temp/ftplogfile
i=0
while [ $i -le 5 ]; do
ftp -inv <<! > $FTPLOG
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
close
quit
!
if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then
i=10 #stupid way of saying: exit the "while"
else
sleep 5
i=expr $i + 1 # ((i++)) not working
fi
done

Related

How to ignore rsync warning about vanished files?

I get a lot of mails from cronjobs with rsync. And I've tried to ignore it with wrapper script like this:
#!/bin/bash
/usr/bin/rsync "$#"
e=$?
if test $e = 24; then
exit 0
fi
exit $e
And saved it like a /usr/bin/rsync-no24
After that, I changed my script for cronjob:
#!/bin/bash
SOURCE_BASE="/var/www/"
TARGETS="server30"
TARGET_DIR="/var/www/"
RSYNC_BIN="/usr/bin/rsync-no24"
RSYNC_OPTIONS="-aqqq"
/usr/bin/find ${SOURCE_BASE}/typo3temp ! -user www-data -exec chown -R www-data:www-data {} \;
#for SOURCE_DIR in fileadmin uploads typo3temp
#do
for TARGET_HOST in ${TARGETS}
do
${RSYNC_BIN} ${RSYNC_OPTIONS} ${SOURCE_BASE}/${SOURCE_DIR} ${TARGET_HOST}:${TARGET_DIR}/
done
#done
But anyway I still get mails from cron such as
file has vanished:
"/var/www/stage2/typo3temp/tx_ncstaticfilecache/OnlineBackup/index33.html.5"
How to ignore messages like this? Probably something wrong with wrapper script?
Thanks a lot.
Replace your /usr/bin/rsync-no24 with this:
#!/bin/bash
(rsync "$#"; if [ $? == 24 ]; then exit 0; else exit $?; fi) 2>&1 | grep -v 'vanished'
source
(on a side note, I don't think there's a difference between RSYNC_OPTIONS="-aqqq" and RSYNC_OPTIONS="-aq"

Transfer files using lftp in bash script

I have server A test-lx, and server B test2-lx, I want to transfer files from server A to server B.
While transfering the files i'll need to create a driectory only if it's not exist, how can i check if a directory exist during the lftp conenction? How can i out several files in one command instead of doing this in 2 lines.
Is there an option to use find -maxdepth 1 -name DirName
Here is my code:
lftp -u drop-up,1Q2w3e4R ftp://ta1bbn01:21 << EOF
cd $desFolder
mkdir test
cd test
put $srcFil
put $srcFile
bye
EOF
Simple way with ftp:
#!/bin/bash
ftp -inv ip << EOF
user username password
cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload
bye
EOF
With lftp:
#!/bin/bash
lftp -u username,password ip << EOF
cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload
bye
EOF
From lftp manual:
-u <user>[,<pass>] use the user/password for authentication
You can use mkdir for create a directory. And you can use put command several time like this:
put what_you_want_to_upload
put what_you_want_to_upload2
put what_you_want_to_upload3
And you can close connection with bye
You can check folder is exist or not like this:
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exist"
fi
From lftp manual:
-c <cmd> execute the commands and exit
And you can open another connection for put some files.
I don't know how to check folder is exist or not with one connection, but I can do that like this. Maybe you can find better solution:
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test2")
if [ "$checkfolder" == "" ];
then
lftp -u user,pass ip << EOF
mkdir test2
cd test2
put testfile.txt
bye
EOF
else
echo "The directory already exists - exiting"
fi
I used the same basic coding outline as phe however I found that using ls /foldername will output "folder does not exist" if the folder is empty. To solve this I use
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls | grep /test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi
Please note this only works if the folder is in the root directory. For sub directories in a folder the following should work.
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; find | grep home/test1/test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi
First prepare credentials record into ~/.netrc file as:
machine site-url-here
login user-login-here
password user-password-here
so you don't have to expose your password on the command line to use this in script.
Then call:
lftp -e "lftp-command-here" ftps://user-login-here#site-url-here/initial-folder-here/`
In my case I run mget -c * lftp command for getting all logs from java spring boot application running linux app instance at azure infrastructure.
Of course you can put your commands separated by semicolon there.

Script is trying to move a directory instead of a file - need assistance

The script appears to be correct. However, after FTP'ing all the files in the directory, it gives me the error that it is trying to move a directory into a directory of itself.
Any ideas on why this is occurring?
mysql -u ????? -p????? -h ????? db < $SCRIPT_FOLDER/script.sql > script.xls
echo "###############################################################################"
echo "FTP the files"
#for FILE in `ls $SOURCE_FOLDER/`
for FILE in $SOURCE_FOLDER/*.xls
do
echo "# Uploading $SOURCE_FOLDER/$FILE" >> /tmp/CasesReport.copy.out
sshpass -p ???? sftp -oBatchMode=no -b - user#ftp << END
cd /source/directory/
put $SOURCE_FOLDER/$FILE
bye
END
echo "Moving $FILE to $SOURCE_FOLDER/history/"
mv $SOURCE_FOLDER/$FILE $SOURCE_FOLDER/history/$FILE
$FILE already contains $SOURCE_FOLDER, so you put command is doubling the path.
Example
$ cd /tmp
$ touch foo.txt bar.txt
$ cd
$ SOURCE_FOLDER=/tmp
$ for FILE in $SOURCE_FOLDER/*.txt; do echo "put $SOURCE_FOLDER/$FILE"; done
put /tmp//tmp/bar.txt
put /tmp//tmp/foo.txt
Inside the for loop, just use "$FILE"

Unexpected end of file in a shell script, cant find the mistake

HEy there i wrote this little shell script for my pi to upload a picture, but everytime i run the script i get "Unexpected end of file" I does not even show me the first echo.
Thanks for your help :)
raspistill -o snapshot2.jpg
HOST=XXXXX #This is the FTP servers host or IP address.
USER= XXXX #This is the FTP user that has access to the server.
PASS=XXXXX #This is the password for the FTP user.
NOW=$(date +"%c")
echo test
if [ -f work ];
then
echo >> ftp.log "$NOW Script failure"
echo ein prozess arbeitet noch
else
echo beginne upload
touch work
ftp -inv $HOST << EOF
user $USER $PASS
cd /bilder2/
put snapshot2.jpg
bye
echo >> ftp.log "$NOW Upload Success"
rm work
echo erfolgreicher upload
fi
EOF
fi should be placed after EOF, my guess would be that your script should look like:
raspistill -o snapshot2.jpg
HOST=XXXXX #This is the FTP servers host or IP address.
USER= XXXX #This is the FTP user that has access to the server.
PASS=XXXXX #This is the password for the FTP user.
NOW=$(date +"%c")
echo test
if [ -f work ];
then
echo >> ftp.log "$NOW Script failure"
echo ein prozess arbeitet noch
else
echo beginne upload
touch work
ftp -inv $HOST << EOF
user $USER $PASS
cd /bilder2/
put snapshot2.jpg
bye
EOF
echo >> ftp.log "$NOW Upload Success"
rm work
echo erfolgreicher upload
fi

Error trapping scp exit code not working

I have
# Transfer today's CMS backup to a remote backup server
scp -P 55 -r $localdumpdirectory/dirdump-cms-`date +%Y%m%d`.tar.gz root#someserver:/$remotedumpdirectory/ >/dev/null 2>&1
status=${$}
if [[ ${status} != 0 ]]
then
echo "Failed to secure copy directory, with code: ${status}"
exit 1
fi;
Everything is working except that even though the SCP succeeds, I get:
Failed to secure copy directory, with code: 27348
Ideas?
Exit code is $?, not $$. $$ is process ID.
status=${$}
should be
status=$? # or ${?} if you really insist.
If this is bash, see the section "Special Parameters" in the documentation.
Do you want
status=$?
That will give you the status of the last command.
status=$$
This is giving you the last PID
http://tldp.org/LDP/abs/html/internalvariables.html#PROCCID
BTW, you can shorten what you have to:
if scp -P 55 -r $localdumpdirectory/dirdump-cms-`date +%Y%m%d`.tar.gz root#someserver:/$remotedumpdirectory/ >/dev/null 2>&1
then
echo "Failed to secure copy directory, with code: ${status}"
exit 1
fi

Resources