Start Free trial

BackHub Blog

Three Ways to Back Up a Single File from Bitbucket

Ever spent time trying to problem-solve only to suddenly recall that you’ve solved a similar problem in the past? You go looking for the project that has the solution in your Bitbucket, find it, and realize the solution consists of about three files.

You don't want to download the entire repository, it's huge. How do you download only those three files without cloning the entire repository?

From experimental purposes to debugging to reusing the same file in a new project, there are many different reasons you might want to download individual files from Bitbucket. So let’s go over three ways you can download files directly from Bitbucket without having to clone the entire repository.

1. Manual Download From Your Browser

This is the easiest way to download files from your Bitbucket repository. You don't have to write any commands—it's a simple visual process. You only need to navigate to the right repository, look for the files, and download them one after the other.

Here’s a walk-through for manually backing up a file on Bitbucket:

  1. Log in to your Bitbucket account.
  2. Search for the repository where your target file is and click on it.

pjAkYvx

3. Click the ellipsis menu at the top right of the file window to receive a drop-down. Right-click Open raw and save the file with its file type using your browser’s Save Link As feature.

1JqRT9F

Advantages of Downloading Single Files Manually

It's a simple method. You just need to follow a few steps and click the right buttons, as described above.

Disadvantages of Downloading Single Files Manually

If you have to automate your processes, this will likely not be the option for you. It doesn't make sense to add files one after the other manually to your development/deployment pipeline. It's better off automated with a script. And if you have to do this for a lot of files, it’ll take a lot of time and monitoring.

2. curl

We can make this process faster with curl, a command-line tool that allows you to transfer data using one of the supported protocols: HTTP, DICT, FILE, GOPHER, HTTPS, IMAP, IMAPS, FTP, FTPS, or LDAP.

For this article, let’s use the HTTPS transfer protocol to download files from Bitbucket since HTTPS is enabled on the Bitbucket domain. If it's a private repository, you’ll be required to log in to access the files anyway, so log in with curl and set the repository, branch, and the filename of the file you’re looking to download on your terminal as shown below:

```bash
curl --user "username":"password" https://bitbucket.org/{username}/{repo}/raw/HEAD/{repo}/{filename.extension} -o {filename.extension}
```

We use the flag --user to specify user authentication credentials. This might not be necessary if your repository is public. Also, we use the -o (output) to specify the name of the output file.

Advantages of Downloading with curl

You’ll benefit from the curl method if you’re a command-line person. It's easy and fast—you’ll only need to replace a few parameters in the code and hit Enter on your command line. Bonus, curl comes preinstalled on Windows, Linux, and macOS.

Occasionally, you may need a file from another repository that’s continuously updated to make your own script work properly. In that case, this method can be a great way to download the script and add it to your workflow without having to do it manually.

Here’s a simple Node.js example of a script that automatically runs curl to download a file from your Bitbucket account using Child Process in Node.js:

```javascript
const cp = require('child_process');

let download = async function(uri, filename, username, password){
let command = `curl --user ${username}:${password} ${uri} -o ${filename}`;
let result = cp.execSync(command);
};

(async function test() {
await download('https://bitbucket.org/ezesundayeze/jobdispatcher/raw/HEAD/jobdispatcher/manage.py', 'manage.py', "username", "password")
})()

```

Disadvantages of Downloading with curl

Even as a one-liner, some devs will prefer a more visual process. In that case, you might prefer the manual process discussed in section 1.

3. WGET

Wget is a command-line utility like curl that allows you to retrieve data from a remote server. Some differences between the two are beyond the scope of this article, however, it's important to know that it can do the job in as much as curl can and in an even shorter way.

You only need to specify the path to the file and enter the username and password with the flag --ask-password to request the user to enter their password when they execute the command. You can also use --password to enter the password directly from the command line without being asked.

You'll also need to pass -- user to the command as shown in the code below. But if your repository is public, you won’t need the username and password arguments.

```bash

wget --user {username} --ask-password https://bitbucket.org/{username}/{repo}/raw/HEAD/{repo}/filename.extension

```

 

Advantages of Using Wget

It's fast and easy to use and can be integrated into your codebase easily with libraries such as node-wget, wget-improved, and Java-wget. It also requires fewer arguments as compared to curl.

Disadvantages of Using Wget

Wget does not come pre-installed with Windows, Linux, or macOS. You'll have to install it manually.

Conclusion

So we've covered three ways to download single files from a Bitbucket repository. From the manual process to using curl and Wget, you should have a solid idea of the advantages and disadvantages of each and whether or not they can make your work easier.

I feel like Bitbucket could have made this process a lot easier if they had introduced a simple API to allow developers to download individual files from any repository. But developers will always find new ways to solve challenges as they come up.

And if you need full repository backups, BackHub will support Bitbucket soon. Click here to be updated as soon as it arrives.