Copy-Assets-to-Disk (php)
De DigiWiki.
Version du 20 août 2013 à 01:44 par Djphil (discuter | contributions)
<?php //FILENAME: copy-assets-to-disk.php modified by ssm2017 (2013) // // DESCRIPTION: // This script copies the asset blob data from the asset table of a MySQL // database to a set of disk files based on the sha1 sum of the blob data. // // This script needs to be run from the directory used to run the OpenSim // instance (standalone mode) or OpenSim asset server (ie. grid mode).This // script will process up to 10,000 asset records at a time. The blob data // for assets will be stored in the directory specified by $ASSET_DIR. The // directory will be created if it does not already exist. // // Upon the completion of this script, a summary will be displayed showing // the number of asset records and bytes processed versus the number of // files created and bytes written to disk. The number of files saved may // be less than the number of asset records as there may be duplicate assets // in the asset table (ie. the same texture was uploaded more than once) and // it indicates how much space was saved by storing the asset blobs on disk. // // Created March 24, 2011 by Kevin Cozens. $ASSET_DIR = "/srv/assets/"; // START MySQL Details // $SRC_SERVER = "localhost"; $SRC_USER = "****"; $SRC_PSWD = "****"; $SRC_DB_NAME = "grid"; $SRC_ASSETS_TBL = "grid.assets"; $DEST_ASSETS_TBL = "assets.assets"; // Open the database $src_db = mysql_connect($SRC_SERVER, $SRC_USER, $SRC_PSWD) or die(mysql_error()); if (!$src_db) die(mysql_error()); $files_total = 0; $files_written = 0; $bytes_total = 0; $bytes_written = 0; //Loop through the assets table a given number of items at a time to //avoid trying to do a SQL query whose results won't fit in memory. $index = 0; $index_stop = 500000; $loop_inc = 10000; $loop = true; while ($loop) { $start = time(); //Fetch and display the selected news item $sql = "SELECT `name`, `description`, `assetType`, `local`, `temporary`, `data`, `id`, `create_time`, `access_time` FROM $SRC_ASSETS_TBL LIMIT $index,$loop_inc"; //print "\$sql: $sql\n"; $results = mysql_query($sql); if (!$results) { print mysql_error(); $loop = false; continue; } $loop = false; while ($row = mysql_fetch_row($results)) { $loop = true; $name = $row[0]; $description = $row[1]; $asset_type = $row[2]; $local = $row[3]; $temporary = $row[4]; $data = $row[5]; $uuid = $row[6]; $created_at = $row[7]; $updated_at = $row[8]; $hash = hash('sha256', $data); // check if already exist $row_exists = mysql_query("SELECT id FROM $DEST_ASSETS_TBL WHERE id='".$uuid."' LIMIT 1"); if (!mysql_fetch_row($row_exists)) { //insert asset record with sha256 hash of asset blob $sql2 = "INSERT INTO $DEST_ASSETS_TBL (`id`, `asset_type`, `sha256`, `name`, `description`, `local`, `temporary`, `base_dir`, `created_at`, `updated_at`) VALUES ('$uuid', $asset_type, '$hash', '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($description)."', $local, $temporary, '$ASSET_DIR', $created_at, $updated_at)"; //$sql = "UPDATE `$DEST_ASSETS_TBL` SET `sha256`='$hash' WHERE `id`='$uuid'"; print "\sql2: $sql2\n"; $results2 = mysql_query($sql2); if (!$results2) { print mysql_error(); $loop = false; continue; } } $folder = $ASSET_DIR . substr($hash, 0, 3) . "/" . substr($hash, 3, 3) . "/"; $fullname = $folder . $hash; if (file_exists($fullname)) $exists = "(already saved)"; else { $exists = ""; if (!file_exists($folder)) mkdir($folder, 0777, true); chown($folder, 'www-data'); chgrp($folder, 'www-data'); $fp = fopen($fullname, "wb"); fwrite($fp, $data); fclose($fp); $files_written++; $bytes_written += strlen($data); } chown($fullname, 'www-data'); chgrp($fullname, 'www-data'); $files_total++; $bytes_total += strlen($data); //print "$uuid: $fullname: " . strlen($data) . " $exists\n"; //print $files_total. "\n"; } mysql_free_result($results); $index += $loop_inc; echo 'time = '. ((time() - $start) / 60). "\n"; echo 'index = '. $index. "\n"; //if ($index >= $index_stop) { // $loop = false; //} } mysql_close($src_db); print "\nAssets written to disk: $files_written ($bytes_written bytes) out of $files_total ($bytes_total bytes)\n"; ?>