Download Rackspace Cloud Files Containers (all of them) for archiving

curl -sS https://getcomposer.org/installer | php
php composer.phar require rackspace/php-opencloud

< ? php
/**
 *  "require": {
 *      "rackspace/php-opencloud": "dev-master"
 *  }
 */

ini_set('memory_limit', '2048M'); // size must be bigger than the biggest file
ini_set('max_execution_time', 0);

require 'vendor/autoload.php';

use OpenCloud\Rackspace;

// Instantiate a Rackspace client.
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
    'username' => 'mycloudusernamehere',
    'apiKey'   => 'myapikeyhere'
));

$objStore = $client->objectStoreService('cloudFiles', 'LON');
$savePath = __DIR__.'/archive/files/';

// get our containers
print("Containers:\n");
$containersList = $objStore->listContainers();

while($container = $containersList->Next()) {
    if( ! in_array($container->name, array('.CDN_ACCESS_LOGS', ''))) {
        printf("*** %s\n", $container->name);

        $containerDir = $savePath.$container->name.'/';
        if (!is_dir($containerDir)) {
            mkdir($containerDir, 0777, true);
        }

        $files = $container->ObjectList();
        while($o = $files->Next()) {
            $file_name = $o->getName();

            if (file_exists($containerDir . $file_name)) {
                echo '## '.$containerDir.$file_name.' already exists'."\n";
                continue;
            }

            // Get our object
            $file = $container->getObject($file_name);

            if (strpos($file->getName(), '') !== false) {
                continue;
            }

            $tempDir = $containerDir . dirname($file->getName()) . '/';
            if (!is_dir($tempDir)) {
                mkdir($tempDir, 0777, true);
            }

            if (file_put_contents($containerDir . $file_name, $file->getContent())) {
                printf("** %s - OK\n", $file->getName());
            } else {
                printf("** %s - KO\n", $file->getName());
            }
            unset($file);
        }
    }
}

The script can be executed like

php script.php 

php cloud-files-php-downloader2.php
Containers:
*** meine.org.cdn.container
** wp-content/themes/twentyfifteen/css/editor-style.css - OK
** wp-content/themes/twentyfifteen/css/ie.css - OK
** wp-content/themes/twentyfifteen/css/ie7.css - OK
** wp-content/themes/twentyfifteen/genericons/Genericons.ttf - OK

Leave a Reply

Your email address will not be published. Required fields are marked *