How I automated my backups to Amazon S3 using s3sync.

October 9th, 2006 | Tags: , ,

UPDATE: See my newer article for the way I currently backup to Amazon S3.

Jeremy Zawodny has an excellent article/discussion about the different tools currently available to take advantage of Amazon simple storage service (S3). After testing many tools available for S3 currently, I decided to use the ruby program s3sync to backup my data to S3.
As I explained an earlier post, I wanted a simple low level tool to perform automatic backups S3. I decided to use s3sync to do the heavy lifting and use the jets3t Cockpit GUI to monitor my S3 account. The following explains how I successfully started automating my backups to S3 using s3sync and cockpit.

My server is running Ubuntu Dapper with samba server. All the machines in my house use a “Public” drive on the samba server to store all files from Windows and Linux. All of our important files like photos, home movies, and documents are stored on this “public” drive. This simplifies the backup procedure, since I don’t have to backup multiple sources.

The following steps describe how I backup my “public drive” to Amazon’s awesome S3 storage service. I decided to post this, because I haven’t found a fairly “simple” guide to actually automate backups to S3 that functions similar to rsync on Linux. This is a follow-up post to my original post on choosing a backup solution.


STEP 1: Activate an Amazon s3 account.

Go http://www.amazon.com/s3 and sign up for a s3 web service account

Have your Access Key ID and your Secret Access Key handy.

STEP 2: Install a management tool

(update, I no longer use cockpit, now I use the command line tools that come with s3sync that were not available at the time I wrote this original article, see Option 1.)

Option 1 use the command line shell tools that are included with s3sync (my new preferred method)

Here is a sampling of the commands from the readme file for command line tool, s3cmd.rb that can be used to create buckets and verify upload success or failure. If you use, this option, make sure you have the correct version of ruby installed on your system and you have downloaded the s3sync package (See step 3)

List all the buckets your account owns:

s3cmd.rb listbuckets

Create a new bucket:

s3cmd.rb createbucket BucketName

Delete an old bucket you don’t want any more:

s3cmd.rb deletebucket BucketName

Find out what’s in a bucket, 10 lines at a time:

s3cmd.rb list BucketName 10

Only look in a particular prefix:

s3cmd.rb list BucketName:startsWithThis

I plan to write a shell script to verify success of backup and run via cron job each night, but I haven’t done it yet. I will update here when I do.

Option 2 (original option that I used before s3sync command line shell tools were available)
UPDATE: I have had trouble getting this (or any other GUI) to work for folders containing large amounts of files. If you plan to have thousands of files stored at Amazon, then I suggest option 1.

Download a GUI tool and make sure you can log into your S3 account, create a bucket, add files, and delete them.

I have tried a lot of them, but I prefer jets3t Cockpit. It is java and open source, plus it is able to read objects uploaded to S3 by other tools. Some tools like Jungle Disk create buckets and objects in a propietary format. This means you would not be able to see your files uploaded to S3 by other tools using JD.
Here is a screenshot of Cockpit.

Cockpit
Create a bucket that you will store your backups in. Make sure to give your Bucket a unique name, because bucket names have to be unique for all users of S3. Many recommend to use your Access Key ID from S3 as a prefix. For example, fakeaccesskey1234.backups. For the rest of this article, I will assume our bucket name is “mybucket”.

Cockpit will be a handy tool for you to monitor your backups in S3, but the actual file uploading/downloading will be done with a shell script using s3sync.

STEP 3: Install s3sync (ruby)

s3sync is an open source ruby script that acts similar to rsync, the linux file sync program. Remember to read the README file from s3sync. Also, all the normal warnings apply. Test this on a couple folders and files you don’t care about and make sure you understand what you are doing. Put the source/destination in the wrong order while using the –delete option and you could blow away all of your precious data.

Lets move on.

The following apply to a Debian/Ubuntu based distribution, but could easily be adapted to your own distro.

First, make sure you have ruby 1.8.4 or greater and the ssl lib for ruby or higher

$ sudo apt-get install ruby libopenssl-ruby

check ruby version

$ ruby -v
ruby 1.8.4 (2005-12-24) [i486-linux]

change into the directory where you want to install s3sync, like /home/john/s3sync

download and unpack s3sync

$ wget http://s3.amazonaws.com/ServEdge_pub/s3sync/s3sync.tar.gz
$ tar xvzf s3sync.tar.gz

clean up

$ rm s3sync.tar.gz

make directory for ssl certificates and download some (important, read README for info about these SSL certs)

$ mkdir certs
$ cd certs
$ wget http://mirbsd.mirsolutions.de/cvs.cgi/~checkout~/src/etc/ssl.certs.shar

run this shell archive

$ sh ssl.certs.shar

get back into main s3sync dir

$ cd ..

create two files with your favorite editor, upload.sh and download.sh with the following contents and update to suit your needs. (Important, like rsync, slashes matter, see README for examples)

upload.sh —————————————-

#!/bin/bash
# script to upload local directory upto s3
cd /path/to/yourshellscript/
export AWS_ACCESS_KEY_ID=yourS3accesskey
export AWS_SECRET_ACCESS_KEY=yourS3secretkey
export SSL_CERT_DIR=/your/path/to/s3sync/certs
ruby s3sync.rb -r --ssl --delete /home/john/localuploadfolder/ mybucket:/remotefolder
# copy and modify line above for each additional folder to be synced

download.sh —————————————-

#!/bin/bash
# script to download local directory upto s3
cd /path/to/yourshellscript/
export AWS_ACCESS_KEY_ID=yourS3accesskey
export AWS_SECRET_ACCESS_KEY=yourS3secretkey
export SSL_CERT_DIR=/your/path/to/s3sync/certs
ruby s3sync.rb -r --ssl --delete mybucket:/remotefolder/ /home/john/localdownloadfolder
# copy and modify line above for each additional folder to be synced

NOTICE: These scripts use the –delete option. This means it will delete any file on the destination not on source. Also, these shell scripts contain your Amazon secret info, so you will want to make sure they are only readable by you (chmod 700, credit Kelvin below). You can also add the “-v” option, so you get a verbose about of the changes. I did this this after my initial upload, so I can monitor activity via cron job emails.

Create the local upload and download directories and put some test files in the upload folder

$ mkdir localuploadfolder
$ mkdir localdownloadfolder

change the permissions on the files

$ chmod 700 upload.sh
$ chmod 700 download.sh

Test upload.sh

$./upload.sh

Use s3cmd.rb or Cockpit to make sure you can see the files made it to Amazon.

Test download.sh

$ ./download.sh

The files you uploaded to S3 should now be in your localdownloadfolder.

Once you are confident everything is working fine and your understand what you are doing. Change the shell scripts to backup your actual folders. Run the scripts manually first to ensure everything is working properly. Remember, the upload script will be limited to the upload speed of your ISP, which can be very slow. If you have a typical Cable internet connection upload speed of 384 k it will take approx. 6 hours to upload 1GB. Download speeds are usually much faster, approx 1GB/20 min, but hopefully you never need it.

STEP 4: set up cronjob to run backup script once a week/month etc.

Once you are sure the script is working for your uploads, you can automate the task by creating a cron job to run once a week, day or month. I have it run once a week, because I do nightly backups locally to my Desktop machine using rsync.

$ crontab -e

add the following line.

30 2 * * sun /path/to/upload.sh

save and exit.

Obviously, monitor to make sure everything is working.

STEP 5: kick back and relax

Now you can relax, if your laptop battery explodes and burns down your house, you know your data is safe sitting on Amazon’s geo-redundant servers right between some bits describing a new book from Oprah and a bad review on latest Ben Affleck movie!

Feel free to leave a comment if you find this useful, incorrect, or just plain uninteresting.

UPDATE 1: One additional step I did, was to create one additional bucket where I uploaded all the necessary code/scripts to restore my files using s3sync (minus my s3 information).

UPDATE 2: I have changed the chmod 755 to chmod 700 to make script not readable to all. (Credit Kelvin below). Also, updated the information about the tools I use. I no longer use cockpit to verify success, but I mostly rely on the s3sync command line tools there were not present at the time I wrote the original article.

UPDATE 3: I never gave enough credit to the actual author of s3sync. Without him, this entire process would not be possible, thanks again.

  1. Darren
    December 10th, 2008 at 11:38
    Reply | Quote | #1

    Been meaning to get round to this for ages and your tutorial really speeded up the process. Many thanks

  2. January 4th, 2009 at 18:40
    Reply | Quote | #2

    Thanks for the tutorial & walkthrough. I just setup a syncing cron job and it works well. I prefer S3Fox to the java applets, in case anyone is looking for a quick in-browser way to view your buckets. Again, appreciate the tutorial.

  3. January 13th, 2009 at 15:17
    Reply | Quote | #3

    Hi! I currently have 2 linux servers ,a windows pc and a macbook that I am backing up with backuppc, I was wondering if anybody has ever tried backing up the local backuppc files to S3. I haven’t seen any backuppc integration with S3 as of now.

  4. February 14th, 2009 at 10:39
    Reply | Quote | #4

    I can’t get this to work for the life of me. Each time I run this command:

    ruby s3sync.rb -vdn -r –ssl /home/myfolder/localuploadfolder/mysql/ mybucketname:mysql

    All I get is:

    s3Prefix mysql
    localPrefix /home/myfolder/localuploadfolder/mysql/
    localTreeRecurse /home/myfolder/localuploadfolder/mysql
    s3TreeRecurse mybucketname mysql
    Creating new connection
    Trying command list_bucket mybucketname max-keys 200 prefix mysql delimiter / with 100 retries left
    Response code: 200

    I’ve tried adding a slashes before the prefix, after, all kinds of options and always get the same response. Any ideas?

  5. April 4th, 2009 at 10:36
    Reply | Quote | #5

    Brilliant, man… brilliant. I’m such a bad administrator — I run many websites from my home server for family and friends, but have never backed them up. I never had a problem, so I always thought “I’ll get to it tomorrow”. Your article helped me finally get around to it!

  6. svittal
    April 27th, 2009 at 10:06
    Reply | Quote | #6

    I’m trying to sync /var/log to S3.
    There are few files in /var log which belong to ‘syslog.adm’
    I see files owned by root.root is begin transferred without any problem. files owned by syslog.adm is not moved.

    any idea why?

  7. Anonymous
    September 23rd, 2009 at 10:02
    Reply | Quote | #7

    A high-frequency s3sync over a large number of files is costly in terms of LIST request.

  8. November 13th, 2009 at 12:13
    Reply | Quote | #8

    Thanks for the great post. I checked the s3sync readme as well but could not figure out how to monitor progress (I am not much of a Linux person). Is there a way to write the progress to a log file (what has been uploaded etc.)?

  9. Simon
    November 24th, 2009 at 04:44
    Reply | Quote | #9

    Great article – thanks.

    Just to note that, in order to get s3sync to work, I had to make a small change to my s3config.rb to get it to check for the s3config.yml file in the local directory, as follows:-
    FROM: confpath = ["#{ENV['S3CONF']}”, “#{ENV['HOME']}/.s3conf”, “/etc/s3conf”]
    TO: confpath = ["./", "#{ENV['S3CONF']}”, “#{ENV['HOME']}/.s3conf”, “/etc/s3conf”]

    Hope this helps someone!

  10. December 14th, 2009 at 03:52

    Great article!
    I’m going to adapt it a bit to my own needs, in combination with backup2l, and then I’ll write a detailed article about it on my blog. That will be in Dutch.

    I’m also thinking about “bouncing” an EC2 server:
    * start the EC2 server
    * rsync from my machine to EC2
    * copy the data from EC2 to S3
    * shut down the EC2
    Ideally it would take less than one hour to do this so it would only cost me a couple of cents per day (or per week) to run the EC2 and I could use the rsync protocol more efficiently.

    By the way, you may want to delete some of the spam comments

  11. August 12th, 2010 at 02:04

    Will this method still work?

  12. August 16th, 2010 at 07:47

    @Slow Down Music:
    I prefer BOTH: a hands-on backup AND a backup at a remote location. Preferably in another continent.
    Just in case a meteor strikes… ;-)

  13. December 28th, 2010 at 06:28

    can find many tutorials on how to use s3sync to do the backups as well. All that is very easy. Too easy actually.

  14. Rone
    January 10th, 2011 at 17:38

    For those freeBSD users, here’s a little tip to get around the “iconv” not found error when trying to run the ./upload.sh

    see this post:http://s3sync.net/forum/index.php?topic=146.0

    basically go to /usr/ports/converters/ruby-iconv

    run “make”
    run “make install”

    and it worked for me.

  15. January 24th, 2011 at 16:42

    Is there anything better now?

  16. Ganesh
    January 29th, 2011 at 16:32

    Upload script worked fine for me. But download script does not work.
    Script used is
    ruby s3sync.rb -r –ssl –delete my_BACKUP:/MYSQL /home/backups/DOWNLOADS/

    Can anybody help me in this.

    Thanks in advance.

    Raj

  17. February 4th, 2011 at 12:35

    Doing this where you have many files costs $ due to the request number to amazon.
    My approach is:
    - local backup with rsnapshot (incremental)
    - tarball with cron
    - upload with a single request
    what do you think?

  18. February 5th, 2011 at 10:37

    I’m still getting errors when I do this. Is there a help desk that I can contact? Any advice, anyone?

  19. February 14th, 2011 at 20:16

    I tried and got some problems with this method. It took me a long time to figure it out.

  20. February 18th, 2011 at 17:35

    Thanks a lot for sharing your knowledge on how to backup “public drive” to Amazon’s awesome S3 storage service.

    I appreciate, well done.

  21. Tony
    February 26th, 2011 at 06:15

    There is also a graphical tool for synchronization on Linux : http://www.dragondisk.com/

  22. March 15th, 2011 at 02:09

    Thanks for all the steps that you posted in the articles. It really makes my work easier and faster. You really walk me through easily. You’re wonderful.

  23. Novice
    September 26th, 2011 at 18:52

    I tried using a similar command to copy files FROM an S3 bucket to a local folder, but didn’t work. Any thoughts on what I’m doing wrong??

    ruby /home/web/s3sync/s3sync.rb -s -d domain_images:/ /home/web/webimages_backup

  24. January 10th, 2012 at 11:24

    awesome.
    did learned lot… :)
    thanks!!

  25. January 26th, 2012 at 18:18

    For those who use Windows Servers or Windows in general, I created a free backup tool (single exe) to backup using 7-Zip compression and Amazon S3 automatically.

    So, you can create a Windows Task and run the B7 tool on a daily basis passing command line parameters. If you don’t specify any parameters, the Windows GUI comes up. It has basic S3 management.

    It’s free and works on my Windows server(s). You can download the B7 tool (1.5 MB) from my blog at http://thomasjaeger.wordpress.com

    It does not require any dlls or run-time, it’s a pure Windows app.

    Hope this helps someone.

    Thomas Jaeger

  26. Nitish
    February 25th, 2012 at 03:52

    Hello

    Please help me out. When i run upload.sh file the follwing error is keep on flasnhig.

    s3 error
    s3sync.rb:290:in `+’: can’t convert nil into Array (TypeError)

  27. July 6th, 2012 at 14:08

    Thanks for every other great article. The place else
    may just anyone get that type of info in such an ideal means of writing?
    I’ve a presentation subsequent week, and I am on the look for such info.

  28. August 22nd, 2012 at 22:51

    I seldom leave responses, however i did a few
    searching and wound up here John Eberly How I automated my backups to Amazon S3 using s3sync..
    And I do have 2 questions for you if it’s allright. Could it be only me or does it look as if like a few of these remarks appear like they are written by brain dead folks? :-P And, if you are posting on other online sites, I would like to follow everything new you have to post. Could you list of the complete urls of your community pages like your Facebook page, twitter feed, or linkedin profile?

  29. I don’t know whether it’s just me or if everybody else encountering issues with
    your blog. It appears as if some of the text in your posts
    are running off the screen. Can someone else please provide feedback and let me know if this
    is happening to them as well? This may be a issue with my browser because I’ve had this happen before. Kudoshttp://emaciatedthales.cage-rage-newspage.com

Comment pages

49 trackbacks

  1. Filter for 11/10 2006 - Felt Pingback | 2006/10/10
  2. links for 2006-10-14 « Amy G. Dala Pingback | 2006/10/14
  3. PapaScott » Blog Archive » links for 2006-10-16 Pingback | 2006/10/15
  4. How I automated my backups to Amazon S3 using s3sync. « The other side of the firewall Pingback | 2006/10/16
  5. The JJW Blog :: links for 2006-10-16 Pingback | 2006/10/17
  6. links for 2006-10-23 at 59ideas Pingback | 2006/10/23
  7. Marc Abramowitz » links for 2006-11-01 Pingback | 2006/11/01
  8. links for 2006-11-06 « Gobán Saor Pingback | 2006/11/06
  9. links for 2006-11-14 « Gobán Saor Pingback | 2006/11/14
  10. links for 2006-11-29 « Bloggitation Pingback | 2006/11/28
  11. tecosystems » Friday Grab Bag From Frigid Denver Pingback | 2007/01/12
  12. tecosystems » The RedMonk IT Report: S3/ZRM for Backup Pingback | 2007/01/13
  13. David Dorgan ’s Weblog » 26-03-2007: S3 and some tools put to the test… Pingback | 2007/03/26
  14. Avinash Meetoo: Blog » Blog Archive » Synchronizing two computers using Amazon S3 Pingback | 2007/04/18
  15. knolleary » Blog Archive » S3, EC2, SQS - The AWS Triumvirate Pingback | 2007/06/05
  16. John Eberly’s Geek Blog :: How I automated my backups to Amazon S3 using s3sync. Pingback | 2007/06/10
  17. Unatine :: blog : links for 2007-07-30 Pingback | 2007/07/30
  18. Link With Reality Web Log » links for 2007-07-31 Pingback | 2007/07/30
  19. Michael Gorsuch, Timid Iconoclast » links for 2007-07-31 Pingback | 2007/07/30
  20. Amazon S3 Storage Tools | Vinod Live! Pingback | 2007/08/19
  21. s3 is Trackback | 2007/08/30
  22. Tech Messages | 2007-09-11 | Slaptijack Pingback | 2007/09/11
  23. Nelson’s Backups Pingback | 2007/10/13
  24. links for 2007-11-13 Pingback | 2007/11/13
  25. Synchronizing two computers using Amazon S3 | RelatedSeek.com Pingback | 2007/12/09
  26. On Amazon S3 and competitive advantage « by jan Pingback | 2007/12/23
  27. Rafael Lima » Configurando sistema de backup do banco de dados MySQL no Amazon S3 em 10 minutos Pingback | 2008/03/19
  28. Resources and Tools for Amazon Services « mindstorms Pingback | 2008/03/26
  29. How To: Bulletproof Server Backups with Amazon S3 - PaulStamatiou.com Pingback | 2008/03/29
  30. David Laing’s blog » Blog Archive » S3 Backup options: s3sync & duplicity Pingback | 2008/05/10
  31. How I automated my backups to Amazon S3 using s3sync. | John Eberly « The other side of the firewall Pingback | 2008/05/17
  32. Tournez les boutons » Blog Archive » links for 2008-06-18 Pingback | 2008/06/17
  33. Web Host Blog » Grid Computing, Backups and Hurricanes: Web Hosting Blog, News Bytes, Features, Commentary, Future Trends, We are the Industry Experts Pingback | 2008/08/29
  34. Web Host Blog | Grid Computing, Backups and Hurricanes Pingback | 2008/08/31
  35. Backup sulla nuvola con Amazon S3 « Dema (fon) blog Pingback | 2009/03/23
  36. links for 2009-04-09 - ArcIris- Web Design in Spain, Remote Support | Diseño Web y Soporte Remoto · Arc Iris Pingback | 2009/04/09
  37. Amazon S3 | I-Tek Pingback | 2009/04/29
  38. links for 2009-05-14 « Where Is All This Leading To? Pingback | 2009/05/14
  39. Cheap Home Network Storage | gtuhl: startup technology Pingback | 2009/07/03
  40. Managing Amazon S3 Online Storage with S3sync « Rforge Pingback | 2009/10/31
  41. Amazon S3 Backup script with encryption | *.hosting Pingback | 2010/02/16
  42. Grid Computing, Backups and Hurricanes « Features « Web Host Blog Pingback | 2010/03/30
  43. Automating backups with Amazon S3 on Linux Pingback | 2011/01/20
  44. How I automated my backups to Amazon S3 using rsync and s3fs. Pingback | 2011/01/23
  45. Neue Backuplösung mit Amazon S3 | aquasonic.ch Pingback | 2011/09/09
  46. S3 command failed if the time is not synced | LogikDevelopment Pingback | 2011/12/06
  47. How To: Bulletproof Server Backups with Amazon S3 « Denis Bosire Pingback | 2011/12/30
  48. Script Installation Trackback | 2012/03/12
  49. Remote Backup to Amazon S3 | Adam's Space Pingback | 2012/04/29