Skip to content

Instantly share code, notes, and snippets.

@scoutman57
Forked from oodavid/README.md
Created June 19, 2014 03:28
Show Gist options
  • Save scoutman57/f8659f62a92207883bed to your computer and use it in GitHub Desktop.
Save scoutman57/f8659f62a92207883bed to your computer and use it in GitHub Desktop.

Sync ./uploads/ with Amazon S3

This is a simple way to Sync a folder with Amazon S3 for a nightly backup - this is all to be done on your server :-)

1 - Install s3cmd

this is for Centos 5.6, see http://s3tools.org/repositories for other systems like ubuntu etc

# Install s3cmd
cd /etc/yum.repos.d/
wget http://s3tools.org/repo/CentOS_5/s3tools.repo
yum install s3cmd
# Setup s3cmd
s3cmd --configure
    # You’ll need to enter your AWS access key and secret key here, everything is optional and can be ignored :-)

2 - Add your script

Upload a copy of s3filesync.sh (it will need some tweaks for your setup), make it executable and test it

# Add the executable bit
chmod +x s3filesync.sh
# Run the script to make sure it's all tickety boo
./s3filesync.sh

3 - Run it every night with CRON

Assuming the backup script is stored in /var/www/s3filesync.sh we need to add a crontask to run it automatically:

# Edit the crontab
env EDITOR=nano crontab -e
    # Add the following lines:
    # Run the database backup script at 3am
    0 3 * * * bash /var/www/s3filesync.sh >/dev/null 2>&1

4 - Don't expose the script!

If for some reason you put this script in a public folder (not sure why you would do this), you should add the following to your .htaccess or httpd.conf file to prevent public access to the files:

### Deny public access to shell files
<Files *.sh>
    Order allow,deny
    Deny from all
</Files>
#!/bin/bash
# Based on https://gist.github.com/2346723
# Be pretty
echo -e " "
echo -e " . ____ . _____________________________"
echo -e " |/ \| | |"
echo -e "[| \e[1;31m♥ ♥\e[00m |] | S3 File Backup Script v.0.1 |"
echo -e " |___==___| / © oodavid 2012 |"
echo -e " |_____________________________|"
echo -e " "
# Basic variables
localfolder="/var/www/html/uploads/"
bucket="s3://bucketname"
# Feedback
echo -e "Dumping \e[1;32m$localfolder\e[00m to \e[1;32m$bucket\e[00m"
# Change to the local folder
cd $localfolder
# Sync the current folder
s3cmd sync ./ $bucket
# Jobs a goodun
echo -e "\e[1;32mJobs a goodun\e[00m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment