Angel
Angel

Reputation: 13

Backup database and web folder in EC2

I have moved my first site to a EC2 micro instance, now the project is working I am trying backup database and images folder, if possible inside Amazon (Glacier? S3?). I have read a lot about it, but I am sure anyone has scripted this before.

Stack: - Ubuntu Server 12.04 LTS - Apache 2.2.1 - PHP 5.4.4

Upvotes: 1

Views: 2457

Answers (3)

BraveNewCurrency
BraveNewCurrency

Reputation: 13065

Generally, you want to use EBS snapshots as your first line of defense. Make a script that creates an EBS backup once per day and deletes the older ones. Put it in cron.

  • If something gets accidentally deleted on your server, create an EBS drive from your snapshot and mount it somewhere, then copy off what you need.
  • If the server hardware dies, you can turn your last snapshot into an EBS drive and boot from it. There is an optimization where you take the existing drive from your dead computer and boot from it -- but don't rely on that working. Sometimes when your server fails, the drive is "stuck" for hours while disconnecting. Plus you can only restore to computers in the same Zone, which isn't as flexible.
  • If you need to read data from an old backup, you can boot a new server for an hour and copy the data off. (Total cost: 8 or 9 cents. Be sure and delete the drive after.)
  • Note that your backups will share data blocks that have not changed. This means each backup looks and acts like a full backup, but you are charged more an incremental backup.

You can consider S3 if you want "extra insurance" in case EBS/EC2 have massive problems. Use s3sync as pointed out in the other answers. It's best to use compression. There is no "cost sharing" between backups like there is with EBS snapshots.

Glacier has a very long wait time for restoring (many hours) and a minimum restore time (you get a penalty if you restore before 90 days). Therefore, you should only look into it if you want to store a lot of backups for a long time. You cannot use this for disaster recovery, unless you're fine with being down for hours.

Upvotes: 1

it´s very basic script, these are the steps:

  • Install s3cmd from official site
  • Invoke s3cmd in your shell and add your AWS credentials
  • Create a new S3 bucket to store the files, better than Glacier
  • In your bucket properties set lifecycle to autoremove them after 90 days
  • Go back to your shell and paste the script below
  • Now create a log file /var/log/my_backups_to_s3
  • Give permissions to the script to execute and the log file
  • Try it with ./your_script_name

EXAMPLE SCRIPT TO MYSQLDUMP AND PUSH IT TO S3

#!/bin/bash    
MY_FOLDER="/__PATH_TO_WRITABLE_FOLDER__/"
    NOW="`date +%Y-%m-%d-%R`"
    FILE=$MY_FOLDER"___FILE_NAME___"$NOW".sql"
    mysqldump -h localhost -u USER_DB -pPASSDB -c --add-drop-table --add-locks --quick --lock-tables DBNAME > $FILE
    s3cmd put $FILE s3://___YOUR_BUCKET_NAME___
    echo "`date -u`" "BACKUP DONE - MySQL uploaded to hipespace ^^ -> ".$FILE  >> /var/log/my_backups_to_s3     

NOTES:

  • There is no space between -p and PASSDB
  • I save the file before pushing to S3, this is not mandatory
  • Bucket need about 30 min to be fully available, before that I had some connection errors

anyway this is only to backup the db, give a try to s3cmd docs, they have another command "sync" that you can user to push your images,

hope it helps

Upvotes: 0

datasage
datasage

Reputation: 19563

This is rather easy to do. You probably want to use s3 for now.

  1. Install s3cmd. It may already be available in your repositories
  2. Decide on how you want to back up your files. Do you want to just sync all your images for example, or do you want to create a new copy of your images folder each day? Use s3cmd sync or put depending on your preferred strategy.
  3. Dump your database to an SQL file. Compress that sql file. You probably want to include the date in the name like YYYY-MM-DD.sql.gz
  4. Copy that file with s3cmd to the bucket you prefer.

You can script all this together into a simple bash script an execute it with cron on a regular interval. Keep in mind that backups do impact performance somewhat, and should be scheduled during off peak periods.

Upvotes: 0

Related Questions