Skip to content

Instantly share code, notes, and snippets.

@zhengkai
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhengkai/92651a8c3353a89111a3 to your computer and use it in GitHub Desktop.
Save zhengkai/92651a8c3353a89111a3 to your computer and use it in GitHub Desktop.
example for Backup files by Amazon Glacier
#! /usr/bin/env php
<?php
/*
* example for Backup files by Amazon Glacier
*
* composer.json:
* {
* "require": {
* "aws/aws-sdk-php": "2.6.5"
* }
* }
*
* Zheng Kai <zhengkai@gmail.com> 2014.6.6(D-Day)
*/
require_once __DIR__.'/vendor/autoload.php';
$sDirScan = '/etc/';
$aConfig = array(
'key' => 'xxxx',
'secret' => 'xxxx',
'region' => 'eu-west-1',
'vault' => 'xxxx',
);
use Aws\Glacier\GlacierClient;
$oClient = GlacierClient::factory(array(
'key' => $aConfig['key'],
'secret' => $aConfig['secret'],
'region' => $aConfig['region'],
));
$sFileArchive = __DIR__.'/archive.csv';
$sFileTmp = __DIR__.'/tmp.gz';
$hFile = @fopen($sFileArchive, 'r+b');
if (!$hFile) {
echo 'can not create file, exit';
exit;
}
$iBlock = 0;
flock($hFile, LOCK_EX | LOCK_NB, $iBlock);
if ($iBlock) {
echo 'blocked, exit';
exit;
}
chdir($sDirScan);
$lFile = scandir($sDirScan);
$lFile = array_filter($lFile, function ($sFile) use ($sDirScan) {
if (substr($sFile, 0, 1) === '.') {
return FALSE;
}
if (!is_file($sDirScan.$sFile)) {
return FALSE;
}
return TRUE;
});
$lFile = array_values($lFile);
echo 'file scaned = '.implode(' ', $lFile), "\n";
echo 'start while', "\n";
while (!feof($hFile)) {
$sLine = fgets($hFile, 1000);
if (!$sLine) {
continue;
}
$aMatch = array();
if (!preg_match('#^(.+?),#', $sLine, $aMatch)) {
continue;
}
$iKey = array_search($aMatch[1], $lFile);
if ($iKey === FALSE) {
continue;
}
unset($lFile[$iKey]);
}
echo "\n";
echo 'file to write = '.implode(' ', $lFile), "\n";
foreach ($lFile as $sFile) {
echo "\n", $sFile, "\n";
if (file_exists($sFileTmp)) {
unlink($sFileTmp);
}
if (file_exists($sFileTmp)) {
echo 'can not remove temp file, exit';
exit;
}
$iSize = filesize($sFile);
$iSizeTmp = 0;
if ($iSize < 10) {
echo 'filesize too small, skip', "\n";
$sKey = 'small';
} else {
$sFilePath = $sDirScan.$sFile;
$sCmd = sprintf('gzip -q -c --best %s 2>/dev/null > %s', escapeshellarg($sFilePath), escapeshellarg($sFileTmp));
exec($sCmd);
if (!file_exists($sFileTmp) || ($iSizeTmp = filesize($sFileTmp)) < 5) {
echo 'can not create temp file, skip', "\n";
continue;
}
$sKey = sha1_file($sFileTmp);
$result = $client->uploadArchive(array(
'vaultName' => $aConfig['vault'],
'archiveDescription' => $sFile,
'body' => fopen($sFileTmp, 'rb'),
));
if ($result) {
$sKey = $result->get('archiveId');
} else {
$sKey = 'failed';
}
unlink($sFileTmp);
}
$iLineCount = intval(exec('wc -l '.escapeshellarg($sFile).' 2>/dev/null'));
$sLine = sprintf('%s,%d,%d,%d,%s'."\n", $sFile, $iLineCount, $iSize, $iSizeTmp, $sKey);
$sFormat = '%-10s : %s'."\n";
printf($sFormat, 'line', $iLineCount);
printf($sFormat, 'filesize', $iSize);
printf($sFormat, 'gzipsize', $iSizeTmp);
printf($sFormat, 'archiveid', $sKey);
fwrite($hFile, $sLine, 10240);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment