Ich habe 2 Scripts getestet:
Das 1.
Code: Alles auswählen
<?php
system("/www/htdocs/web3/ -uUSER -pPasswort -h localhost Datenbankname | gzip > ".dirname(__FILE__)."/db_backup_files/dump.gz", $fp);
if ($fp==0) echo "Daten exportiert"; else echo "Es ist ein Fehler aufgetreten";
?>
Gut also ist alles positiv gelaufen, aber ich will das backup script in einen anderen ordner haben
also im ordner db_backup_script , abeeeeerrrrrrr dann müsste mein code so aussehen:
Code: Alles auswählen
<?php
system("/www/htdocs/web3/db_backup_script/ -uUSER -pPasswort -h localhost Datenbankname | gzip > ".dirname(__FILE__)."/db_backup_files/dump.gz", $fp);
if ($fp==0) echo "Daten exportiert"; else echo "Es ist ein Fehler aufgetreten";
?>
Was muss ich ändern damit es im /www/htdocs/web3/db_backup_files/ ordner gespeichert wird? Und achja wird die Datei auch überschrieben?
Also wird auch immer das neuste Backup gespeichert / und das alte überspeichert?
Sooooooooooooooo nun zum 2. Script:
Code: Alles auswählen
<?php
/***************************************************************************
* cron_dbbackup.php
* ------------------
*
*
***************************************************************************/
// Set DB Connection
$dbhost = "localhost";
$dbuser = "User";
$dbpasswd = "passwort";
$dbname = "datenbankname";
// Set backup path
$root_path = "/www/htdocs/web3/"; // Root-path of the webserver
$path = "db_backup_files"; // backup directory
// set variables for backup and delete
// Backup Script 1: Full Backup
$full_backup = 1; // do full backup
$full_backup_gz = 0; // make compression for full backup?
$full_backup_store = 2; // count of always stored full backups
// Backup Script 2: Single file Backup
$single_backup = 1; // do single file backup
$single_backup_gz = 0; // make compression for single file backup?
$single_backup_store = 2; // count of always stored single backups
// CHANGE NOTHING BELOW THIS LINE !
// Generate Date for filename
$date = date("ymd");
$full = '_full';
$datedir = $root_path . $path . "/" . $date;
echo "DB-Backup on Webspace<br><br>";
// Option output
if ($full_backup == '1')
{
echo "Full Backup aktivated!<br>";
if ($full_backup_gz == '1')
{
echo "Compression: yes<br>";
}
else
{
echo "Compression: no<br>";
}
echo "Number of stored Backups: " . $full_backup_store . "<br><br>";
}
if ($single_backup == '1')
{
echo "Single file Backup aktivated!<br>";
if ($single_backup_gz == '1')
{
echo "Compression: yes<br>";
}
else
{
echo "Compression: no<br>";
}
echo "Number of stored Backups: " . $single_backup_store . "<br><br>";
}
// Script 1:
if ($full_backup == '1')
{
if ($full_backup_gz == '1')
{
$GZIP1 = "| gzip";
$GZIP1gz = ".gz";
}
else
{
$GZIP1 = "";
$GZIP1gz = "";
}
system("mysqldump -u$dbuser -p$dbpasswd -h$dbhost $dbname | sed -e 's/^--/##/' -e '/^\//d' $GZIP1 > $root_path$path/$date$full.sql$GZIP1gz", $fp1);
echo "$date$full.sql$GZIP1gz ... ";
if ($fp1==0)
{
echo "<font color=green>saved\n\n</font><br><br><font color=green>Backup Script 1 - OK!</font><br><br>";
}
else
{
echo "<font color=red>ERROR\n\n</font><font color=red>Backup Script 1 - ERROR!</font><br><br>";
}
}
// Script 2:
if ($single_backup == "1")
{
if ($single_backup_gz == "1")
{
$GZIP2 = "| gzip";
$GZIP2gz = ".gz";
}
else
{
$GZIP2 = "";
$GZIP2gz = "";
}
$type = @filetype($datedir);
echo "<br>Erstellen des Ordners $datedir ... ";
if ( $type != 'dir' )
{
if ( false !== @mkdir("$datedir"))
{
echo "<font color=green>ok</font><br><br>";
}
else
{
echo "<font color=red>ERROR</font><br><br>"; die;
}
}
else
{
echo "<font color=green>Ordner existiert bereits!</font><br><br>";
}
MYSQL_CONNECT($dbhost, $dbuser, $dbpasswd) or die ( "<H3>Datenbankserver nicht erreichbar - Databaseserver not connected</H3>");
MYSQL_SELECT_DB($dbname) or die ( "<H3>Datenbank nicht vorhanden - Database not found</H3>");
$result = MYSQL_QUERY("SHOW TABLES");
$numrow = MYSQL_NUM_ROWS($result);
for($i = 0;$i < $numrow;$i++)
{
$table = MYSQL_RESULT($result,$i);
echo "$table ... ";
system("mysqldump -h$dbhost -u$dbuser -p$dbpasswd $dbname $table | sed -e 's/^--/##/' -e '/^\//d' $GZIP2 > $datedir/$table.sql$GZIP2gz", $fp2);
if ($fp2==0) echo "<font color=green>saved!\n\n</font>"; else echo "<font color=red>ERROR!\n\n</font>";
}
MYSQL_CLOSE();
if ($fp2==0) echo "<br><br><font color=green>Backup Script 2 - OK!</font><br><br>"; else echo "<br><br><font color=red>Backup Script 1 - ERROR!</font><br><br>";
}
// delete old backups
$count1 = $full_backup_store;
$count2 = $single_backup_store;
$fc = 0;
// part 1 : delete full backups
if ($count1 <> 0)
{
$fh = opendir($root_path . $path);
$filearray = array("");
$i = 0;
while (false !== ($file = readdir($fh)))
{
if ($file != ".htaccess" && $file != "." && $file != ".." && !is_dir($root_path . $path ."/" . $file))
{
$filearray[$i] = $file;
$i++;
}
}
closedir($fh);
rsort ($filearray);
$anzahl = count ($filearray);
if ($anzahl > $count1)
{
echo "<br><br>";
$n = $count1;
while ($n < $anzahl)
{
if (false !== @unlink ($root_path . $path ."/" . $filearray[$n]))
{
echo "$filearray[$n] ... ";echo "<font color=green>deleted\n\n</font>";
}
else
{
echo "$filearray[$n] ... ";echo "<font color=red>not deleted\n\n</font>";
}
$n++;
}
}
else
{
echo "No full backups to delete!";echo "<br><br>";
}
}
else if (($count1 == 0) && ($count2 <> 0))
{
echo "<br><br>No full backup deletion activated!";
}
// part 2 : delete single file backups
if ($count2 <> 0)
{
$dh = opendir($root_path . $path);
$dirarray = array("");
$i = 0;
while (false !== ($dir = readdir($dh)))
{
if ($dir != "." && $dir != ".." && is_dir($root_path . $path ."/" .$dir))
{
$dirarray[$i] = $dir;
$i++;
}
}
closedir($dh);
rsort ($dirarray);
$anzahl = count ($dirarray);
if ($anzahl > $count2)
{
echo "<br><br>";
$n = $count2;
while ($n < $anzahl)
{
$fh = opendir($root_path . $path . "/". $dirarray[$n]);
while (false !== ($file = readdir($fh)))
{
if ($file != "." && $file != "..")
{
if (false == @unlink ($root_path . $path ."/" . $dirarray[$n] . "/" . $file))
{
$fc = $fc + 1;
}
}
}
closedir($fh);
if ($fc > 0)
{
echo "files in directory $dirarray ... ";echo "<font color=red>not deleted\n\n</font>";
echo "directory $dirarray[$n] ... ";echo "<font color=red>not empty: cannot delete\n\n</font>";
}
else
{
echo "files in directory $dirarray[$n] ... ";echo "<font color=green>deleted\n\n</font>";
if (false !== @rmdir($root_path . $path . "/". $dirarray[$n]))
{
echo "directory $dirarray[$n] ... ";echo "<font color=green>deleted\n\n</font>";
}
else
{
echo "directory $dirarray[$n] ... ";echo "<font color=red>not deleted\n\n</font>";
}
}
$n++;
}
}
else
{
echo "No single file backups to delete!";
}
}
else if (($count1 <> 0) && ($count2 == 0))
{
echo "<br><br>No single file backup deletion activated!";
}
if (($count1==0) && ($count2==0))
{
echo "<br><br>No deletion activated!";
}
echo "<br><br>";
?>
</center>
</span></td>
</tr>
</table>
Gut ich habe es also so eingestellt das immer eine .sql datei erstellt wird und jede Tabelle von extra gespeichert wird.
Aber hier wieder die Frage: werden die alten Dateien auch überspeichert mit den neuen Dateien?
Und ich habe in den Backup Ordner mit einem htaccess schutz belegt.
Aber beim ausführen vom script kommt folgende Meldung:
.htpasswd ... deleted No single file backups to delete!
Toll.. und was ist jetzt? Hab ich nun einen pw schutz dort oder nicht......
Hoffe mir kann jemand helfen
mfg UserXY
Edit: Ist es möglich ein Script so zu konfigurieren damit es die BackUps auf einen anderen webspace raufladet?