Brainfish Eat Fishbrain

Saturday, May 22, 2010

Migrating a Wordpress installation from your localhost to a production server (using cpanel)

Say you have locally WP installed under WAMP or MAMP or whatever under;

http//localhost:8888/somesite

and you want to run it, with all content and plugins and what have not under:

http://www.somesite.com

The steps to do this:

- create somesite.com in your WHM system (http://yourhost.com/whm) or make sure your site exists
- go to your local phpmyadmin, usually; http://localhost:8888/ and then clicking on phpmyadmin
- click on the database you are using
- click on export
- make sure SQL is selecting on the left
- click on go
- open another tab in your browser and open http://www.somesite.com/cpanel
- login
- create a database and a user for that database and remember what you you used there
- now open phpmyadmin in your cpanel
- click on the database you just created
- click on SQL
- copy/paste the sql from your localhost tab into this new db and click go
- now connect (with ftp) to your somesite.com server and copy over everything in your root directory from your localhost (for instance; htdocs/somesite/*)
- edit, remotely, your public_html/wp-config.php; change the values DB_NAME, DB_USER, DB_PASS to the values you used to create your db in cpanel
- now, basically, your site should work; simply try http://www.somesite.com
- if you get a database error, please check your remote wp-config.php to see what went wrong
- you probably now get some error; not found http://somesite.com:8888 or something like that; this is because the php options are wrong
- to fix this, just open SSH to your remote server and run;

cd /home/somesite
mysqldump YOURDBNAME wp_options > tmp.sql
sed -i 's/localhost:8888\/somesite/www.somesite.com/ig' tmp.sql
mysql YOURDBNAME < tmp.sql

Now your site will work.

Have fun.

Monday, October 26, 2009

Cpanel: Monitoring :2082 logins

Simple script to send you all new logins of the day. Seeing something strange would trigger further research.

#!/usr/bin/perl

chdir('/root');

$d = `date +%m/%d/%Y`;
chomp($d);

@logins = `cat /usr/local/cpanel/logs/access_log|grep $d|awk '{print \$1 " " \$3}'|sort|uniq`;

$x = "";

foreach(@logins) {
 chomp; 
 /(.*?)\ (.*)/;
 next if $2 eq "-";
 $z = `whois $1|grep addres|tail -n 1`;
 chomp($z);
 $x.="$z $1 $2\n";
}

if (not -f "./latestscan") {
 `touch ./latestscan`;
}

$y = `cat ./latestscan`;

exit if $y eq $x;

open(F, ">latestscan");
print F $x; 
close F;

 $sendmail = "/usr/sbin/sendmail -t";
 open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; 
 print SENDMAIL "Reply-to: root\@myserver.org\n"; 
 print SENDMAIL "Subject: Login report hostf1\n"; 
 print SENDMAIL "To: alerts\@yourserver.com\n"; 
 print SENDMAIL "Content-type: text/plain\n\n"; 
 print SENDMAIL $x; 
 close(SENDMAIL); 

Sunday, October 25, 2009

Watching series; I don't want to touch my computer

When I watch series I don't want to touch my computer and I watch all episodes in one fell swoop; if I do something like mplayer *.mpg it crashes, so that doesn't work. This does, run like;


./play "*.mpg"


or


./play "*.avi"


The code;


#!/usr/bin/perl

$a = "";
foreach(@ARGV) {
$a.=" " if $a;
$a.=$_;
}

while(1){foreach(glob("$a")){`mplayer -fs \"$_\"`}}

Saturday, October 24, 2009

Cpanel security: scanning for usage or upload of c99 shell script (or other scripts)

Sometimes users upload stuff to your server or use scripts you don't want used. To detect them fast, I wrote this script.


#!/usr/bin/perl

use Digest::Perl::MD5 'md5_hex';

chdir('/root/');
`touch ./scanned` if not -f "./scanned";

%h = ();
open(F, "scanned");
while() {
chomp;
$h{$_} = 1;
}
close F;

@x = `cd /etc/httpd/domlogs/; grep c99me *`;
open(F, ">>scanned");
$s = "";
foreach(@x) {
chomp;
$m = md5_hex($_);
next if $h{$m};
print F "$m\n";
$s.=$_."\n";
}
close F;

if ($s) {
$sendmail = "/usr/sbin/sendmail -t";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL "Reply-to: root\@myserver.org\n";
print SENDMAIL "Subject: Found some illegal stuff on server\n";
print SENDMAIL "To: alerts\@somewhere.com\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $s;
close(SENDMAIL);
}

Cpanel security: cron update all Wordpress installations on your server

Two simple scripts. Use with caution and at your own risk. Might eat your machine and piss off all your users.


!/usr/bin/perl

`rm -fR wordpress`;
`wget http://wordpress.org/latest.zip`;
`unzip latest.zip`;

@all = `ls -la /home/|awk '{print \$3}'|grep -v root`;

foreach(@all) {
chomp;
next if /^$/;
`./updatewp $_`;
}



#!/usr/bin/perl

$host = `hostname`;
chomp($host);

$u = $ARGV[0];

exit if !$u; # user as arg

exit if not -f "/home/$u/public_html/wp-config.php"; # not wp install

# you shouldn't actually have the readme.html, but if it's there it's a bit faster
$v1 = `cat /home/$u/public_html/readme.html|grep Version > dev/null`;
$v1 =~ /Version\ (\d+\.\d+\.\d+)/;
$v1 = $1;

$v2 = `cat wordpress/readme.html|grep Version`;
$v2 =~ /Version\ (\d+\.\d+\.\d+)/;
$v2 = $1;

exit if $v1 eq $v2; # already updated

`cp -a wordpress /home/$u/wp_int`;

`cp -rpf /home/$u/public_html/wp-config.php /home/$u/wp_int`;
`cp -rpf /home/$u/public_html/wp-content/* /home/$u/wp_int/wp-content/`;
`cp -rpf /home/$u/public_html/.htaccess /home/$u/wp_int/`;

`chown $u.$u /home/$u/wp_int`;

`cp -a /home/$u/public_html /home/$u/wpback\`date +%d%m%y\``;

`cp -rpf /home/$u/wp_int/* /home/$u/public_html/`;

`rm -fR /home/$u/wp_int`;

$x = `lynx -dump http://$host/~$u/wp-admin/upgrade.php`;

if ($x =~ /Database Upgrade Required/isgm) {
`lynx -dump http://$host/~$u/wp-admin/upgrade.php?step=1\&backto=`;
}

print "Updated $u\n";

Monday, August 03, 2009

Mac OS X Firefox 3.5 intervals to 100% CPU about every 30 seconds

Yesterday suddenly my Firefox started to show the busy mouse pointer about every 30 seconds. Very annoying as it was completely unusable during about 5 seconds, and then 30 seconds later again etc.

I removed my Profile in ~/Library and checked if it was FF or something in my profile. Ofcourse it was something in my profile.

Checking my files I saw one file that looked 'odd' ; places.sqlite was rather huge (I do browser a lot), however sqlite can handle quite big databases. Anyway; I ran a

rm -f places*

and restarted FF. No more 100%. Fixed.

What is going on I don't know; it looks like FF is running some kind of really bad query on that sqlite every +/- 30 s which is messing up the whole thing.

Tuesday, June 30, 2009

Checking/repairing all MySQL tables

Caution: on busy servers this will make a lot of load. Use only when you suspect/know tables are broken.


#!/usr/bin/perl

$mysql = "/var/lib/mysql";

@res = `cd $mysql; find .|grep MYD`;

foreach(@res) {
chomp;
/\.\/(.*?)\/(.*?).MYD/;
$tab = $1.".".$2;
print `mysql --execute='repair table $tab'`;
}

Friday, February 06, 2009

A simple, non bloated script for Amazon S3 backups (Linux, easy portable) - II - recurse to subdirs

To follow this, please read this post first.

We needed to backup our images from http://www.picturepush.com to S3 for our premium members so we were searching for a simple script to do that one time. As in my previous post; there is no such thing. Bloated, uninstallable shit is the only thing there is.

So I changed the code a bit and added;


foreach($_SERVER["argv"] as $d) {
recurse_copy($d, $d);
}
exit;

function recurse_copy($d, $org) {
foreach(glob("$d/*") as $d1) {
if ($d1=='..' || $d1=='.') continue;
if (is_dir($d1)) {
recurse_copy($d1, $org);
} else {
// put on s3
global $s3;
global $bucket;
$s3->putObjectFile($d1, $bucket, $d1);
echo "Storing: ".$d1."\n";
}
}
}


below the last;


array_shift($_SERVER["argv"]);