Backup de arquivos na Cloud com AWS Amazon S3 e PHP
Boas práticas de Segurança da Informação ditam que não se deve manter os backups somente na mesma máquina onde os dados principais estão. Para ajudar a resolver este ponto específico - manter cópias dos backups em ambiente externo à empresa - vamos exemplificar como utilizar a Amazon S3 como storage para seus dados.
Descrição
Boas práticas de Segurança da Informação ditam que não se deve manter os backups somente na mesma máquina onde os dados principais estão. Para ajudar a resolver este ponto específico - manter cópias dos backups em ambiente externo à empresa - vamos exemplificar como utilizar a Amazon S3 como storage para seus dados.
#!/usr/bin/php
<?php
/*
* Script: s3-send.php
* Author: Edilson Osorio Junior
* Contact: ml: osorio.edilson at eddieoz dot com | twttr: eddieoz
* Date: 2013-01-04
* Description: This script backups a file to some amazonS3 bucket
* Requisites: - An AWS Amazon account
* - S3 bucket
* - Functional AWS SDK for PHP installed
* Usage: s3-send.php --bucket= --file=<fullpath_filename>
*
* More information on http://www.eddieoz.com/us/articles/cloud-computing/43-backup-de-arquivos-na-aws-amazon-s3
*/
error_reporting(-1);
header("Content-type: text/plain; charset=utf-8");
# SDK AWS for PHP
require_once '/installed/binaries/AWSSDKforPHP/sdk.class.php';
# Get and test parameters
$shortopts = "";
$longopts = array(
"bucket:",
"file:",
);
$param = getopt($shortopts,$longopts);
if ( count($param) < 2 ){
print("Params: --bucket= --file=\n");
exit(1);
}
$fullpath_filename = $param['file'];
$bucket = $param['bucket'];
# Initialize AmazonS3
$s3 = new AmazonS3();
# Get just the filename, without path
$filename = explode(DIRECTORY_SEPARATOR, $fullpath_filename);
$filename = array_pop($filename);
# Prepare to send the file
$s3->batch()->create_object($bucket, $filename, array('fileUpload' => $fullpath_filename));
# send the file and get response, thats an array
$file_upload_response = $s3->batch()->send();
# Consists the return
if ($file_upload_response->areOK())
{
# Show the full URL to access the file.
echo $s3->get_object_url($bucket, $filename, '5 minutes') . PHP_EOL . PHP_EOL;
}
else
{
# On error, show the xml
print_r( $file_upload_response );
}
?>