~crackerizer/cloudsync/trunk

« back to all changes in this revision

Viewing changes to cloudsync

  • Committer: Phinitnan Chanasabaeng
  • Date: 2013-12-09 09:26:46 UTC
  • Revision ID: phinitnan_c@xtony.us-20131209092646-542ma7z1pnxxzf81
Added sync feature

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * MAIN PROGRAM
20
20
*/
21
21
date_default_timezone_set("UTC");
22
 
$REVISION = "A13";
 
22
$REVISION = "B13";
23
23
$EXT = ".csync";
24
24
error_reporting(0);
25
25
 
36
36
$cmd ["cmd"] = "help";
37
37
$cmd ["mtime"] = 0; /* seconds */
38
38
$desc ["mtime"] = "- Last modified time to be backup in second";
39
 
//$cmd ["sync"] = false;
40
 
//$desc ["sync"] = "- During backup, sync files between input and output";
 
39
$cmd ["sync"] = false;
 
40
$desc ["sync"] = "- During operations, sync files from source to target";
41
41
$cmd ["exclude"] = array ();
42
42
$desc ["exclude"] = "- List of excluded directories separated by comma";
43
43
$cmd ["mode"] = "0700";
72
72
 
73
73
        case "--input" :
74
74
            $n ++;
75
 
            $cmd ["input"] = $argv [$n];
 
75
            $cmd ["input"] = rtrim($argv [$n], "/ \\");
76
76
            break;
77
 
            /*
78
 
             case "--sync":
 
77
 
 
78
        case "--sync":
79
79
            $cmd["sync"] = true;
80
80
            break;
81
 
            */
82
81
 
83
82
        case "--output" :
84
83
            $n ++;
85
 
            $cmd ["output"] = $argv [$n];
 
84
            $cmd ["output"] = rtrim($argv [$n], "/ \\");
86
85
            break;
87
86
 
88
87
        case "--permfile" :
133
132
 
134
133
$flog = null;
135
134
$start_run = time();
 
135
 
136
136
/* Execute command */
137
137
switch ($cmd ["cmd"]) {
138
138
    case "backup" :
149
149
        cs_header();
150
150
 
151
151
        cslog("########## Backup started ###########\n");
152
 
        cslog("* Read permfile...");
153
 
        $perm = csread_perm($cmd["permfile"]);
154
 
        cslog("Done\n");
155
 
 
156
 
        if(empty($perm))
157
 
            $perm = array();
158
 
 
159
 
        cs_backup ( $cmd ["input"], $perm);
 
152
        //cslog("* Read permfile...");
 
153
        //$perm = csread_perm($cmd["permfile"]);
 
154
        //cslog("Done\n");
 
155
 
 
156
        //if(empty($perm))
 
157
        $perm = array();
 
158
 
 
159
        cs_backup ($cmd["input"], $perm);
 
160
        csbackup_sync($perm);
160
161
 
161
162
        cslog("* Write permfile...");
162
163
        cswrite_perm($cmd["permfile"], $perm);
184
185
 
185
186
        cs_restore ( $cmd ["input"]);
186
187
        csrestore_perm($perm);
 
188
        csrestore_sync($perm);
187
189
        cslog("########## Restore ended ###########\n");
188
190
        break;
189
191
 
351
353
    if($stderr != "")
352
354
        cslog("cs_decompress - STDERR:\n$stderr\n");
353
355
}
354
 
function cs_backup($input_dir, &$perm) {
 
356
function cs_delete($in) {
 
357
    $proc_desc = array(
 
358
                    0 => array("pipe", "r"),
 
359
                    1 => array("pipe", "w"),
 
360
                    2 => array("pipe", "w")
 
361
    );
 
362
 
 
363
    $cmd = "rm -rf \"$in\"";
 
364
 
 
365
    $p = proc_open($cmd, $proc_desc, $pipe);
 
366
 
 
367
    $stdout = stream_get_contents($pipe[1]);
 
368
    $stderr = stream_get_contents($pipe[2]);
 
369
 
 
370
    fclose($pipe[0]);
 
371
    fclose($pipe[1]);
 
372
    fclose($pipe[2]);
 
373
    proc_close($p);
 
374
 
 
375
    if($stdout != "")
 
376
        cslog("cs_decompress - STDOUT:\n$stdout\n");
 
377
 
 
378
    if($stderr != "")
 
379
        cslog("cs_decompress - STDERR:\n$stderr\n");
 
380
}
 
381
function cs_backup($input, &$perm) {
355
382
    global $cmd;
356
383
    global $compress;
357
384
    global $EXT;
358
385
 
359
 
    $input_dir = rtrim($input_dir, "/")."/";
360
 
    $output_dir = rtrim($cmd["output"], "/");
 
386
    /* add "/" in case root directory are trimmed */
 
387
    $input_dir = $input."/";
 
388
    $output_dir = $cmd["output"];
361
389
 
362
390
    cslog("* Open input directory $input_dir\n");
363
391
    $open_dir = dir ( $input_dir );
399
427
        }
400
428
    }
401
429
 
 
430
    /* backup loop */
402
431
    while ( $filename = $open_dir->read () ) {
403
432
        /* skip . and .. */
404
433
        if ($filename != "." && $filename != "..") {
424
453
                cslog("\t$cur_file\n");
425
454
 
426
455
                /* recursive for directory */
427
 
                if(is_dir($cur_file) && !is_link($cur_file))
 
456
                if(is_dir($cur_file) && !is_link($cur_file)) {                                                          
428
457
                    cs_backup($cur_file, $perm);
 
458
                }
429
459
                else {
430
460
                    $xfile = $output_dir.$cur_file.$EXT;
 
461
                    
431
462
                    $cur_file_mtime = filemtime($cur_file);
432
463
                    if(!$cur_file_mtime) {
433
464
                        cslog("* Can not file mtime\n");
497
528
        }
498
529
    }
499
530
}
 
531
function csbackup_sync($perm) {
 
532
    global $cmd;
 
533
    global $EXT;
 
534
    
 
535
    $output_dir = $cmd['output'];
 
536
    
 
537
    if(!empty($perm)) {
 
538
        foreach($perm as $source_dir=>$val) {
 
539
            $indir = array();
 
540
                        
 
541
            $out_dir = $output_dir . $source_dir;
 
542
            
 
543
            $fsd = dir($out_dir);
 
544
            
 
545
            if (! $fsd) {
 
546
                cslog("[ERROR] csbackup_sync: Can not open target directory $out_dir, halted!\n");
 
547
                die ();
 
548
            }   
 
549
 
 
550
            $fod = dir($source_dir);
 
551
            
 
552
            if (! $fod) {
 
553
                cslog("[ERROR] csbackup_sync: Can not open source directory $source_dir, halted!\n");
 
554
                die ();
 
555
            }
 
556
            
 
557
            cslog("* Backup sync: $source_dir => $out_dir\n");
 
558
            
 
559
            /* read input */
 
560
            while($filename = $fod->read()) {
 
561
                if($filename != "." && $filename != "..") {
 
562
                    if(is_dir($source_dir . $filename))              
 
563
                        $indir[$filename] = true;
 
564
                    else
 
565
                        $indir[$filename.$EXT] = true;
 
566
                }
 
567
            }                
 
568
                
 
569
            /* compare with output & sync */
 
570
            while($filename = $fsd->read()) {
 
571
                if($filename != "." && $filename != "..") {
 
572
                    if(!isset($indir[$filename])) {
 
573
                        cslog("\tFile does not exist, deleting...\n");
 
574
                        cs_delete($out_dir.$filename);
 
575
                        cslog("\t$out_dir$filename\n");
 
576
                    }
 
577
                }                
 
578
            }
 
579
            
 
580
            empty($indir);
 
581
            $fsd->close();
 
582
            $fod->close();
 
583
        }
 
584
    }
 
585
}
500
586
function cs_restore($input_dir) {
501
587
    global $EXT;
502
588
    
510
596
        die ();
511
597
    }
512
598
 
 
599
    $sync_list = null;
 
600
    
513
601
    while($filename = $open_dir->read()) {
514
602
        if($filename != ".." && $filename != ".") {
515
603
            $cur_file = "$input_dir$filename";
569
657
        }
570
658
    }
571
659
}
 
660
function csrestore_sync($perm) {
 
661
    global $EXT;
 
662
    global $cmd;
 
663
    
 
664
    $input_dir = $cmd['input'];
 
665
    
 
666
    if(!empty($perm)) {
 
667
        foreach($perm as $out_dir=>$val) {
 
668
            $indir = array();
 
669
                        
 
670
            $src_dir = $input_dir . $out_dir;
 
671
            
 
672
            $fsd = dir($src_dir);
 
673
            
 
674
            if (! $fsd) {
 
675
                cslog("[ERROR] csrestore_sync: Can not open input directory $src_dir, halted!\n");
 
676
                die ();
 
677
            }   
 
678
 
 
679
            $fod = dir($out_dir);
 
680
            
 
681
            if (! $fod) {
 
682
                cslog("[ERROR] csrestore_sync: Can not open output directory $out_dir, halted!\n");
 
683
                die ();
 
684
            }
 
685
            
 
686
            cslog("* Restore sync: $src_dir => $out_dir\n");
 
687
            
 
688
            /* read input */
 
689
            while($filename = $fsd->read()) {
 
690
                if($filename != "." && $filename != "..") {
 
691
                    if(substr($filename, strlen($filename) - strlen($EXT)) == $EXT) {
 
692
                        $indir[substr($filename, 0, strlen($filename) - strlen($EXT))] = true;
 
693
                    }
 
694
                    else if(is_dir($src_dir . $filename))              
 
695
                        $indir[$filename] = true;
 
696
                }
 
697
            }                
 
698
                
 
699
            /* compare with output & sync */
 
700
            while($filename = $fod->read()) {
 
701
                if($filename != "." && $filename != "..") {
 
702
                    if(!isset($indir[$filename])) {
 
703
                        cslog("\tFile does not exist, deleting...\n");
 
704
                        cs_delete($out_dir.$filename);
 
705
                        cslog("\t$out_dir$filename\n");
 
706
                    }
 
707
                }                
 
708
            }
 
709
            
 
710
            empty($indir);
 
711
            $fsd->close();
 
712
            $fod->close();
 
713
        }
 
714
    }
 
715
}
572
716
?>