Tuesday, March 20, 2012

Simple backup script - using PHP and rsync

- Backup multiple directories and files
- Backup every X hours (run backup.php periodically using a scheduler; ex: cron)
- Store backup data separately for each date (backup history by date; ex: YYYY-MM-DD folders)
- Copy data in multiple (unlimited) backup destinations folders

// Backup options
$bkp_verbose = false;
$bkp_write_log_file = true;

// Directories / Files to back-up
$dir_bkp = array();
$dir_bkp[] = '/path-to-project1';
$dir_bkp[] = '/path-to-project2';
$dir_bkp[] = '/path-to-project3/file1';

// Backup storage directories
$dir_bkp_storage = array();
$date_bkp = date('Y-m-d');
$dir_bkp_storage[] = '/path-to-backup-dir1/' . $date_bkp;
$dir_bkp_storage[] = '/path-to-backup-dir2/' . $date_bkp;


foreach($dir_bkp_storage as $dir_storage) {
   
    $time_start = microtime(true);
   
    if(!is_dir($dir_storage)) {
        if(!mkdir($dir_storage, 0755)) {
            echo 'Error: could not create bkp dir: ' . $dir_storage;
            continue;
        }
    }

    foreach($dir_bkp as $dir) {
        echo chr(10) . 'copy backup from ' . $dir . ' to ' . $dir_storage . ' ... ';
        $cmd = "rsync -avz $dir $dir_storage/";
        if($bkp_verbose) {
            echo shell_exec($cmd);
        } else {
            shell_exec($cmd);
        }
    }
   
    if($bkp_write_log_file) {
        $time = microtime(true) - $time_start;
        file_put_contents(
            $dir_storage . '/bkp-log',
            'Date end: '.date('Y-m-d H:i:s') . chr(10) .
                'Total time: ' . $time . ' seconds' . chr(10)
        ');
    }
}

Linux search files by content (grep)

Linux, command line search files by content.
Search file contents, display line, highlight text, show line number:

$ grep  -iRn "text fragment" path1/*

Saturday, March 17, 2012

Bash(shell prompt) server name, time and other options

Edit ~/.bashrc

Change bash(shell prompt) hostname and add a custom server name:
 #export PS1='\h:\w\$ '
export PS1='my-server:\w\$ '

Add bash(shell prompt) time:
export PS1='[\t] my-server:\w\$ '

Apply changes:
Logout ( CTRL + D ) and login again.

Other available options:
\a ASCII bell char
\d current date
\e ASCII escape char
\h hostname (until first ".")
\H hostname
\n new line
\r carriage return
\s shell name
\t current time 24H
\T current time 12H
\@ current time 12H AM/PM
\u username
\v bash version
\V bash version + patch level
\w current working directory
\W basename of the current directory
\! history number of this command
\# number of the command
\$ "$" (if UID is not 0), "#" (if UID is 0)
\\ "\"
\[ begin sequence nonprinting chars
\] end sequence nonprinting chars

Friday, March 16, 2012

Javascript objects and classes; private and public methods and properties

1) Object literals

var Object = {
   
    speed : 0,
   
    setSpeed : function(speed) {
        this.speed = speed;
    },
   
    getSpeed : function() {
        return this.speed;
    }
   
}

Example:
Object.setSpeed(150);
var speed = Object.getSpeed();

All properties and methods of the object literal are public.

2) Classes

function Class1() {
   
    // private property example
    var name = 'Random name here';
   
    // public property example
    this.speed = 0;


    // private method example
    function getName() {
        return name;
    }
   
    // public method example
    this.setSpeed = function(speed) {
        this.speed = speed;
    }
   
    // public method example
    this.getSpeed = function() {
        return this.speed;
    }
   
   
}

Example:
var Object1 = new Class1();
Object1.setSpeed(150);
var speed = Object1.getSpeed();

MySQL Error: Got a packet bigger than 'max_allowed_packet' bytes

Command:
mysql -u root -ppassw db_name < db.sql

Error:
MySQL ERROR 1153 (08S01) at line 35977: Got a packet bigger than 'max_allowed_packet' bytes

FIX/SOLUTION:
1) Edit db.sql and add:
set global net_buffer_length=1000000;
set global max_allowed_packet=100000000;

2) Run:
mysql --max_allowed_packet=100000000 -u root -ppassw db_name < db.sql