Business, Technology, Tutorial

How to Use Bitbucket as a Backup Tool for Upgrades and Changes to Server

I had never heard of Bitbucket before to be honest. Turns out it’s a kind of ‘private repository thing’. And you can get a free account to start. Pretty nice. I was asked to ‘put your files into Bitbucket’ by a development company. I didn’t know how so I figured I’d just figure it out and then make a blog so others can benefit from it.

Step 1 – Create a Bitbucket Account

That’s pretty easy. It’s free. Go to Bitbucket, go through the usual process, create an account.

Step 2 – Create a Project

Not sure if you have to do this, probably you don’t, but it keeps things clean – especially if you plan on adding more repositories later. Give it a name, a fancy description, and keep it on ‘private’ mode unless you plan on sharing with the world. Completing this step will bring you to a page where you can conveniently create a repository.

Step 3 – Create a Repository

You can start this from the last prompt page or from the drop-down on the top menu of your Bitbucket space. Give it a Repository name, and you can keep the rest as their default settings, especially if you don’t actually know what you’re doing like I didn’t… Hit the ‘Create Repository button”

Step 4 – Moving the Files

So now we need to run this workflow:

  • Pull the blank repository we just made down to our local machine so that we can:
  • a) dump the files from our server in there and then,
  • b) push the files back up to the Bitbucket repository later.

So let’s break this down into smaller sub-steps as we do this.

Step 4a – Make the Bitbucket App Password

In order for you or your development team to run git commands on your Bitbucket repository, you’ll need an ‘app password’ with permissions to do just this. The support page gives detailed instructions, but in short, if all you want to do is share the right for someone to access the repository and make changes, just follow the directions in the link above, in order to understand what permissions to give, and then follow these instructions in order to understand the steps to actually create the app password. Save the password in a secure place like in a keepass manager.

Step 4b – Git Clone the Blank Bitbucket Repo

  1. Make sure that you have git installed on your ubuntu machine. If you don’t, sudo apt install git should do it
  2. Next, create a new directory on your local machine where the bitbucket repository will be.
  3. Next, in your terminal cd into that directory
  4. Go to Bitbucket into your repository and at the top right click the ‘clone’ button and the ‘copy’ icon to copy the git clone link to your clipboard.
  5. Back in your terminal, run the command to clone the repository git clone "<your-bit-bucket-clone-link>" of course replacing the contents of <> with your link from Bitbucket
  6. You’ll be prompted for a password. This is the one from Step 4a above (app password)

Congrats, your blank repo is cloned – nothing more exciting and meaningful than doubling nothing…

Step 4c – Pull Your Server Stuff Into Your Git Directory

Using sftp (and there could be a better way to do this…it was slow for me but worked…) we will pull down the directory from our server.

  1. cd into your directory where you have cloned your repository (if you aren’t still there)
  2. sftp user@123.123.1.123 (sudo apt install it if you don’t have it of course)
  3. Find your repo directory with cd or run the path to begin pulling it down. In my case it was located at /var/www/html/ so I ran: get -R /var/www/html/mydirectory

Tip 1: Don’t run this if you have a lot of files and not a lot of time to wait while your computer does this.

Congrats. After this is done, you now have the files (not database) from your server on your local machine

Step 4d – Git Ignoring

If you have certain directories not required, then you can add those to your .gitignore file. Bitbucket’s default gitignore file is full of examples so you will want to delete the ones they give to begin, just in case, and add the ones you need.

  1. cd to your git zone (if you aren’t already there)
  2. ls -al to make sure indeed your .gitignore file is there
  3. nano .gitignore
  4. adjust the .gitignore file
  5. control x, Y, enter to leave

Nice. You’ve ignored something. Where else do you progress by ignoring?

Step 4e – Git Configging

If you have just installed git like I did and not configured it, after you try to proceed to the next section it will require you do this step, so you might as well do this step now – before you get there:

  1. Set your ‘global’ email address for git commits:
    git config --global user.email "you@example.com"
  2. Set your ‘global’ user name for git commits:
    git config --global user.name "Your Name"

Step 4f – Pushing the Server Stuff Up to Bitbucket

Now we will move this all back to Bitbucket. This guide seems amazing if you need git help like I did…

  1. Make sure you are still in the same git directory you made
  2. Tell git to add everything (files and directories and subdirectories) and its with the magical dot (.): git add .
  3. Commit them to the repo and comment so accordingly: git commit -m "added all files"

Tip 2: Don’t run this command if you have a lot of files and not a lot of time to wait while your computer does this. It might seem like TIP 1 above, but…

  1. Now let’s move it from the staging area up to Bitbucket: git push origin main

NOTE: Typically branch is called ‘master’ in github, etc. But if you didn’t change the bitbucket defaults somewhere around Step 3 above, it will be called ‘main’ here. This threw me off a bit, so just mentioning it. So main = master in functionality.

Good job. Now your server stuff is living in Bitbucket.

Step 4g – Sharing with Collaborators

In my case, I now need to share this repository with a developer and his team.

  1. Add user to Bitbucket workspace:
  • Go to the ‘Settings’ cogwheel, top right
  • Select the ‘User management’ option
  • Add user’s email. If they are BB user, they can just accept invite, if not have to make account (they will be prompted to do so)

2. Add user to repository:

  • Make sure in ‘Bitbucket’ and not Atlassian part of website (top left icon allows switching)
  • “Repository” menu item on top menu (third from left)
  • SElect repository you want to add a user to
  • Click ‘invite’ button top right
  • Click ‘Add users or groups’ button
  • Start typing users name. If you invited them and they accepted it should show.
  • Select them and appropriate permissions

Step 5 – Database

I will not actually include any instructions here since there are tonnes of tutorials online about backing up MySQL databases, but I wanted to make sure to add it as a bold reminder that you should make sure not to forget this in your backup process.

Concluding Words and Wise Statements

None. I feel concluded. I have no further wisdom.

Tagged , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *