Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    52

    Bash script for batch upload files on server

    hello

    I have my own VPS and I need to make often some modification of files under all my present and future users customer account (inside public_html) and I would like to know if you can give me some advice to how I can do...

    For example I have 2 files must be uploaded to 100 users account exactly to same location (for example inside public_html/folder/myfolder/). How I can perform this job without need to connect me using FTP 100 time with 100 different user account data ??? No possible to automate this task with a batch upload or other method ???

    In fact I have found a ftp tools (but under window) permitting to batch/upload specific files according a list of pre-determinate users account...This software open a ftp connexion to one user account, upload files, close the connexion and so on... The problem with this software it can not upload in SFTP and are not able to chmod files if needed...

    My VPS provider tell me is possible to make a simple bash script making this kind of job....but unfortunately am not able to code a script..

    Somebody can help me ? Anybody have a kind of script ?

    Any suggestion are mutch appreciate...thanks

  2. #2
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Upload it (the changed files) once and only once, to your own account on the server, and then with escalated privileges (i.e. sudo), copy to the other accounts using a script. I've given an example AS A STARTING POINT ONLY, to illustrate to you more than anything that it isn't that complicated to write such a script.

    Do not just blindly run this without creating some mirror file system and checking it out.

    Code:
    for dir in `ls /usr/home`
    do
        # skip your own directory
        if [ "$dir" == "mydirectory" ] 
        then
            echo "Skipping $dir - my own home directory"
            continue
        fi
        for f in some_updated_filename another_updated_filename
        do
            cp $f /usr/home/$dir/public_html/$f
        done
    done
    Any suggestion are mutch appreciate
    I have an important suggestion:

    If you find simple shell scripting too difficult, you should not run any scripts. For all you know I've given instructions to wipe out all your user directories. You *must* take some responsibility for this and dig in and learn a little, and practice somewhere other than on a live server.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  3. #3
    Join Date
    Mar 2009
    Posts
    52
    Thank for your reply....

    I have get also another kind of script suggestion:

    while read user
    do
    sftp [email protected] << EOF
    cd /home/${user}/public_html/folder/myfolder/
    put file
    chmod 744 file
    EOF
    done < /path/to/list/of/users

    ..but this work only with files and not with folder....Another suggestion working with folder give to me:

    #!/bin/bash
    while read user
    do
    scp -pr /path/to/local/folder [email protected]:/home/${user}/public_html/folder/myfolder/.
    ssh -n [email protected] "chown ${user}:users /home/${user}/public_html/folder/myfolder/folder."
    done < /path/to/list/of/users

    Yes I must make some test...I have a VPS in production but maybe is better for me to rent for short time another server just for make my test...

  4. #4
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    I specifically avoided telling you to scp using a user with escalated privileges (**root**) because in my view that is dangerous. Most also will disable remote root logins.

    Another reason to avoid that: slower. Uploading once and then distributing is always going to be faster.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  5. #5
    Join Date
    Mar 2009
    Posts
    52
    Quote Originally Posted by mwatkins View Post
    I specifically avoided telling you to scp using a user with escalated privileges (**root**) because in my view that is dangerous. Most also will disable remote root logins.

    Another reason to avoid that: slower. Uploading once and then distributing is always going to be faster.
    but what you mean about "Uploading once and then distributing is always going to be faster" ??

    How I can distribute files and folder to each user account without to be root ?

    In fact for me is equal the method....My goal is to automate the task and do this with the most secure process and avoid possible issue...I know making modification with root privilege is dangerous...am not confident with this...I can maybe just upload all stuff using sftp using a list of each username, password and path to folder ?? Like that am sure each user account will have they files uploaded correctly (also without worry about permission) because it is probably the same then if each user make this job using they ftp client...If the processe are slow is not big deal...

  6. #6
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    I prefer not to ever enable remote root login; the script examples you've unearthed both depend on remote root login. Nothing in your set of stated requirements requires a remote root login. Do not create potentially larger problems while trying to solve another problem.

    Break the problem down into pieces:

    1. Copy files or folders from local machine to remote host; no root login required, just a regular user (i.e. yours), one who has sudo permissions granted.

    2. Initiate what is then a local copy from your staging area on the remote host to user directories / file locations on the remote host - i.e. it is all local at that point; employ a regular user account (yours) and sudo.

    You have enough pieces in the above comments and script bits I've provided and you have found to figure it out now. Do test it somewhere first before unleashing it on your users.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  7. #7
    Join Date
    Mar 2009
    Posts
    52
    Thank for all your advice...you are right...I will take time to learn and test....

Similar Threads

  1. Upload files script needed
    By vegemite in forum Software & Scripts Requests
    Replies: 4
    Last Post: 05-20-2009, 10:27 AM
  2. php script to slice upload large files
    By OnlineRack in forum Programming Discussion
    Replies: 10
    Last Post: 10-25-2007, 02:30 PM
  3. Batch upload script (via flash?)
    By yegorpb in forum Programming Discussion
    Replies: 0
    Last Post: 02-20-2007, 12:23 PM
  4. Bash script to recrusivly open files in a folder and replace text?
    By matt2kjones in forum Programming Discussion
    Replies: 1
    Last Post: 09-06-2006, 05:29 PM
  5. How to count files in a directory? (bash script)
    By Dan Grossman in forum Programming Discussion
    Replies: 7
    Last Post: 07-15-2006, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •