mirror of
https://github.com/alexanderepstein/Bash-Snippets
synced 2018-11-08 02:59:35 +01:00
Using my own progress. Also check for valid ghUsername
This commit is contained in:
parent
561465176e
commit
af797f2225
@ -91,42 +91,54 @@ getBitbucketInfo()
|
|||||||
fi
|
fi
|
||||||
echo -n 'Enter your Bitbucket password: '
|
echo -n 'Enter your Bitbucket password: '
|
||||||
read -s password # -s flag hides password text;
|
read -s password # -s flag hides password text;
|
||||||
|
echo
|
||||||
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
backupRepo()
|
backupRepo()
|
||||||
{
|
{
|
||||||
cd ~/temp/github/$repoName || { echo "Fatal error"; return 1; }
|
cd ~/temp/github/$repoName || { echo "Fatal error"; return 1; }
|
||||||
repoSlug=$(echo $repoName | tr '[:upper:]' '[:lower:]')
|
repoSlug=$(echo $repoName | tr '[:upper:]' '[:lower:]')
|
||||||
if [[ $tStamp == "0" ]]; then { echo "Attempting to delete existing bitbucket repostiory"; httpGet -X DELETE "https://$bbUsername:$password@api.bitbucket.org/1.0/repositories/$bbUsername/$repoSlug" > /dev/null; }; fi ## if no timestamp then repo will not be unique we must look to delete old repo
|
if [[ $tStamp == "0" ]]; then { echo -n "Attempting to delete existing bitbucket repostiory..."; httpGet -X DELETE "https://$bbUsername:$password@api.bitbucket.org/1.0/repositories/$bbUsername/$repoSlug" > /dev/null || echo -n -e "Failure!\n"; echo -n -e "Success!\n"; }; fi ## if no timestamp then repo will not be unique we must look to delete old repo
|
||||||
if [[ $private == "1" ]]; then
|
if [[ $private == "1" ]]; then
|
||||||
echo "Creating private repository for $repoName"
|
echo -n "Creating private repository for $repoName..."
|
||||||
else
|
else
|
||||||
echo "Creating public repository for $repoName"
|
echo -n "Creating public repository for $repoName..."
|
||||||
fi
|
fi
|
||||||
if [[ $tStamp == "1" ]]; then ## create the repository with a timestamp appended to repo name
|
if [[ $tStamp == "1" ]]; then ## create the repository with a timestamp appended to repo name
|
||||||
timestamp=$(date | tr " " _ | tr : _ ) ## we do this because it takes care of changes bitbucket would have made
|
timestamp=$(date | tr " " _ | tr : _ ) ## we do this because it takes care of changes bitbucket would have made
|
||||||
if [[ $private == "1" ]]; then # if so we will use --data is_private=true when creating repository
|
if [[ $private == "1" ]]; then # if so we will use --data is_private=true when creating repository
|
||||||
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName$timestamp --data is_private=true > /dev/null || { echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName$timestamp --data is_private=true > /dev/null || { echo -n -e "Failure!\n"; echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
||||||
else
|
else
|
||||||
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName$timestamp > /dev/null || { echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName$timestamp > /dev/null || { echo -n -e "Failure!\n"; echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
||||||
fi
|
fi
|
||||||
echo "Setting new remote url"
|
echo -n -e "Success!\n"
|
||||||
git remote set-url origin "https://$bbUsername:$password@bitbucket.org/$bbUsername/$repoName$timestamp.git" > /dev/null || return 1 ## switch the remote over to bitbucket rather than github
|
echo -n "Setting new remote url..."
|
||||||
|
git remote set-url origin "https://$bbUsername:$password@bitbucket.org/$bbUsername/$repoName$timestamp.git" > /dev/null || { echo -n -e "Failure!\n"; return 1;} ## switch the remote over to bitbucket rather than github
|
||||||
else # we are creating a reoo without a timestamp appended name
|
else # we are creating a reoo without a timestamp appended name
|
||||||
if [[ $private == "1" ]]; then # if so we will use --data is_private=true when creating repository
|
if [[ $private == "1" ]]; then # if so we will use --data is_private=true when creating repository
|
||||||
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName --data is_private=true > /dev/null
|
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName --data is_private=true > /dev/null || { echo -n -e "Failure!\n"; echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
||||||
else
|
else
|
||||||
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName > /dev/null
|
httpGet --user $bbUsername:$password https://api.bitbucket.org/1.0/repositories/ --data name=$repoName > /dev/null || { echo -n -e "Failure!\n"; echo "Error: creating the bitbucket repo failed, most likely due to an incorrect username or password."; return 1; }
|
||||||
fi
|
fi
|
||||||
echo "Setting new remote url"
|
echo -n -e "Success!\n"
|
||||||
git remote set-url origin "https://$bbUsername:$password@bitbucket.org/$bbUsername/$repoName.git" > /dev/null || return 1 ## switch the remote over to bitbucket rather than github
|
echo -n "Setting new remote url..."
|
||||||
|
git remote set-url origin "https://$bbUsername:$password@bitbucket.org/$bbUsername/$repoName.git" > /dev/null || { echo -n -e "Failure!\n"; return 1;} ## switch the remote over to bitbucket rather than github
|
||||||
|
echo -n -e "Success!\n"
|
||||||
fi
|
fi
|
||||||
echo "Uploading $repoName to bitbucket"
|
echo -n "Uploading $repoName to bitbucket.."
|
||||||
git push -q --progress origin --all > /dev/null || return 1 ## pushes al files to github and most of the repo history
|
|
||||||
echo "Uploading the tags for $repoName"
|
git push -q origin --all > /dev/null && touch ~/temp/github/.BSnippetsHiddenFile || { echo -n -e "Failure!\n"; touch -f ~/temp/github/.BSnippetsBrokenFile; return 1; } & ## pushes al files to github and most of the repo history
|
||||||
git push -q --progress origin --tags > /dev/null || { echo "Tags for $repoName not succesfully backed up."; return 1; } ## have to push tags here since --tags and --all are mutually exclusive
|
while [[ ! -f ~/temp/github/.BSnippetsHiddenFile ]] ;do if [ -f ~/temp/github/.BSnippetsBrokenFile ];then return 1;fi && echo -n "." && sleep 2; done
|
||||||
echo "Successfully backedup $repoName"
|
rm -f ~/temp/github/.BSnippetsHiddenFile
|
||||||
|
echo -e -n "Success!\n"
|
||||||
|
echo -n "Uploading the tags for $repoName.."
|
||||||
|
git push -q origin --tags > /dev/null && touch ~/temp/github/.BSnippetsHiddenFile || { echo -n -e "Failure!\n"; touch -f ~/temp/github/.BSnippetsBrokenFile; return 1; } & ## have to push tags here since --tags and --all are mutually exclusive
|
||||||
|
while [[ ! -f ~/temp/github/.BSnippetsHiddenFile ]] ;do if [ -f ~/temp/github/.BSnippetsBrokenFile ];then return 1;fi && echo -n "." && sleep 2; done
|
||||||
|
rm -f ~/temp/github/.BSnippetsHiddenFile
|
||||||
|
echo -e -n "Success!\n"
|
||||||
rm -rf ~/temp #if we have succesfully backedup the repo we dion't need this anymore and if we do we will recreate it
|
rm -rf ~/temp #if we have succesfully backedup the repo we dion't need this anymore and if we do we will recreate it
|
||||||
|
echo "Successfully backed up $repoName"
|
||||||
}
|
}
|
||||||
|
|
||||||
## When cloudup is called with no flags
|
## When cloudup is called with no flags
|
||||||
@ -146,13 +158,20 @@ getGitHubUserInfo()
|
|||||||
## function that handles cloning a repository it uses $ghUsername and $repoName
|
## function that handles cloning a repository it uses $ghUsername and $repoName
|
||||||
cloneGitHubRepo()
|
cloneGitHubRepo()
|
||||||
{
|
{
|
||||||
|
echo -n "Cloning $repoName.."
|
||||||
|
validRepo="false"
|
||||||
|
for repo in "${repoNames[@]}"; do
|
||||||
|
if [[ $repo == "$repoName" ]];then validRepo="true" && break; fi
|
||||||
|
done
|
||||||
|
if ! $validRepo;then { echo -n -e "Failure!\n"; echo "Github repostiory is not valid (either incorrect username or repository)"; return 1;}; fi
|
||||||
mkdir ~/temp
|
mkdir ~/temp
|
||||||
cd || { echo "Fatal error"; return 1; }
|
cd || { echo "Fatal error"; return 1; }
|
||||||
mkdir ~/temp/github || { echo "Fatal error"; return 1; }
|
mkdir ~/temp/github || { echo "Fatal error"; return 1; }
|
||||||
cd ~/temp/github || { echo "Fatal error"; return 1; }
|
cd ~/temp/github || { echo "Fatal error"; return 1; }
|
||||||
echo "Cloning $repoName"
|
git clone -q https://github.com/$ghUsername/$repoName && touch ~/temp/github/.BSnippetsHiddenFile || { touch ~/temp/github/.BSnippetsBrokenFile; echo -e -n "Failure!\n"; return 1;} &
|
||||||
git clone -q --progress https://github.com/$ghUsername/$repoName || return 1
|
while [[ ! -f ~/temp/github/.BSnippetsHiddenFile ]] ;do if [ -f ~/temp/github/.BSnippetsBrokenFile ];then return 1;fi && echo -n "." && sleep 2; done
|
||||||
echo "Successfully cloned $repoName"
|
rm -f ~/temp/github/.BSnippetsHiddenFile
|
||||||
|
echo -n -e "Success!\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
## Grabs the last 100 updated repos and greps to grab the repository names and puts them in an array called repoNames
|
## Grabs the last 100 updated repos and greps to grab the repository names and puts them in an array called repoNames
|
||||||
@ -160,6 +179,8 @@ getGithubRepoNames()
|
|||||||
{
|
{
|
||||||
for pageNumber in {1..100}; do ## iterate through 100 possible pages
|
for pageNumber in {1..100}; do ## iterate through 100 possible pages
|
||||||
response=$(httpGet "https://api.github.com/users/$ghUsername/repos?sort=updated&per_page=100&page=$pageNumber") ## grab the original response
|
response=$(httpGet "https://api.github.com/users/$ghUsername/repos?sort=updated&per_page=100&page=$pageNumber") ## grab the original response
|
||||||
|
echo $response
|
||||||
|
if [[ $pageNumber == "1" && ( $(echo $response | grep -Eo "Not Found") == "Not Found" || $response == "[' ']") ]];then { echo -e -n "Failure!"; echo "Github username is invalid"; return 1;};fi
|
||||||
repoResponse=$(echo $response | grep -Eo '"name": "[ a-Z . \/ \\ 0-9 -- _ ]*' | sed s/'"name": "'/""/g ) ## extract the repo names from the response
|
repoResponse=$(echo $response | grep -Eo '"name": "[ a-Z . \/ \\ 0-9 -- _ ]*' | sed s/'"name": "'/""/g ) ## extract the repo names from the response
|
||||||
forkResponse=($(echo $response | grep -Eo '"fork": [a-Z]*' | cut -d " " -f 2 | sed s/"'"//g )) ## extract the fork status of each repo
|
forkResponse=($(echo $response | grep -Eo '"fork": [a-Z]*' | cut -d " " -f 2 | sed s/"'"//g )) ## extract the fork status of each repo
|
||||||
count=0 ## used to iterate through the fork statuses
|
count=0 ## used to iterate through the fork statuses
|
||||||
@ -246,6 +267,7 @@ fi
|
|||||||
if [[ $all == "0" ]]; then
|
if [[ $all == "0" ]]; then
|
||||||
if [[ ($private == "0" && $tStamp == "0" && $1 != "")]]; then ## checking for an arguments after possible flags if so then run through the arguments (repositories) and back them up
|
if [[ ($private == "0" && $tStamp == "0" && $1 != "")]]; then ## checking for an arguments after possible flags if so then run through the arguments (repositories) and back them up
|
||||||
getGitHubUserInfo || exit 1
|
getGitHubUserInfo || exit 1
|
||||||
|
getGithubRepoNames || exit 1
|
||||||
getBitbucketInfo || exit 1
|
getBitbucketInfo || exit 1
|
||||||
for i in "$@"; do ## if private is not on as a flag start rpping through them
|
for i in "$@"; do ## if private is not on as a flag start rpping through them
|
||||||
repoName=$i
|
repoName=$i
|
||||||
@ -257,6 +279,7 @@ if [[ $all == "0" ]]; then
|
|||||||
exit 0
|
exit 0
|
||||||
elif [[ ( $private == "0" && $tStamp == "0" && $1 == "" ) || ( $private == "1" && $tStamp == "0" && $2 == "" ) || ( $private == "0" && $tStamp == "1" && $2 == "" ) || ( $private == "1" && $tStamp == "1" && $3 == "" ) ]]; then ## check for empty arguments after all possible flags, this will intiate a guided backup
|
elif [[ ( $private == "0" && $tStamp == "0" && $1 == "" ) || ( $private == "1" && $tStamp == "0" && $2 == "" ) || ( $private == "0" && $tStamp == "1" && $2 == "" ) || ( $private == "1" && $tStamp == "1" && $3 == "" ) ]]; then ## check for empty arguments after all possible flags, this will intiate a guided backup
|
||||||
getGitHubUserInfo || exit 1
|
getGitHubUserInfo || exit 1
|
||||||
|
getGithubRepoNames || exit 1
|
||||||
getGitHubRepoInfo || exit 1
|
getGitHubRepoInfo || exit 1
|
||||||
getBitbucketInfo || exit 1
|
getBitbucketInfo || exit 1
|
||||||
echo
|
echo
|
||||||
@ -266,6 +289,7 @@ if [[ $all == "0" ]]; then
|
|||||||
else ## flags are set but arguments are also provided
|
else ## flags are set but arguments are also provided
|
||||||
firstArg=$(( $private + $tStamp + 1 ))
|
firstArg=$(( $private + $tStamp + 1 ))
|
||||||
getGitHubUserInfo || exit 1
|
getGitHubUserInfo || exit 1
|
||||||
|
getGithubRepoNames || exit 1
|
||||||
getBitbucketInfo || exit 1
|
getBitbucketInfo || exit 1
|
||||||
for i in "${@:$firstArg}"; do
|
for i in "${@:$firstArg}"; do
|
||||||
repoName=$i
|
repoName=$i
|
||||||
|
Loading…
Reference in New Issue
Block a user