benfany
Posts: 4
Joined: Sun Mar 30, 2014 2:03 am

upload file to FTP server

Sat Apr 19, 2014 7:40 am

hi everyone,

i am making project to read the temperature , pressure using AVR chip, and transferred to Raspberry pi,
i have already successfully generate txt File on my raspi folder, and next step this txt file will be uploaded into my ftp server in different network on ftp://xxx.yyy.zzz.nn (need username and password to access this ftp server).

some tutorial give the idea to make PHP script which will be activated using cron daily,

i have no idea how to make this script and setting cron..

can someone let me know the php script and cron setting to make this automatically uploaded to my ftp server ?

file name : DSF-01.txt

thanks and regards,
benfany

User avatar
DougieLawson
Posts: 39198
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: upload file to FTP server

Sat Apr 19, 2014 10:51 am

Don't use FTP, it's insecure.

Run sshd on the machine you're targeting and use scp on the RPi to send files over there. Or set-up a folder on the target that's shared with nfs or cifs and mount that on the RPi.

scp (using certificate authentication) doesn't need any special handling. It works just like a stock cp command (with a few syntactic anomalies).

scp /directory/subdirectory/filename_at_localhost user@remotehost:/remotedir/remotesubdir/filename_at_remote
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

benfany
Posts: 4
Joined: Sun Mar 30, 2014 2:03 am

Re: upload file to FTP server

Sun Apr 20, 2014 4:42 am

Hi, thanks for suggestion

I would like to use about 10 raspberry and connected to my server. On my raspberry will be using GSM/3G internet connection, can we make it ssh with this situation? 10 raspi and 3G internet connection (dynamic IP)

Regards,
Benfany

User avatar
DougieLawson
Posts: 39198
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: upload file to FTP server

Sun Apr 20, 2014 8:30 am

As long as your 3G service doesn't block port 22 then SSH is the best option.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Tue Dec 15, 2015 6:53 pm

As I understand I need to use scp to transfer files from one pi to another but how can I do that

I am using this script

Code: Select all

    #!/bin/bash
    # LOCAL/FTP/SCP/MAIL PARAMETERS
    SERVER="192.168.0.10"   # IP of Network disk, used for ftp
    USERNAME="root"         # FTP username of Network disk used for ftp
    PASSWORD="root"         # FTP password of Network disk used for ftp
    DESTDIR="/opt/backup"   # used for temorarily storage
    DOMO_IP="192.168.0.90"  # Domoticz IP 
    DOMO_PORT="8080"        # Domoticz port 
    ### END OF USER CONFIGURABLE PARAMETERS
    TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`
    BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
    BACKUPFILEGZ="$BACKUPFILE".gz
    ### Create backup and ZIP it
    /usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/$BACKUPFILE
    gzip -9 /tmp/$BACKUPFILE
    ### Send to Network disk through FTP
    curl -s --disable-epsv -v -T"/tmp/$BACKUPFILEGZ" -u"$USERNAME:$PASSWORD" "ftp://$SERVER/media/hdd/Domoticz_backup/"				
    ### Remove temp backup file
    /bin/rm /tmp/$BACKUPFILEGZ
    ### Done!
But that is not working for me and as you say its not save.
So how can I change this script so its working and its secure. When script is done I want to schedule it with crontab so its backup daily

User avatar
DougieLawson
Posts: 39198
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: upload file to FTP server

Wed Dec 16, 2015 9:00 am

Why not use scp? That's already there and will work.

Code: Select all

scp /tmp/$BACKUPFILE pi@192.168.0.90:/$SERVER/media/hdd/Domoticz_backup/$BACKFILE
The only pre-req for doing that is
sshkey-gen
ssh-copy-id pi@192.168.0.90

that generates /home/pi/.ssh/id_rsa and /home/pi/.ssh/id_rsa.pub then sends the public key from the source RPi to /home/pi/.ssh/authorized_keys on the target machine. That allows you to use scp without a password. You can do the same stuff for the root id (except you'll have to send the key by copying it a USB stick, or by using the pi userid to send it over the network).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Wed Dec 16, 2015 5:46 pm

And when I use scp I still can schedule my *.sh to run daily?

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Wed Dec 16, 2015 6:41 pm

Question:when I use scp , can I still schedule my *.sh to run daily? in crontab?

When I use ssh pi@192.168.5.10 I can login into other pi without password but when I run below SH I need to logon

Code: Select all

#!/bin/bash

## LOCAL/FTP/SCP/MAIL PARAMETERS
SERVER="192.168.5.10"         # IP of Network disk, used for: ftp mail scp
DOMO_IP="192.168.5.75"    # Domoticz IP used for all
DOMO_PORT="8080"            # Domoticz port used for all
## END OF USER CONFIGURABLE PARAMETERS

TIMESTAMP=`/bin/date +%Y%m%d%H%M%S`

## BACKUP DATABASE
BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named "domoticz_YYYYMMDDHHMMSS.db.gz"
BACKUPFILEGZ="$BACKUPFILE".gz

## BACKUP SCRIPTS

BACKUPFILEDIR="domoticz_all_$TIMESTAMP.tar.gz"

### Create backup and ZIP it
/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /tmp/$BACKUPFILE
gzip -9 /tmp/$BACKUPFILE
##Back domoticz folder incl database
tar -zcvf /tmp/$BACKUPFILEDIR /home/pi/domoticz/

### Send to Network disk through SCP
scp /tmp/$BACKUPFILE pi@192.168.5.10:/$SERVER/media/WDBOOK/Domoticz_backup/$BACKFILE
scp /tmp/$BACKUPFILEDIR  pi@192.168.5.10:/$SERVER/media/WDBOOK/Domoticz_backup/$BACKFILEDIR

### Remove temp backup files
/bin/rm /tmp/$BACKUPFILEGZ
/bin/rm /tmp/$BACKUPFILEDIR

### Done!
I run Sudo Backup.sh

after i will i see
pi@192.168.5.10's password:
/tmp/domoticz_20151216193620.db: No such file or directory
pi@192.168.5.10's password:
scp: /192.168.5.10/media/hdd/Domoticz_backup/domoticz_x_xxx_20151216193620.tar.gz: No such file or directory


What am I doing wrong
Last edited by rjobaan on Thu Dec 17, 2015 9:18 am, edited 1 time in total.

DirkS
Posts: 10367
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: upload file to FTP server

Wed Dec 16, 2015 7:01 pm

rjobaan wrote:What am I doing wrong
I think it's the use of 'sudo'.
scp / ssh will look for the key in the 'root' home dir instead of the one for 'pi' (that's the one you generated).

Looking at the commands you use, you don't need sudo at all, so just leave it out.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Thu Dec 17, 2015 8:39 am

You are right now I don't have to fill in password
And I saw a mistake in script :
I changed script into

Code: Select all

### Send to Network disk through SCP
scp /tmp/$BACKUPFILEGZ pi@192.168.5.10:/$SERVER/media/hdd/Domoticz_backup/
scp /tmp/$BACKUPFILEDIR  pi@192.168.5.10:/$SERVER/media/hdd/Domoticz_backup/
but still I am getting
scp: /192.168.5.10/media/hdd/Domoticz_backup/ No such file or directory
scp: /192.168.5.10/media/hdd/Domoticz_backup/ No such file or directory

when I run the scp code directly it is working
scp /tmp/domoticz_20151217095013.db.gz pi@192.168.5.10:/$SERVER/media/hdd/Domoticz_backup/
Last edited by rjobaan on Thu Dec 17, 2015 9:17 am, edited 1 time in total.

DirkS
Posts: 10367
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: upload file to FTP server

Thu Dec 17, 2015 9:08 am

rjobaan wrote:

Code: Select all

### Send to Network disk through SCP
scp /tmp/$BACKUPFILEGZ pi@192.168.5.10:/$SERVER/media/WDBOOK/Domoticz_backup/
scp /tmp/$BACKUPFILEDIR  pi@192.168.5.10:/$SERVER/media/WDBOOK/Domoticz_backup/
but still I am getting
scp: /192.168.5.10/media/hdd/Domoticz_backup/ No such file or directory
scp: /192.168.5.10/media/hdd/Domoticz_backup/ No such file or directory
Shouldn't the target of the operation just be the local path on the remote? In your command the remote IP address is added at the beginning of the target path

Code: Select all

scp /tmp/$BACKUPFILEGZ pi@192.168.5.10:/media/WDBOOK/Domoticz_backup/
or

Code: Select all

scp /tmp/$BACKUPFILEGZ pi@$SERVER:/media/WDBOOK/Domoticz_backup/

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Thu Dec 17, 2015 9:22 am

yes that was it. I removed $Server and it worked thanks!

I was wondering what is the difference between *.gz (this is used for backing up database) and *.tar.gz (this is used to backup the whole folder)
It seems I can not unzip *.gz files but I can *.tar.gz

So now next challenge

How can I combine those db files into one?
I have SQL Light running

User avatar
DougieLawson
Posts: 39198
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: upload file to FTP server

Thu Dec 17, 2015 11:09 am

rjobaan wrote:
So now next challenge

How can I combine those db files into one?
I have SQL Light running
A new challenge == A NEW THREAD!
posting.php?mode=post&f=36
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: upload file to FTP server

Thu Dec 17, 2015 12:03 pm

DougieLawson wrote:
rjobaan wrote:
So now next challenge

How can I combine those db files into one?
I have SQL Light running
A new challenge == A NEW THREAD!
posting.php?mode=post&f=36
Oh sorry ofcourse. Thank you very much for your help for far

How can I send file directly without zipping but adding timestamp to filename??

Code: Select all

scp /home/pi/domoticz/domoticz.db pi@/192.168.5.10:/media/hdd/Domoticz_backup/

Return to “Networking and servers”