How I automated my backups to Amazon S3 using s3sync.
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.

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.

Been meaning to get round to this for ages and your tutorial really speeded up the process. Many thanks
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.
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.
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?
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!
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?
A high-frequency s3sync over a large number of files is costly in terms of LIST request.
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.)?
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!
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
Will this method still work?
@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…
can find many tutorials on how to use s3sync to do the backups as well. All that is very easy. Too easy actually.
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.
Is there anything better now?
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
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?
I’m still getting errors when I do this. Is there a help desk that I can contact? Any advice, anyone?
I tried and got some problems with this method. It took me a long time to figure it out.
Thanks a lot for sharing your knowledge on how to backup “public drive” to Amazon’s awesome S3 storage service.
I appreciate, well done.
There is also a graphical tool for synchronization on Linux : http://www.dragondisk.com/
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.
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
awesome.
did learned lot…
thanks!!
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
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)
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.
I seldom leave responses, however i did a few
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?
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?
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