Scripts

Backup Website via FTP

Met dit script kan je een complete website (Folder) back-uppen naar een externe ftpserver. Je kan dit script handig uitvoeren door onderstaande cron op je server in te stellen. 00 21 * * * php /var/www/backup/domein.nl/backup.php Zorg er voor dat de ftp gebruiker rechten heeft om mappen aan te maken en bestanden te overschrijven. http://www.dnssniffer.com

backup-website-via-ftp
<?
//Backup Script
//
// Copyright DNSSniffer.com

//Settings Begin -- You can edit the values below --
//Folder you want to backup.
$localpath = "/var/www/domein.nl/";
//Folder where you placed this file.
$backuppath = "/var/www/backup/domein.nl/";
//Folder on the ftp server.
$ftppath = "domein.nl";
//Ftp hostname
$ftp_server = "ftp.jouwftpserver.nl";
//Ftp username
$ftp_user_name = "gebruikersnaam";
//Ftp Password
$ftp_user_pass = "wachtwoord";
//Settings End -- Do not edit the settings below --

//Global Settings --Do Not Edit--
$fullday = date("l");
$dayofmonth = date("d");
$date = date("m-d-Y");
$dailydstfile = "$ftppath/$fullday/$fullday.tgz";
$dailysrcfile = "$backuppath$fullday.tgz";
$monthdstfile = "$ftppath/Monthly/$date.tgz";
$monthsrcfile = "$backuppath$fullday.tgz";

//Tar the the folder contents
exec("tar czPf $fullday.tgz $localpath");

//Open the connection
$conn_id = ftp_connect($ftp_server);

//Login in the ftp server
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

//Check the connection
if ((!$conn_id) || (!$login_result)) {
            echo"FAIL. Connection Error \n";
        exit;
    } else {
            echo"OK. Connected to $ftp_server \n";
}

//Check The Main Directory
$contentsfolder = ftp_nlist($conn_id, ".");
if (in_array("./$ftppath", $contentsfolder)) {
    echo "OK. Directory $ftppath exists. \n";
} else {
if (ftp_mkdir($conn_id, $ftppath)) {
 echo "OK. Created the directory $ftppath \n";
} else {
 echo "Fail. Error creating the directory $ftppath \n";
}
}

//Check The Day Directory
$contentsday = ftp_nlist($conn_id, "./$ftppath");
if (in_array("./$ftppath/$fullday", $contentsday)) {
    echo "OK. Directory $fullday exists. \n";
} else {
if (ftp_mkdir($conn_id, "$ftppath/$fullday")) {
 echo "OK. Created the directory $fullday \n";
} else {
 echo "Fail. Error creating the directory $fullday \n";
}
}

//Start uploading the file
$upload = ftp_put($conn_id, $dailydstfile, $dailysrcfile, FTP_BINARY);

//Check if the file is uploaded
if (!$upload) {
        echo"FAIL. Error uploading file \n";
    } else {
        echo"OK. File Uploaded \n";
}

if ($dayofmonth == "01") {

echo "OK. It's THAT time of the month. (Starting Monthly Backup) \n";
   
//Check The month Directory
$contentsmonth = ftp_nlist($conn_id, "./$ftppath");
if (in_array("./$ftppath/Monthly", $contentsmonth)) {
    echo "OK. Directory Monthly exists. \n";
} else {
if (ftp_mkdir($conn_id, "$ftppath/Monthly")) {
 echo "OK. Created the directory Monthly \n";
} else {
 echo "Fail. Error creating the directory Monthly \n";
}
}

//Start uploading the monthly backup
$monthlyupload = ftp_put($conn_id, $monthdstfile, $monthsrcfile, FTP_BINARY);

//Check if the file is uploaded
if (!$monthlyupload) {
        echo"FAIL. Error uploading the monthly backup \n";
    } else {
        echo"OK. Monthly backup Uploaded \n";
}

}

//Close the ftp connection
ftp_close($conn_id);

//Remove Temp File
exec("rm $backuppath$fullday.tgz ");

?>

Reacties

0
Nog geen reacties.