Created
October 29, 2018 06:35
-
-
Save finalwebsites/2dded94afc12dd53b48b9d64ef9f7c2e to your computer and use it in GitHub Desktop.
Migrate Serverpilot all apps from old server to a new server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// the Serverpilot wrapper, get it the class from https://github.com/daverogers/serverpilot-php | |
include_once 'ServerPilot.php'; | |
$sp_id = 'SPCLIENTID'; | |
$api_key = 'SPAPIKEY'; | |
// you can find those id's in the URL from your Serverpilot application | |
$from_server = 'MIGRATEFROM'; | |
$to_server = 'MIGRATETO'; | |
$excl_apps = array('0default'); | |
$old_serverpilot_user_id = ''; | |
$new_serverpilot_user_id = ''; | |
$config = array('id' => $sp_id, 'key' => $api_key); | |
$sp = new ServerPilot($config); | |
$apps = $sp->app_list(); // get all apps | |
// get and store all database users / names | |
$dbs = $sp->database_list(); | |
$dbdata = array(); | |
foreach ($dbs as $db) { | |
foreach ($db as $d) { | |
if ($d->serverid == $from_server) { | |
$dbdata[$d->appid][] = array('dbname'=>$d->name, 'dbuser'=>$d->user->name); | |
} | |
} | |
} | |
$new_users = array(); // array to store the new user id's | |
foreach ($apps as $app) { | |
foreach($app as $a) { | |
// only those apps from the one old server | |
if ($a->serverid == $from_server && !in_array($a->name, $excl_apps)) { | |
// create or get the user id | |
// additional if statement for existing serverpilot user | |
if ($a->sysuserid == $old_serverpilot_user_id) { | |
$uid = $new_serverpilot_user_id; // user "serverpilot" | |
} else { | |
$user = $sp->sysuser_info( $a->sysuserid ); | |
$username = reset($user)->name; | |
if (array_key_exists($username, $new_users)) { | |
$uid = $new_users[$username]; | |
} else { | |
// create the user on the new server | |
$newuser = $sp->sysuser_create($to_server, $username, uniqid()); | |
$uid = $newuser->data->id; | |
$new_users[$username] = $uid; // store the user id in array | |
} | |
} | |
// create the app on the new server | |
$newapp = $sp->app_create($a->name, $uid, $a->runtime, $a->domains); | |
// create now the databases(s) | |
if (is_array($dbdata[$a->id]) && count($dbdata[$a->id]) > 0) { | |
foreach ($dbdata[$a->id] as $curr_db) { | |
$debres = $sp->database_create($newapp->data->id, $curr_db['dbname'], $curr_db['dbuser'], uniqid()); | |
} | |
} | |
// add or copy the SSL certificate with key | |
$ssl = $sp->ssl_add($newapp->data->id, $a->ssl->key, $a->ssl->cert); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment