Example #1
0
def do_bb(username, password):

    try:
        from urllib.parse import quote
    except ImportError:
        from urllib.parse import quote

    oauth_key = None
    oauth_secret = None
    _verbose = False
    owner = username

    bucket = bitbucket.BitBucket(
        username=username,
        password=password,
        oauth_key=oauth_key,
        oauth_secret=oauth_secret,
        verbose=_verbose,
    )

    user = bucket.user(owner)
    repos = sorted(user.repositories(), key=lambda repo: repo.get("name"))
    urls = []

    for repo in repos:
        owner_url = quote(owner)
        slug = repo.get('slug')
        slug_url = quote(slug)
        url = "[email protected]:%s/%s.git" % (owner_url, slug_url)
        urls.append(url)
    return urls
Example #2
0
def main():
    parser = argparse.ArgumentParser(description="Usage: %prog [options] ")
    parser.add_argument("-u",
                        "--username",
                        dest="username",
                        help="Bitbucket username")
    parser.add_argument("-p",
                        "--password",
                        dest="password",
                        help="Bitbucket password")
    parser.add_argument("-l",
                        "--location",
                        dest="location",
                        help="Local backup location")
    parser.add_argument('--http', action='store_true', help="Fetch via https")
    parser.add_argument(
        '--mirror',
        action='store_true',
        help=
        "Cloning with mirror option (only applicable with Git repositories)")
    args = parser.parse_args()
    username = args.username
    password = args.password
    location = args.location
    mirror = args.mirror
    http = args.http
    if not password:
        password = getpass(prompt='Enter your bitbucket password: '******'Please supply a username and backup location'
                     ' (-u <username> -l <backup location>)')
    if not os.path.isdir(location):
        print(
            'Backup location does not exist.  Please provide an '
            'existing directory.')
    bb = bitbucket.BitBucket(username, password)
    user = bb.user(username)
    repos = user.repositories()
    if not repos:
        print(
            'No repositories found.  Are you sure you provided '
            'the correct password')
    for repo in repos:
        print "Backing up %s" % repo.get("name")
        backup_dir = os.path.join(location, repo.get("slug"))
        if not os.path.isdir(backup_dir):
            clone_repo(repo, backup_dir, http, password, mirror)
        else:
            update_repo(repo, backup_dir)
Example #3
0
def sync_dir(bucket_name, path):
    bucket = bitbucket.BitBucket(
        bucket_name,
        access_key='0Z9ZYHJQKNASPMD5W6R2',
        secret_key='japrNLqnUHlwa43efYmO5TIWsDc52sLEtM8zghmd')
    for root, dirs, files in os.walk(path):
        for file in files:
            fullpath = os.path.join(root, file)
            try:
                if bucket.has_key(fullpath):
                    bits = bucket[fullpath]
                    bits.filename = fullpath
                else:
                    bits = bitbucket.Bits(filename=fullpath)
                    bucket[fullpath] = bits
            except bitbucket.BitBucketEmptyError:
                print 'sync_dir: Empty File - Ignored %s' % fullpath
        full_paths = [os.path.join(root, f) for f in files]
        for file in bucket.keys():
            if file not in full_paths:
                del bucket[file]

    return bucket
Example #4
0
    _quiet = args.quiet
    _verbose = args.verbose
    _mirror = args.mirror
    _with_wiki = args.with_wiki
    if _quiet:
        _verbose = False  # override in case both are selected
    http = args.http
    if not password:
        if not args.skip_password:
            password = getpass(prompt='Enter your bitbucket password: '******'Please supply a username and backup location (-u <username> -l <backup location>)')

    # ok to proceed
    try:
        bb = bitbucket.BitBucket(username, password, _verbose)
        user = bb.user(owner)
        repos = user.repositories()
        if not repos:
            print("No repositories found. Are you sure you provided the correct password")
        for repo in repos:
            debug("Backing up [%s]..." % repo.get("name"), True)
            backup_dir = os.path.join(location, repo.get("slug"))
            if not os.path.isdir(backup_dir):
                clone_repo(repo, backup_dir, http, password, mirror=_mirror, with_wiki=_with_wiki)
            else:
                debug("Repository [%s] already in place, just updating..." % repo.get("name"))
                update_repo(repo, backup_dir, with_wiki=_with_wiki)
        if args.compress:
            compress(repo, location)
        debug("Finished!", True)
def main():
    parser = argparse.ArgumentParser(description="Usage: %prog [options] ")
    parser.add_argument("-u",
                        "--username",
                        dest="username",
                        help="Bitbucket username")
    parser.add_argument("-p",
                        "--password",
                        dest="password",
                        help="Bitbucket password")
    parser.add_argument("-k",
                        "--oauth-key",
                        dest="oauth_key",
                        help="Bitbucket oauth key")
    parser.add_argument("-s",
                        "--oauth-secret",
                        dest="oauth_secret",
                        help="Bitbucket oauth secret")
    parser.add_argument("-t", "--team", dest="team", help="Bitbucket team")
    parser.add_argument("-l",
                        "--location",
                        dest="location",
                        help="Local backup location")
    parser.add_argument("-v",
                        "--verbose",
                        action='store_true',
                        dest="verbose",
                        help="Verbose output of all cloning commands")
    parser.add_argument("-q",
                        "--quiet",
                        action='store_true',
                        dest="quiet",
                        help="No output to stdout")
    parser.add_argument(
        "-c",
        "--compress",
        action='store_true',
        dest="compress",
        help=
        "Creates a compressed file with all cloned repositories (cleans up location directory)"
    )
    parser.add_argument("-a",
                        "--attempts",
                        dest="attempts",
                        type=int,
                        default=1,
                        help="max. number of attempts to backup repository")
    parser.add_argument(
        '--mirror',
        action='store_true',
        help="Clone just bare repositories with git clone --mirror (git only)")
    parser.add_argument('--fetchlfs',
                        action='store_true',
                        help="Fetch LFS content after clone/pull (git only)")
    parser.add_argument('--with-wiki',
                        dest="with_wiki",
                        action='store_true',
                        help="Includes wiki")
    parser.add_argument('--http',
                        action='store_true',
                        help="Fetch via https instead of SSH")
    parser.add_argument(
        '--skip-password',
        dest="skip_password",
        action='store_true',
        help=
        "Ignores password prompting if no password is provided (for public repositories)"
    )
    parser.add_argument('--prune',
                        dest="prune",
                        action='store_true',
                        help="Prune repo on remote update")
    parser.add_argument('--ignore-repo-list',
                        dest='ignore_repo_list',
                        nargs='+',
                        type=str,
                        help="specify list of repo slug names to skip")
    args = parser.parse_args()
    location = args.location
    username = args.username
    password = args.password
    oauth_key = args.oauth_key
    oauth_secret = args.oauth_secret
    http = args.http
    max_attempts = args.attempts
    global _quiet
    _quiet = args.quiet
    global _verbose
    _verbose = args.verbose
    _mirror = args.mirror
    _fetchlfs = args.fetchlfs
    _with_wiki = args.with_wiki
    if _quiet:
        _verbose = False  # override in case both are selected

    if all((oauth_key, oauth_secret)):
        owner = args.team if args.team else username
    else:
        if not username:
            username = input('Enter bitbucket username: '******'Enter your bitbucket password: '******'Enter local location to backup to: ')
    location = os.path.abspath(location)

    # ok to proceed
    try:
        bb = bitbucket.BitBucket(
            username=username,
            password=password,
            oauth_key=oauth_key,
            oauth_secret=oauth_secret,
            verbose=_verbose,
        )
        user = bb.user(owner)
        repos = sorted(user.repositories(), key=lambda repo: repo.get("name"))
        if not repos:
            print(
                "No repositories found. Are you sure you provided the correct password"
            )
        for repo in repos:
            if args.ignore_repo_list and repo.get(
                    "slug") in args.ignore_repo_list:
                debug("ignoring repo %s with slug: %s" %
                      (repo.get("name"), repo.get("slug")))
                continue

            debug("Backing up [%s]..." % repo.get("name"), True)
            backup_dir = os.path.join(location, repo.get("slug"))

            for attempt in range(1, max_attempts + 1):
                try:
                    if not os.path.isdir(backup_dir):
                        clone_repo(repo,
                                   backup_dir,
                                   http,
                                   username,
                                   password,
                                   mirror=_mirror,
                                   with_wiki=_with_wiki,
                                   fetch_lfs=_fetchlfs)
                    else:
                        debug(
                            "Repository [%s] already in place, just updating..."
                            % repo.get("name"))
                        update_repo(repo,
                                    backup_dir,
                                    with_wiki=_with_wiki,
                                    prune=args.prune,
                                    fetch_lfs=_fetchlfs)
                except:
                    if attempt == max_attempts:
                        raise MaxBackupAttemptsReached(
                            "repo [%s] is reached maximum number [%d] of backup tries"
                            % (repo.get("name"), attempt))
                    debug(
                        "Failed to backup repository [%s], keep trying, %d attempts remain"
                        % (repo.get("name"), max_attempts - attempt))
                else:
                    break

        if args.compress:
            compress(repo, location)
        debug("Finished!", True)
    except HTTPError as err:
        if err.code == 401:
            exit("Unauthorized! Check your credentials and try again.",
                 22)  # EINVAL - Invalid argument
        else:
            exit("Connection Error! Bitbucket returned HTTP error [%s]." %
                 err.code)
    except URLError as e:
        exit("Unable to reach Bitbucket: %s." % e.reason,
             101)  # ENETUNREACH - Network is unreachable
    except (KeyboardInterrupt, SystemExit):
        exit(
            "Operation cancelled. There might be inconsistent data in location directory.",
            0)
    except MaxBackupAttemptsReached as e:
        exit("Unable to backup: %s" % e)
    except:
        if not _quiet:
            import traceback

            traceback.print_exc()
        exit("Unknown error.", 11)  # EAGAIN - Try again