Skip to main content
Topic: Little help with script needed (Read 5803 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Little help with script needed

Hi,

I found a script online which automatically saves my databases and stores them for two weeks, started every night with a cronjob. Files older then that inside the folder will be automatically deleted. Now the script deletes itself, 'cause the script file is older then two weeks! lol

Sure there is a way to prevent the script file from deleted, make an exception inside the code. This is the script I am using:

Code: [Select]
<?php

######## Einstellungen DB-Backup #############################################

$db_name = "d01b41f9";
$db_passwd = "xxx";

$sql_file = "dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";

####################################################################

exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $sql_file");
exec("gzip $sql_file");

$datei = $sql_file . ".gz";
$link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$link = str_replace(basename(__FILE__),$datei,$link);
echo "Downloadlink: <a href='" . $link . "'>" . $datei . "</a><br>";

######## Dateien löschen, die älter als 14 Tage sind #############################################

    /*
    * Created on 30.01.2010 by Nico Schubert
    */
    $dir =
        $_SERVER["DOCUMENT_ROOT"].dirname($_SERVER['PH
        P_SELF']).'/';
    $folder = dir($dir);
    while ($dateiname = $folder->read()) {
        if (filetype($dir.$dateiname) != "dir") {
            if (strtotime("-2 weeks") >
                @filemtime($dir.$dateiname)) {
                if (@unlink($dir.$dateiname) != false)
                echo $dateiname.' wurde gelöscht<br>';
                else
                echo $dateiname.' konnte nicht
                    gelöscht werden<br>';
                }
        }
    }
    echo "Suchen nach zu löschenden Dateien... Fertig";
    $folder->close();
    exit;

?>

Would be great if someone could help me out and tell me where I can change the code so the script itself wouldn't be deleted anymore.

Re: Little help with script needed

Reply #1

I am not sure whether this will work as I haven't tested it yet.

Code: [Select]
<?php

######## Einstellungen DB-Backup #############################################

$db_name = "d01b41f9";
$db_passwd = "xxx";

$sql_file = "dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";

####################################################################

exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $sql_file");
exec("gzip $sql_file");

$datei = $sql_file . ".gz";
$link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$link = str_replace(basename(__FILE__),$datei,$link);
echo "Downloadlink: <a href='" . $link . "'>" . $datei . "</a><br>";

######## Dateien löschen, die älter als 14 Tage sind #############################################

    /*
    * Created on 30.01.2010 by Nico Schubert
    */
    $dir =
        $_SERVER["DOCUMENT_ROOT"].dirname($_SERVER['PH
        P_SELF']).'/';
    $folder = dir($dir);
    while ($dateiname = $folder->read()) {
        if (filetype($dir.$dateiname) != "dir"
&& pathinfo($dir.$dateiname, PATHINFO_EXTENSION) != "php")
{
            if (strtotime("-2 weeks") >
                @filemtime($dir.$dateiname)) {
                if (@unlink($dir.$dateiname) != false)
                echo $dateiname.' wurde gelöscht<br>';
                else
                echo $dateiname.' konnte nicht
                    gelöscht werden<br>';
                }
        }
    }
    echo "Suchen nach zu löschenden Dateien... Fertig";
    $folder->close();
    exit;

?>

Basically I just added: && pathinfo($dir.$dateiname, PATHINFO_EXTENSION) != "php" exactly after: if (filetype($dir.$dateiname) != "dir". I think you can also use: && pathinfo($dir.$dateiname, PATHINFO_EXTENSION) === "gz", if gz is the extension of file(s) to be deleted (as it will later be filtered by number of weeks set).

Re: Little help with script needed

Reply #2

Thank you, I will try it!

Re: Little help with script needed

Reply #3

Do note: judging from the first few lines, it looks like the script is set up to save the database backup on a "web-accessible" place (based on the fact it provides a download link).
Keep in mind that having the database backed up in a web-accessible location is a security risk for both you and your users.

That said, if you put the script in a directory that is not accessible from an http url, then you are fine.
Bugs creator.
Features destroyer.
Template killer.

Re: Little help with script needed

Reply #4

It isn't. I changed the line to:

Code: [Select]
echo "Datei erstellt: " . $datei . "<br>";

Thanks for the hint.

Re: Little help with script needed

Reply #5

That just doesn't explicitly give out the url, but if you have the php file in the same place as for example the index.php of your forum, the result is still kinda the same, because the file name is not random at all and with a bit of luck can be guessed. ;)
In most hosting you have the "root" of your space, then you have a "public" or "http" or something similar directory that is the only one accessible from "the outside", so:
Code: [Select]
\
 |-\log
 |-\http
       |-\forum
etc.
anything under "http" can be read via an URL, instead things in other locations (e.g. "log") cannot. If you create a directory "backups" like this:
Code: [Select]
\
 |-\backups
 |-\log
 |-\http
       |-\forum
etc.
and put the script there, then you are save, otherwise you may not be.
Bugs creator.
Features destroyer.
Template killer.

Re: Little help with script needed

Reply #6

Sorry for the offtopic , but if you have ssh access you can achieve this with mysqlbackup tool in a more quick and safer way . 
sorry for my bad english

Re: Little help with script needed

Reply #7

Mysqlbackup using ssh is also useful but php script should be safe subject to the practise suggested by emanuele above.

Re: Little help with script needed

Reply #8

Thank you all, but I need more help.  :-[

At my webspace I can set domains to all my folders. I have no HTTP folder as mentioned by @emanuele. What I can do, so the support told me via email, is to save the backup files in the absolute server path /www/htdocs/(my-FTP-login)/(path-on-FTP). The backup script must be reachable with HTTP because the cronjob must find the script, so I think I must save it in a folder reachable with a (sub)domain, right? In the cronjob settings must be a path to the script defined with HTTP or HTTPS.

So for the script to work, I must change it to save the files here: /www/htdocs/(my-FTP-login)/(path-on-FTP). I don't know how.  :-[  I wish there would me a much more safer way as for in the script the database name and password is shown!




Re: Little help with script needed

Reply #9

No. The backup script can be hidden. You can run your cronjob as follows:
Code: [Select]
* * * * * sshusername php -q /yourhiddenpathfromhttp/yourscript.php

Change the scheduled time (*****) and your sshusername  accordingly.

Re: Little help with script needed

Reply #10

Hm... I don't think you understand. This is the settings page for the cronjob. There is no command line, only path to the php file and the time the script will be started.

Re: Little help with script needed

Reply #11

Noted. I guess that is your issue with your hosting. If you are using VPS, you should be able to do beyond that.

Re: Little help with script needed

Reply #12

VPS? Not that I know.  ;)  It's just hosted webspace and databases.

Re: Little help with script needed

Reply #13

Put your absolute path in:
Code: [Select]
    $dir =
        $_SERVER["DOCUMENT_ROOT"].dirname($_SERVER['PH
        P_SELF']).'/';
something like:
Code: [Select]
$dir = '/www/htdocs/(my-FTP-login)/(path-on-FTP)';
should do.
Bugs creator.
Features destroyer.
Template killer.

Re: Little help with script needed

Reply #14

Doesn't work. The backup file is saved where the script is. I tried:

Code: [Select]
	$dir = '/www/htdocs/(ftp-login-name)/backup/';

Edit: The older files in the backup folder were deleted, this seems to work. But the new backup files are saved in the wrong directory. So we must tell the backup script to create the files in another folder. Could it be here?

Code: [Select]
$sql_file = "dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";