MySQL database back-uppen met PHP -> Cronjob

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Pagina: « vorige 1 2

- Ariën  -
Beheerder

- Ariën -

11/10/2011 12:18:16
Quote Anchor link
try?

Ik heb het helaas nog niet kunnen uitproberen vanwege een bokkende server :-/
Gewijzigd op 11/10/2011 12:18:42 door - Ariën -
 
PHP hulp

PHP hulp

28/11/2024 04:58:05
 
Tom C

Tom C

11/10/2011 12:51:21
Quote Anchor link
Werkt ook al niet -.-

Edit: oh wacht, foutje. Even opnieuw proberen.

Edit: Weer foutje -.-

Edit: OK, het werkt echt niet.
Gewijzigd op 11/10/2011 12:54:21 door Tom C
 
- Ariën  -
Beheerder

- Ariën -

11/10/2011 13:06:43
Quote Anchor link
je krijgt ook geen ticket o.i.d in DA?
De aangemaakte backups, vind je in de backupfolder, voor je public_html
Gewijzigd op 11/10/2011 13:08:53 door - Ariën -
 
Tom C

Tom C

11/10/2011 13:08:31
Quote Anchor link
Nope.

Edit: Wacht ik heb 't werkend met een nieuw script:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/local/bin/php -n
<?php

//User backup script, allowing DA Users to create backups via a cronjob.

/*******************************
**  Set your variables here
*******************************/


$username = "<username>";
$password = "<password>";

// you must specify one of your domain names. (Any one of them will be fine)
$domain = "domain.com";

// These usually dont require modification
// if you use https to connect to DA, set $ssl = 1;

$host = "localhost";
$ssl = 0;
$port = 2222;

/*******************************
**  Install Instructions
*******************************

1) Place this file somewhere under your home directory.
A good place might be:
/home/username/backup.php

From the File Manager in DA, that would be:
/backup.php

2) Chmod the file to 700.  This is very important for security.

3) Edit the above variables.
Most likely, you''ll only need to change the username and password.
If your system uses ssl to connect to DA (https) then you'll need to set $ssl = 1;

4) Create the cronjob to execute:

/home/username/backup.php

as often as you wish. Usually once per day is the most you'll want to run it.
To create the cronjob, see:
http://www.site-helper.com/misc.html#cron

Usually a very early morning hour, say 5am is best as the load is usually lowest then.

Note that we do not need to add /usr/local/bin/php in front of this php script because of the
#!/usr/local/bin/php -n
line at the very top.

5) check your 'username' account.  Any emails will be delivered there if it doesn't work.
If needed, you can test manually via ssh by running:

/home/username/backup.php



6) Optional:  If you wish to have an ftp upload along with the backup, create a
cronjob to run about an hour later than the backup, and use the command:

/usr/bin/ncftpput -f /home/username/ftp.conf -V -t 25 -m "/ftp/path/" "/home/username/backups/backup-`date +%b-%e-%Y`-1.tar.gz"

and in the ftp.conf, put:

host 1.2.3.4
user ftpuser
pass ftppass

Then chmod the ftp.conf to 700.
Note this assumes you've only got 1 bacukp per day since it uses -1.tar.gz and doesn't take into account possible -2.tar.gz, etc.

********************************
*******************************/


$sock = newSock();
$c=0;
$sock->query('/CMD_API_SITE_BACKUP',
    array(
        'action' => 'backup',
        'domain' => $domain,
        'select'.$c++ => 'domain',
        'select'.$c++ => 'subdomain',
        'select'.$c++ => 'email',
        'select'.$c++ => 'forwarder',
        'select'.$c++ => 'autoresponder',
        'select'.$c++ => 'vacation',
        'select'.$c++ => 'list',
        'select'.$c++ => 'emailsettings',
        'select'.$c++ => 'ftp',
        'select'.$c++ => 'ftpsettings',
        'select'.$c++ => 'database',
    ));


$result = $sock->fetch_parsed_body();

if ( $result['error'] != "0" )
{

    echo "Error issuing backup creation: ".$result['details']."\n";
    exit(1);
}


//all should be fine.
exit(0);

function
newSock()
{

        global $host;
        global $port;
        global $ssl;
        global $username;
    global $password;

        $tsock = new HTTPSocket;
        $tsock->set_method('POST');
        $tsock->set_login($username, $password);

        if ($ssl)
        {

                $tsock->connect("ssl://$host", $port);
        }

        else
        {
                $tsock->connect("$host", $port);
        }


        return $tsock;
}






















/**
 * Socket communication class.
 *
 * Originally designed for use with DirectAdmin's API, this class will fill any HTTP socket need.
 *
 * Very, very basic usage:
 *   $Socket = new HTTPSocket;
 *   echo $Socket->get('http://user:[email protected]/somedir/some.file?query=string&this=that');
 *
 * @author Phi1 'l0rdphi1' Stier <[email protected]>
 * @package HTTPSocket
 * @version 2.6
 */

class HTTPSocket {

    var
$version = '2.6';
    
    /* all vars are private except $error, $query_cache, and $doFollowLocationHeader */

    var $method = 'GET';

    var
$remote_host;
    var
$remote_port;
    var
$remote_uname;
    var
$remote_passwd;

    var
$result;
    var
$result_header;
    var
$result_body;
    var
$result_status_code;

    var
$lastTransferSpeed;

    var
$bind_host;

    var
$error = array();
    var
$warn = array();
    var
$query_cache = array();

    var
$doFollowLocationHeader = TRUE;
    var
$redirectURL;

    var
$extra_headers = array();

    /**
     * Create server "connection".
     *
     */

    function connect($host, $port = '' )
    {

        if (!is_numeric($port))
        {

            $port = 80;
        }


        $this->remote_host = $host;
        $this->remote_port = $port;
    }

    function
bind( $ip = '' )
    {

        if ( $ip == '' )
        {

            $ip = $_SERVER['SERVER_ADDR'];
        }


        $this->bind_host = $ip;
    }


    /**
     * Change the method being used to communicate.
     *
     * @param string|null request method. supports GET, POST, and HEAD. default is GET
     */

    function set_method( $method = 'GET' )
    {

        $this->method = strtoupper($method);
    }


    /**
     * Specify a username and password.
     *
     * @param string|null username. defualt is null
     * @param string|null password. defualt is null
     */

    function set_login( $uname = '', $passwd = '' )
    {

        if ( strlen($uname) > 0 )
        {

            $this->remote_uname = $uname;
        }


        if ( strlen($passwd) > 0 )
        {

            $this->remote_passwd = $passwd;
        }

    }


    /**
     * Query the server
     *
     * @param string containing properly formatted server API. See DA API docs and examples. Http:// URLs O.K. too.
     * @param string|array query to pass to url
     * @param int if connection KB/s drops below value here, will drop connection
     */

    function query( $request, $content = '', $doSpeedCheck = 0 )
    {

        $this->error = $this->warn = array();
        $this->result_status_code = NULL;

        // is our request a http:// ... ?
        if (preg_match('!^http://!i',$request))
        {

            $location = parse_url($request);
            $this->connect($location['host'],$location['port']);
            $this->set_login($location['user'],$location['pass']);
            
            $request = $location['path'];
            $content = $location['query'];

            if ( strlen($request) < 1 )
            {

                $request = '/';
            }

        }


        $array_headers = array(
            'User-Agent' => "HTTPSocket/$this->version",
            'Host' => ( $this->remote_port == 80 ? $this->remote_host : "$this->remote_host:$this->remote_port" ),
            'Accept' => '*/*',
            'Connection' => 'Close' );

        foreach ( $this->extra_headers as $key => $value )
        {

            $array_headers[$key] = $value;
        }


        $this->result = $this->result_header = $this->result_body = '';

        // was content sent as an array? if so, turn it into a string
        if (is_array($content))
        {

            $pairs = array();

            foreach ( $content as $key => $value )
            {

                $pairs[] = "$key=".urlencode($value);
            }


            $content = join('&',$pairs);
            unset($pairs);
        }


        $OK = TRUE;

        // instance connection
        if ($this->bind_host)
        {

            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_bind($socket,$this->bind_host);

            if (!@socket_connect($socket,$this->remote_host,$this->remote_port))
            {

                $OK = FALSE;
            }

        }

        else
        {
            $socket = @fsockopen( $this->remote_host, $this->remote_port, $sock_errno, $sock_errstr, 10 );
        }


        if ( !$socket || !$OK )
        {

            $this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port.";
            return 0;
        }


        // if we have a username and password, add the header
        if ( isset($this->remote_uname) && isset($this->remote_passwd) )
        {

            $array_headers['Authorization'] = 'Basic '.base64_encode("$this->remote_uname:$this->remote_passwd");
        }


        // for DA skins: if $this->remote_passwd is NULL, try to use the login key system
        if ( isset($this->remote_uname) && $this->remote_passwd == NULL )
        {

            $array_headers['Cookie'] = "session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}";
        }


        // if method is POST, add content length & type headers
        if ( $this->method == 'POST' )
        {

            $array_headers['Content-type'] = 'application/x-www-form-urlencoded';
            $array_headers['Content-length'] = strlen($content);
        }

        // else method is GET or HEAD. we don't support anything else right now.
        else
        {
            if ($content)
            {

                $request .= "?$content";
            }
        }


        // prepare query
        $query = "$this->method $request HTTP/1.0\r\n";
        foreach ( $array_headers as $key => $value )
        {

            $query .= "$key: $value\r\n";
        }

        $query .= "\r\n";

        // if POST we need to append our content
        if ( $this->method == 'POST' && $content )
        {

            $query .= "$content\r\n\r\n";
        }


        // query connection
        if ($this->bind_host)
        {

            socket_write($socket,$query);

            // now load results
            while ( $out = socket_read($socket,2048) )
            {

                $this->result .= $out;
            }
        }

        else
        {
            fwrite( $socket, $query, strlen($query) );

            // now load results
            $this->lastTransferSpeed = 0;
            $status = socket_get_status($socket);
            $startTime = time();
            $length = 0;
            $prevSecond = 0;

            $readsize=1024;

            while ( !feof($socket) && !$status['timed_out'] )
            {

                $chunk = fgets($socket, $readsize);
                $length += strlen($chunk);
                $this->result .= $chunk;

                $elapsedTime = time() - $startTime;

                if ( $elapsedTime > 0 )
                {

                    $this->lastTransferSpeed = ($length/$readsize)/$elapsedTime;
                }


                if ( $doSpeedCheck > 0 && $elapsedTime > 5 && $this->lastTransferSpeed < $doSpeedCheck )
                {

                    $this->warn[] = "kB/s for last 5 seconds is below 50 kB/s (~".( ($length/$readsize)/$elapsedTime )."), dropping connection...";
                    $this->result_status_code = 503;
                    break;
                }

            }

            
            if ( $this->lastTransferSpeed == 0 )
            {

                $this->lastTransferSpeed = $length/$readsize;
            }

        }

        
        list($this->result_header,$this->result_body) = split("\r\n\r\n",$this->result,2);

        if ($this->bind_host)
        {

            socket_close($socket);
        }

        else
        {
            fclose($socket);
        }


        $this->query_cache[] = $query;


        $headers = $this->fetch_header();

        // what return status did we get?
        if (!$this->result_status_code)
        {

            preg_match("#HTTP/1\.. (\d+)#",$headers[0],$matches);
            $this->result_status_code = $matches[1];
        }


        // did we get the full file?
        if ( !empty($headers['content-length']) && $headers['content-length'] != strlen($this->result_body) )
        {

            $this->result_status_code = 206;
        }


        // now, if we're being passed a location header, should we follow it?
        if ($this->doFollowLocationHeader)
        {

            if ($headers['location'])
            {

                $this->redirectURL = $headers['location'];
                $this->query($headers['location']);
            }
        }
        
    }

    function
getTransferSpeed()
    {

        return $this->lastTransferSpeed;
    }


    /**
     * The quick way to get a URL's content :)
     *
     * @param string URL
     * @param boolean return as array? (like PHP's file() command)
     * @return string result body
     */

    function get($location, $asArray = FALSE )
    {

        $this->query($location);

        if ( $this->get_status_code() == 200 )
        {

            if ($asArray)
            {

                return split("\n",$this->fetch_body());
            }


            return $this->fetch_body();
        }


        return FALSE;
    }


    /**
     * Returns the last status code.
     * 200 = OK;
     * 403 = FORBIDDEN;
     * etc.
     *
     * @return int status code
     */

    function get_status_code()
    {

        return $this->result_status_code;
    }


    /**
     * Adds a header, sent with the next query.
     *
     * @param string header name
     * @param string header value
     */

    function add_header($key,$value)
    {

        $this->extra_headers[$key] = $value;
    }


    /**
     * Clears any extra headers.
     *
     */

    function clear_headers()
    {

        $this->extra_headers = array();
    }


    /**
     * Return the result of a query.
     *
     * @return string result
     */

    function fetch_result()
    {

        return $this->result;
    }


    /**
     * Return the header of result (stuff before body).
     *
     * @param string (optional) header to return
     * @return array result header
     */

    function fetch_header( $header = '' )
    {

        $array_headers = split("\r\n",$this->result_header);
        
        $array_return = array( 0 => $array_headers[0] );
        unset($array_headers[0]);

        foreach ( $array_headers as $pair )
        {

            list($key,$value) = split(": ",$pair,2);
            $array_return[strtolower($key)] = $value;
        }


        if ( $header != '' )
        {

            return $array_return[strtolower($header)];
        }


        return $array_return;
    }


    /**
     * Return the body of result (stuff after header).
     *
     * @return string result body
     */

    function fetch_body()
    {

        return $this->result_body;
    }


    /**
     * Return parsed body in array format.
     *
     * @return array result parsed
     */

    function fetch_parsed_body()
    {

        parse_str($this->result_body,$x);
        return $x;
    }

}


?>


Bron: files.directadmin.com

Toevoeging op 11/10/2011 13:15:15:

Toch bedankt voor de hulp!
Gewijzigd op 11/10/2011 13:15:01 door Tom C
 
- Ariën  -
Beheerder

- Ariën -

11/10/2011 13:15:45
Quote Anchor link
Zou kunnen dat fopen uit staat in PHP? En dat de CLI het wel toestaat?

Anyway, graag gedaan, en success ermee.
Gewijzigd op 11/10/2011 13:16:25 door - Ariën -
 
Tom C

Tom C

11/10/2011 13:25:38
Quote Anchor link
- Aar - op 11/10/2011 13:15:45:
Zou kunnen dat fopen uit staat in PHP? En dat de CLI het wel toestaat?

Anyway, graag gedaan, en success ermee.

Ik heb geen idee..

Ik heb het script in ieder geval hier geplaatst indien andere mensen er ook naar op zoek zijn.
 

Pagina: « vorige 1 2



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.