Iluvatar+ Posted March 25, 2013 Share Posted March 25, 2013 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted March 26, 2013 Share Posted March 26, 2013 Do you have a ssh access to both servers or only ftp? Quote Link to comment Share on other sites More sharing options...
Iluvatar+ Posted March 26, 2013 Author Share Posted March 26, 2013 yes Quote Link to comment Share on other sites More sharing options...
trq Posted March 26, 2013 Share Posted March 26, 2013 Where exactly are you stuck? It should be pretty simple, edit your crontab (crontab -e) and add something like: @hourly scp /path/to/local.csv remote-server-name:/path/to/`date +%s`.csv Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted March 26, 2013 Share Posted March 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted March 26, 2013 Share Posted March 26, 2013 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 Quote Link to comment Share on other sites More sharing options...
trq Posted March 27, 2013 Share Posted March 27, 2013 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. Quote Link to comment Share on other sites More sharing options...
Iluvatar+ Posted March 27, 2013 Author Share Posted March 27, 2013 Thanks for you help guys, sorry i didnt reply sooner. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.