Skip to content

Instantly share code, notes, and snippets.

@cukabeka
Last active October 7, 2015 09:28
Show Gist options
  • Save cukabeka/daa53041ffc1c4ba2830 to your computer and use it in GitHub Desktop.
Save cukabeka/daa53041ffc1c4ba2830 to your computer and use it in GitHub Desktop.
CRONJOB Komplettbackup
<?php
// http://hilfe-center.1und1.de/hosting/scripte_datenbanken/datenbanken/5.html
global $REX;
$host = $REX['DB']['1']['HOST'];
$db = $REX['DB']['1']['NAME'];
$dbuser = $REX['DB']['1']['LOGIN'];
$dbpw = $REX['DB']['1']['PSW'];
// Befehl ausführen und in Zipfile speichern
system(sprintf(
'mysqldump --opt -h%s -u%s -p"%s" %s | gzip > %s/backup_db/_regular_dumpDB.sql.gz',
$host,
$dbuser,
$dbpw,
$db,
getenv('DOCUMENT_ROOT')
));
echo '+DONE';
?>
<?php
global $REX;
$host = $REX['DB']['1']['HOST'];
$db = $REX['DB']['1']['NAME'];
$dbuser = $REX['DB']['1']['LOGIN'];
$dbpw = $REX['DB']['1']['PSW'];
backup_tables($host,$dbuser,$dbpw,$db);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
global $REX;
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
error_reporting(0);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
error_reporting(1);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen($REX['INCLUDE_PATH'].'/_backup/db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
echo "gespeichert in".($REX['INCLUDE_PATH'].'/_backup/db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql');
}
?>
<?php
# http://www.fractalizer.ru/frpost_356/php-script-to-backup-all-databases/
//GRANT SELECT,SHOW DATABASES,SHOW VIEW,LOCK TABLES ON *.* TO "backupuser"@localhost INDENTIFIED BY "XXXXXXXXXX"
//Change options below to fine-tune your copy
global $REX;
$host = $REX['DB']['1']['HOST']; # "dbXX.1und1.de";
$db = $REX['DB']['1']['NAME']; # "dbXXXXXXXX";
$user = $REX['DB']['1']['LOGIN']; # "pXXXXXXX";
$password = $REX['DB']['1']['PSW']; # "XXXXXXX";
$backupPath = $REX['INCLUDE_PATH'].'/_db_backups/';
$maxdaysold = 7;
$dateStr = date("Y-m-d-h-i-s");
$databasesToSkip = array('mysql', 'information_schema', 'test');
$output = null;
$timeStarted = time();
mysql_connect($host, $user, $password) or die('Cannot connect to MySQL: '.mysql_error());
$sql = 'SHOW DATABASES';
$result = mysql_query($sql);
if(!$result) {
die('Error getting database list: '.mysql_error());
}
$databases = array();
while ($row = mysql_fetch_assoc($result)) {
$databases[] = $row['Database'];
}
foreach($databases as $database) {
if(in_array($database, $databasesToSkip)) {
continue;
}
exec("mysqldump --complete-insert --create-options --add-locks --disable-keys --extended-insert --quick --quote-names -u $user --password=$password $database|gzip --fast -c>{$backupPath}/$database.$dateStr.sql.gz", $output, $result);
if($result) {
echo("Error saving database $backupPath: $result <br>"); var_dump ($output);
}
}
//Deleting old backups
exec('find '.$backupPath.' -mtime '.$maxdaysold.' -name "*.sql.gz" -exec rm {} \\;', $output, $result);
if($result) {
echo("Error removing old database backups! <br>");
}
echo('Database backup succeeded. Time elapsed: '.(time() - $timeStarted).' sec.');
echo("\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment