ElkArte Community

General => Chit Chat => Topic started by: Jorin on November 08, 2016, 01:00:30 am

Title: Little help with script needed
Post by: Jorin on November 08, 2016, 01:00:30 am
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.
Title: Re: Little help with script needed
Post by: ahrasis on November 08, 2016, 01:33:32 am
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).
Title: Re: Little help with script needed
Post by: Jorin on November 08, 2016, 05:06:46 am
Thank you, I will try it!
Title: Re: Little help with script needed
Post by: emanuele on November 08, 2016, 07:40:22 am
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.
Title: Re: Little help with script needed
Post by: Jorin on November 08, 2016, 07:50:33 am
It isn't. I changed the line to:

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

Thanks for the hint.
Title: Re: Little help with script needed
Post by: emanuele on November 08, 2016, 09:43:49 am
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.
Title: Re: Little help with script needed
Post by: radu81 on November 08, 2016, 03:23:41 pm
Sorry for the offtopic , but if you have ssh access you can achieve this with mysqlbackup tool in a more quick and safer way . 
Title: Re: Little help with script needed
Post by: ahrasis on November 08, 2016, 07:14:33 pm
Mysqlbackup using ssh is also useful but php script should be safe subject to the practise suggested by emanuele above.
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 01:55:54 am
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!



Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 02:25:04 am
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.
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 02:32:54 am
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.
Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 02:42:43 am
Noted. I guess that is your issue with your hosting. If you are using VPS, you should be able to do beyond that.
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 03:01:49 am
VPS? Not that I know.  ;)  It's just hosted webspace and databases.
Title: Re: Little help with script needed
Post by: emanuele on November 09, 2016, 03:10:40 am
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.
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 03:57:39 am
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";

Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 06:02:56 am
What about changing to something like this:
Code: [Select]
> /backupfolderpath/$sql_file");
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 06:40:48 am
Which line please? There are a few $sql_file in the script...

Sorry, I am an absolute noob with PHP. I thought the line to change must be:

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

to something like:

Code: [Select]
$sql_file = "/www/htdocs/(my-FTP-login)/(path-on-FTP)/dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";
Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 06:49:03 am
At the end of this one:
Code: [Select]
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $sql_file");
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 06:58:50 am
Okay, but now it isn't a .gz file anymore, the backup (in the right folder, yippie!) is an SQL file.  :o
Title: Re: Little help with script needed
Post by: emanuele on November 09, 2016, 07:16:32 am
Code: [Select]
<?php

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

$db_name = "d01b41f9";
$db_passwd = "FUkRXMwV23R5qk5L";
$destination = '/absolute/path/to/a/safe/location/'; // Do not forget the leading /

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

####################################################################
$output = $destination . $sql_file;
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $output");
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 = $destination;
    $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;

?>
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 07:56:24 am
Thank you, Emanuele. Your script worked, but....

The backup is again an uncompressed SQL file. Why that?  :o
Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 09:03:35 am
That's because it is a similar solution to mine but longer one with minor mistakes I think (this include my earlier suggestion as well). @emanuele changed the variable to $output but forget to change the same variable $sql_file to $output below it. This is because the file is in other path now and need the same to be defined for the one below. I think.
Title: Re: Little help with script needed
Post by: Jorin on November 09, 2016, 09:10:17 am
I understand not a word. I think.  :D
Title: Re: Little help with script needed
Post by: ahrasis on November 09, 2016, 11:07:02 am
I mean what about changing the latter $sql_file to $output:
Code: [Select]
exec("gzip $sql_file");

$datei = $sql_file . ".gz";
Otherwise the code is looking for the sql file and gzipped sql file at the script location and not in the set location? I think.
Title: Re: Little help with script needed
Post by: Jorin on November 10, 2016, 02:51:57 am
Now the backup file has the .gz ending, but it is not packed (203 MB file size, while packed backup files are 36 MB).  :o

I tried this line:

Code: [Select]
exec("gzip $destination . $sql_file");

...so the gzip procedure will find the file in the other location, but it didn't work. File size is still 203 MB.

Code is now:

Code: [Select]
$destination = '/www/htdocs/xxx/backup/'; // Do not forget the leading /

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

####################################################################
$output = $destination . $sql_file . ".gz";
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $output");
exec("gzip $destination . $sql_file");
Title: Re: Little help with script needed
Post by: emanuele on November 10, 2016, 04:46:07 am
Because interpretation changes the meaning of things. :P

Code: [Select]
<?php

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

$db_name = "d01b41f9";
$db_passwd = "FUkRXMwV23R5qk5L";
$destination = '/absolute/path/to/a/safe/location/'; // Do not forget the leading /

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

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

echo "Backup created<br>";

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

    /*
    * Created on 30.01.2010 by Nico Schubert
    */
    $dir = $destination;
    $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;

?>
Title: Re: Little help with script needed
Post by: Jorin on November 10, 2016, 05:02:59 am
Great! This code worked! And you know what? I know what the solution was! Seems logical now that I see it. I think!  :D

Thank you both, @emanuele and @ahrasis, for your help with this stupid thing!  O:-)
Title: Re: Little help with script needed
Post by: emanuele on November 10, 2016, 08:01:32 am
yw! :)