jgillman's Liquid Web Update Unofficial tips, tricks, and happenings from a Liquid Web sales engineer

29Jul/110

Copying databases from one cPanel user to another

I was going through some of my tomboy notes and saw some PHP code that I cobbled together for a customer who needed to copy MySQL databases from one cPanel user to another.

I thought that it may be useful to others as well

 PHP | 
 
 copy code |
?

01
<?php
02
	// cPanel user you are copying data from
03
	$from_user = "fromuser";
04
	// cPanel user you are copying data to
05
	$to_user = "touser";
06
	// Some place to temporarilly story the SQL dump files
07
	$dumpdir = "/root/tempdbdump/";	
08
 
09
	// Add all the databases you need copied over to the array
10
	$dumpdb[] = "db1";
11
	$dumpdb[] = "db2";
12
	$dumpdb[] = "db3";
13
	$dumpdb[] = "db4";
14
	$dumpdb[] = "db5";
15
	$dumpdb[] = "db6";
16
 
17
	foreach($dumpdb as $database)
18
	{
19
		$dumpline = "mysqldump " .  $from_user . "_" . $database . " " . $dumpdir . $database .".sql";
20
		shell_exec($dumpline);
21
 
22
		$restoreline = "mysql " $to_user . "_" . $databasename " < " . $dumpdir . $database . ".sql";
23
		shell_exec($restoreline);
24
	}
25
?>

One thing to note is that I got lazy and didn't include code to delete the dump file after use, but that's a relatively simple task.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.

FireStats icon Powered by FireStats
Optimization WordPress Plugins & Solutions by W3 EDGE