Automatmate Transfer of Files from Apache Server to Google Cloud Storage

If you want to Automate the transfer of files from an apache server to Google Cloud Storage the below Bash script will transfer the files and create a transfer log of the events.

  1. Create a file in your desired location called autotransfer.sh by opening your editor
    nano /path/to/location/autotransfer.sh

     

  2. Create the script.
    #!/bin/sh
    exec 1>>/path/to/transfer.log 2>&1
    echo "[$(date)] - Start Transfer"
    gsutil cp /path/to/location/* gs://your-bucket/
    echo "[$(date)] - End Transfer"
    echo "\n"
    
    exit
    

    The above script will transfer all files in a directory to your google storage bucket. This is ideal for backup files, but if you want to add just one file, just remove the * and add the file name. This script will also add the results of the transfer to a log file called transfer.log

  3. Now save the file.
  4. Now that it’s save we need to give it executable permissions.
    chmod +x /path/to/autotransfer.sh

     

  5. Test the script by running it. Make sure you have no errors and the files are transferred.
    /path/to/autotransfer.sh

     

  6. Now we want to automate the script. We need to add it to the Cron utility. Open the Cron Utility.
    crontab -e

     

  7. I want to run my transfer at about 3 am my time. Most servers are set to UTC time so you will need to adjust it depending on your location. For me, 11am UTC time is 3am my time, so I add this this line to Cron.
    0 11 * * * /path/to/autobackup.sh
    

     

    That’s all there is to it.

Leave a comment