• FTP script to automate file transfers to remote server

    Below you will find a FTP script that will allow you to automate your files transfers without requiring any user input. The only problem with this method is you will be putting a username and password into the script file, but having a simple automated FTP file upload or FTP file download probably outweighs the drawback.

    So to describe what this script does:
    First the script will tar and zip all files and folders in the /var/log/ folder to the home directory of the user who runs the script. This will create a dated zip file in the user’s home folder who ran the script. Then it will FTP the files to the remote host. You can then add the script to a cron job to execute it everyday, week, hour, etc.

    This is the scipt, it is fairly well commented so you know what is going on:

    
    #!/bin/sh
    # Tar and zip log files with todays date
    tar cvf ~/logs.$(date +%F).tar /var/log/*
    gzip logs.$(date +%F).tar
    
    # Add username and PW
    REM_USER=USERNAMEHERE
    REM_PASS=PASSWORDHERE
    
    {
    # Open FTP connection to server
    echo "open FTPHOSTNAMEHERE"
    
    # pass user login from above
    echo "user $REM_USER $REM_PASS"
    
    # If FTP server is requiring user input toggle the echo "prompt" below
    # echo "prompt"
    
    # make sure you are in the folder where you made the zip on the local server
    echo "lcd ~/"
    
    # Change to remote folder [optional]
    echo "cd remotefolder"
    
    # if you want to put multiple files use mput, put will be fine for 1 file
    echo "mput *.gz  "
    
    # Connect to the FTP with the above info
    } | ftp -vin
    
    # Remove the created zip file
    rm logs.$(date +%F).tar.gz
    
    

    If you run this in the root crontab, it will ensure you have access to all the file you want to zip.

    Share

    Leave a reply