Jump to content

bash-sscript to run on cron


Recommended Posts

I have a drupal module that generates a csv and spits it out onto a blank unmarked-up page. What i need is a bash-script to run on a cron-job to get the csv content and save it as a file (with unix timestamp as its name) onto another server. This is something i have never done before so if anyone would be kind enought to lend a helping hand it would be much apreachated.

Link to comment
Share on other sites

Hm....scp uses the same authentication and level of security as ssh for data transfer, in other words there is no way to provide a password in your bash script and to get the data from the remote server.

 

The best way for me that I know and use to log in without asking any password authentication is to install a trusty certificate between the machine A (client) and the machine B (server), see that - http://www.linuxproblem.org/art_9.html.

 

If both servers are share hosting, probably you don't have any permission to install it, you could be use a "Secure File Transfer Protocol" , vsFTP under linux is a good alternative.

 

Link to comment
Share on other sites

This is my script that I wrote a month ago for one bulgarian guy to dump 3 databases and 3 directories from his local web server and send data to the remote machine using the vsFTP.

 

You can use the same logic to achieve your task:

 

 

#!/bin/bash
 
# MYSQL DATABASE Login info
DBUSER=jazzman
DBHOST=localhost
DBPASS=password
DBNAMES="invoicr test blog"
MYSQLDUMP="$(which mysqldump)"
 
# WEB SERVER Directories
WBNAMES="/var/www/html/invoicr /var/www/html/test/ /var/www/html/blog/"
 
# File archives function, Date function, Current directory of the script
TAR="$(which tar)"
NOW=`/bin/date +%Y%m%d`
DIR=$(dirname $0)
 
# FTP SERVER Login info
HOST='192.168.1.105'  #This is the FTP servers host or IP address.
USER=dummy  #This is the FTP user that has access to the server.
PASSWD=password  #This is the password for the FTP user.
 
 
if [ ! -d $DIR/$NOW ];then mkdir -p $DIR/$NOW;fi
 
${MYSQLDUMP} --host=$DBHOST --user=$DBUSER --password=$DBPASS --protocol=TCP --port=3306 --single-transaction --databases ${DBNAMES} > $DIR/$NOW/$NOW.sql
 
${TAR} zcf ${DIR}/db\-$NOW.tgz ${DIR}/${NOW}/$NOW.sql ${WBNAMES}
 
FILE=${DIR}/db\-$NOW.tgz
 
ftp -ivn $HOST <<FTP_SCRIPT
quote USER ${USER}
quote PASS ${PASSWD}
cd Desktop
put ${FILE}
quit
FTP_SCRIPT
 
# remove the new directory
rm -rf ${DIR}/${NOW}
 
# remove the archive file
rm -rf ${FILE}

 


 

Results:

Connected to 192.168.1.105 (192.168.1.105).
220 (vsFTPd 2.2.2)
331 Please specify the password.
230 Login successful.
250 Directory successfully changed.
local: ./db-20130305.tgz remote: ./db-20130305.tgz
227 Entering Passive Mode (192,168,1,105,130,139).
150 Ok to send data.
226 Transfer complete.
92489086 bytes sent in 1.97 secs (46997.32 Kbytes/sec)
221 Goodbye.

If you want to send a binary data using a sFTP you have to add a binary word to the ftp script.  

 

ftp -ivn $HOST <<FTP_SCRIPT
quote USER ${USER}
quote PASS ${PASSWD}
binary
cd Desktop
put ${FILE}
quit
FTP_SCRIPT

 

Link to comment
Share on other sites

Hm....scp uses the same authentication and level of security as ssh for data transfer, in other words there is no way to provide a password in your bash script and to get the data from the remote server.

That is what ssh keys are for.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.