Zip and ftp script?

Hello, I am looking for a script to zip some folders and ftp the zip files automatically,

is there a script already written somewhere or can someone help me start to write one?

thanks a lot

What have you tried so far? Have you tried to search the forums even....?

Sample script to ftp a file. Hope you know how to create zip ...............

#!/bin/bash
filename="/home/paul/myfile.tar.gz"
hostname="ftp.myhost.com"
username="username"
password="password"
ftp -un $hostname <<EOF
quote USER $username
quote PASS $password
binary
put $filename
quit
EOF

thanks for that, I can create the zip files and ftp no problem but I wanted to automate the process if possible..

I have 900 folders, I need to zip all folders to 900 zip files and then ftp all the zip files but I dont have enough room on my pc to zip them all at once so ideally the script should make a zip then ftp it then delete it, then move on to the next one.

thanks

for X in *
do
        zip -r "$dir.zip" dir
        send_ftp_stuff
        rm "$dir.zip"
done

thanks but I am a beginner, im not sure what to do with that. :confused:

Then see first what this does:

for DIR in *
do
        echo $DIR
done

that showed me a list of the folders in my working directory

function FTP_ZIP()
{
      zip_file=$1
      ftp -n remote_server <<FTP
         quote USER remote_username
         quote PASS remote_password
         prompt noprompt
         put $zip_file
         ls -l
         bye
FTP
}

for X in *
do
        zip -r "$dir.zip" dir
        FTP_ZIP $dir.zip
        rm "$dir.zip"
done

thanks, this is what it said , I tried -i but it didnt work either

zip error: Nothing to do! (try: zip -r .zip . -i dir)
Interactive mode off.
local: .zip: No such file or directory
drwxr-xr-x    6 jo   jo         4096 Jul 24 21:02 .
drwxr-xr-x    6 jo   jo         4096 Jul 24 21:02 ..
drwxr-xr-x    3 jo   jo         4096 Jul 24  2013 test

rm: cannot remove `.zip': No such file or directory
 
for dir in *
do
        zip -r "$dir.zip" $dir
        FTP_ZIP $dir.zip
        rm "$dir.zip"
done

perfect! thanks to all of you guys :stuck_out_tongue:

---------- Post updated at 04:53 PM ---------- Previous update was at 03:56 PM ----------

it was fine until it hit a folder with spaces eg, John set 1

zip error: Nothing to do! (try: zip -r John Set 1.zip . -i John Set 1)

how can I process the folders with spaces?

thanks

quote it. I'm not 100% sure the FTP part will work.

function FTP_ZIP()
{
      zip_file=$1
      ftp -n remote_server <<FTP
         quote USER remote_username
         quote PASS remote_password
         prompt noprompt
         put "$zip_file"
         ls -l
         bye
FTP
}

for dir in *
do
        zip -r "$dir.zip" "$dir"
        FTP_ZIP "$dir.zip"
        rm "$dir.zip"
done

perfect :slight_smile: thanks

hi corona668
may be i am wrong but why do you think the FTP part wont work??
actually i am using the similar way for doing FTP to remote servers and it works fine. one drawback is that you will have to pass your password to the ftp in this way. if you do not want to hard code your username and password in the script, try using .netrc file.

---------- Post updated at 12:15 PM ---------- Previous update was at 08:35 AM ----------

sorry.. function word will not come in front of the function name

FTP_ZIP()
{
      zip_file=$1
      ftp -n remote_server <<FTP
         quote USER remote_username
         quote PASS remote_password
         prompt noprompt
         put "$zip_file"
         ls -l
         bye
FTP
}

for dir in *
do
        zip -r "$dir.zip" "$dir"
        FTP_ZIP "$dir.zip"
        rm "$dir.zip"
done

Because the quotes may confuse it.

I just noticed when I try to unzip the zip files with windows explorer it says... the compressed zip file is invalid!

with windows winrar it says Unexpected end of archive!

but if I open the zip files with linux centos they are just fine

if I run zip -r name.zip name just from the command line the zip file works fine in windows?

Try using a binary FTP. Add the command binary in the FTP deck something like this:-

ftp -inv $host <<-EOFTP >$LOGFILE
user userid password
binary
cd $target_dir
prompt
put "$zip_file"
quit
EOFTP

The default if probably ASCII, and that can corrupt a non-ASCII file in transit.

Robin

adding binary did the trick, thanks :slight_smile:

---------- Post updated at 06:09 AM ---------- Previous update was at 05:30 AM ----------

to take this script one step further.. how can I process more than one folder?

so I execute the script in the root directory with all the folders inside
eg.
folder1 has 100 folders inside
folder 2 has 100 folders inside
etc

so the script would create the folders with the same name on the ftp server and copy them in to the same folder?
resulting in a copy of my files but all zipped individually in there own folders.

currently I have to go in to each folder to execute the script.

I tried adding mkdir "$dir" cd "$dir" but its creating seperate folders for each individual zip file rather than just a folder with the same name with all the zip files inside.

thanks

If you don't want to cd into it, don't. zip will still work, that's what the -r is for.

zip -r filename.zip foldername