PHP - Backup GIT Repositories
The following code will download all GIT repositories from an account.
<?php
/**
* Script to backup all git repos from CodebaseHQ
*
* @author Marcelo Honorio <marcelohonorio@gmail.com>
*
*/
define('API_URL', 'https://api3.codebasehq.com/');
define('API_USER', '<Your API User>');
define('API_PASSWD', '<Your API Password>');
define('BACKUP_TO', '/storage/'); // Dir destination
define('DIR_TMP', '/storage/backup_cb/'); // Temp dir to git clones
exec('rm -Rf ' . DIR_TMP . '*');
$projects = request_api('projects');
foreach($projects->project as $p){
mkdir(DIR_TMP . $p->permalink);
$repos = request_api($p->permalink . '/repositories');
foreach($repos->repository as $r){
$r = (Array)$r;
exec("git clone --mirror {$r['clone-url']} " . DIR_TMP . $p->permalink . '/' . $r['permalink'] . '.repo');
}
}
$date = date('Ymd');
exec("cd " . DIR_TMP . "; tar czf " . BACKUP_TO . "codebase_{$date}.tar.gz *");
function request_api($method, $parse_xml = true){
$ch = curl_init(API_URL . $method);
curl_setopt($ch, CURLOPT_USERPWD, API_USER . ':' . API_PASSWD);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
if($parse_xml == true){
$ret = simplexml_load_string($ret);
}
return $ret;
}