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.
- Create a file in your desired location called autotransfer.sh by opening your editor
- Create the script.
exec 1>>/path/to/transfer.log 2>&1
echo "[$(date)] - Start Transfer"
gsutil cp /path/to/location/* gs://your-bucket/
echo "[$(date)] - End Transfer"
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
- Now save the file.
- Now that it’s save we need to give it executable permissions.
chmod +x /path/to/autotransfer.sh
- Test the script by running it. Make sure you have no errors and the files are transferred.
- Now we want to automate the script. We need to add it to the Cron utility. Open the Cron Utility.
- 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.