Snippets populaires
Migrer un dépôt Subversion d'une machine à une autre
Sur l'ancienne machine :
$ svnadmin dump /var/lib/subversion/myproject > ~/myproject.svndump
$ scp ~/myproject.svndump new-server:
Sur la nouvelle machine :
$ svnadmin create /var/lib/subversion/myproject
$ svnadmin load /var/lib/subversion/myproject < ~/myproject.svndump
Via http://fashion.hosmoz.net/blog/post/2006/11/09/Migrer-un-depot-subversion
Le pattern Strategy en PHP5
<?php interface IStrategy { function filter( $record ); } class FindAfterStrategy implements IStrategy { private $_name; public function __construct( $name ) { $this->_name = $name; } public function filter( $record ) { return strcmp( $this->_name, $record ) <= 0; } } class RandomStrategy implements IStrategy { public function filter( $record ) { return rand( 0, 1 ) >= 0.5; } } class UserList { private $_list = array(); public function __construct( $names ) { if ( $names != null ) { foreach( $names as $name ) { $this->_list []= $name; } } } public function add( $name ) { $this->_list []= $name; } public function find( $filter ) { $recs = array(); foreach( $this->_list as $user ) { if ( $filter->filter( $user ) ) $recs []= $user; } return $recs; } } $ul = new UserList( array( "Andy", "Jack", "Lori", "Megan" ) ); $f1 = $ul->find( new FindAfterStrategy( "J" ) ); print_r( $f1 ); $f2 = $ul->find( new RandomStrategy() ); print_r( $f2 ); ?>
Le pattern ChainOfCommands en PHP5
<?php interface ICommand { function onCommand( $name, $args ); } class CommandChain { private $_commands = array(); public function addCommand( $cmd ) { $this->_commands []= $cmd; } public function runCommand( $name, $args ) { foreach( $this->_commands as $cmd ) { if ( $cmd->onCommand( $name, $args ) ) return; } } } class UserCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != 'addUser' ) return false; echo( "UserCommand handling 'addUser'\n" ); return true; } } class MailCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != 'mail' ) return false; echo( "MailCommand handling 'mail'\n" ); return true; } } $cc = new CommandChain(); $cc->addCommand( new UserCommand() ); $cc->addCommand( new MailCommand() ); $cc->runCommand( 'addUser', null ); $cc->runCommand( 'mail', null ); ?>
Le pattern Observer en PHP5
<?php interface IObserver { function onChanged( $sender, $args ); } interface IObservable { function addObserver( $observer ); } class UserList implements IObservable { private $_observers = array(); public function addCustomer( $name ) { foreach( $this->_observers as $obs ) $obs->onChanged( $this, $name ); } public function addObserver( $observer ) { $this->_observers []= $observer; } } class UserListLogger implements IObserver { public function onChanged( $sender, $args ) { echo( "'$args' added to user list\n" ); } } $ul = new UserList(); $ul->addObserver( new UserListLogger() ); $ul->addCustomer( "Jack" ); ?>
Le pattern Factory en PHP5
<?php interface IUser { function getName(); } class User implements IUser { public static function Load( $id ) { return new User( $id ); } public static function Create( ) { return new User( null ); } public function __construct( $id ) { } public function getName() { return "Jack"; } } $uo = User::Load( 1 ); echo( $uo->getName()."\n" ); ?>
Synchroniser son serveur à l'heure atomique
$ sudo apt-get install ntpdate
$ sudo ntpdate europe.pool.ntp.org
Puis dans un crontab en root :
# m h dom mon dow command 0 0 * * * ntpdate europe.pool.ntp.org
Compter le nombre de fichiers en fonction d'un pattern
$ ls -R /my/path/ | grep -v monpattern | wc -l
Dumper toutes les bases mysql et les réimporter
Dump :
$ mysqldump -uroot -p --all-databases --opt > export.sql
Reimport :
$ mysql -uroot -p < export.sql
Avec compression bz2 :
Export :
$ mysqldump -uroot -p --all-databases --opt | bzip2 > export.sql.bz2
Réimport :
$ bzcat export.sql.bz2 | mysql -uroot -p
Faire un screencast sous Ubuntu Edgy
Pas facile vu que ffmpeg nécessite un patch. Heureusement, y'a un .deb de la version patchée :
$ wget -c http://erunar.co.uk/debs/ubuntu_dapper/ffmpeg-0.4.9-p20051216_i386.deb
$ dpkg -i ffmpeg-0.4.9-p20051216_i386.deb
Pour lancer une capture :
$ ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s 1400x1050 ~/Desktop/test.avi
Il faut adapter 1400x1050 à la résolution de l'écran, oeuf corse.
Exporter et réimporter la liste des paquets installés
Genre pour reproduire la config d'un ancien serveur vers un nouveau serveur.
Export :
$ sudo dpkg --get-selections > selections.txt
Réimportation :
$ sudo dpkg --set-selections < selections.txt
Réinstallation des paquets :
$ sudo apt-get dselect-upgrade





