-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepobackup.php
79 lines (70 loc) · 2.38 KB
/
Repobackup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
require_once 'vendor/autoload.php';
require_once 'RepoSources.php';
$repoBackup = new RepoBackup();
$repoBackup->run();
class RepoBackup{
private $repositories = null;
private $gists = null;
private $gistsFolder = "gists/";
private $repositoriesFolder = "repositories/";
public function __construct(){
$this->git = new PHPGit\Git();
$this->cwd = getcwd()."/Backups/";
}
function getInput($message){
echo $message;
return trim(fgets(STDIN));
}
function run(){
echo "Backing up\n";
$line = $this->getInput("Backup GitHub [y/n]:");
if(strtolower($line) == 'y'){
$gitSource = new GitHubSource();
$user = $this->getInput("GitHub Username:");
$token = $this->getInput("GitHub API Token:");
$gitSource->setCredentials(array('user'=>$user,'token'=>$token));
$this->backupRepositories($gitSource,'GitHub/'.$user."/");
}
$line = $this->getInput("Backup BitBucket [y/n]:");
if(strtolower($line) == 'y'){
$user = $this->getInput("BitBucket Username:");
$pass = $this->getInput("BitBucket Pass:");
$bbSource = new BitbucketSource();
$bbSource->setCredentials(array('user'=>$user,'pass'=>$pass));
$this->backupRepositories($bbSource,'BitBucket/'.$user.'/');
}
}
function backupRepositories(RepoSource $src, $path){
$repositories = $src->getRepositories();
foreach($repositories as $repository){
echo "Pulling ".$repository['name']."\n";
$this->backupRepo($repository,$path);
}
}
function backupRepo($repository,$path){
$repoName = $repository['name'];
$cloneURL = $repository['clone_url'];
$folder = $this->cwd."/".$path.$repoName;
echo "Backup Repo: $cloneURL $folder\n";
if($repository['type'] == 'git'){
if(!file_exists($folder . DIRECTORY_SEPARATOR . '.git')){
echo "Calling $cloneURL\n";
$this->git->clone($cloneURL, $folder);
}
$this->git->setRepository($folder);
$this->git->fetch->all();
}elseif($repository['type'] == 'hg'){
$output = array();
$return = 0;
if(!file_exists($folder . DIRECTORY_SEPARATOR . '.hg')){
echo "Calling "."hg clone ".$cloneURL. " ".$folder."\n";
exec ("hg clone ".$cloneURL. " ".$folder, $output , $return);
}
echo "Calling "."hg pull -R ".$folder."\n";
exec ("hg pull -R ".$folder, $output , $return);
}else{
echo "Unrecognised VC type!\n";
}
}
}