[Gitea](https://gitea.io) is a source code repository based on the version control system, [Git](https://git-scm.org). While there are several self-hosted solutions available such as GitLab and Gogs, Gitea has the benefit of being light-weight and easy to run on a small server.

However, having a small server, especially in the realm of VPSes, often means being limited on space. Thankfully, many hosting providers also offer additional storage in the form of block storage or networked file storage (NFS). This gives small and medium businesses the option to save money on smaller VPS hosts for their applications while not sacrificing storage.

With Gitea and the ability to decide where your source code is stored, it's easy to ensure that your projects and files have room to expand.

## Prerequisites

Before you get started, you will need the following:

* A server running Ubuntu 20.04
* An installation of Gitea. If you'd like a quick way to get started, you can see this tutorial on [Installing Gitea on Ubuntu 20.04 Using Docker](link-goes-here). <!-- TODO -->
* An additional Linux volume; for example, DigitalOcean's [block storage volumes](https://docs.digitalocean.com/products/volumes/) attached to a Droplet.

## Step 1 — Mounting a Linux Volume

A volume can take many different forms. It could be an NFS volume, which is storage available on the network provided via a file share. Another possibility is that it takes the form of block storage via a service such as DigitalOcean's Volumes or Amazon's EBS. In both cases, storage is mounted on a system using the `mount` command.

These volumes will be visible as a device, files which live in `/dev`. These files are how the kernel communicates with the storage devices themselves, not actually used for storage. In order to be able to store files on the storage device, you will need to **mount** them using the `mount` command.

First, you will need to create a **mount point** --- that is, a folder which will be associated with the device, such that data stored within it winds up stored on that device. Mount points for storage devices such as this typically live in the `/mnt` directory.

Create a mount point named `gitea` as you would create a normal directory using the `mkdir` command:

```command
sudo mkdir /mnt/gitea
```

From here, we can mount the device to that directory in order to use it to access that storage space. Use the `mount` command to mount the device:

```command
sudo mount -o defaults,noatime /dev/disk/by-id/<^>your_disk_id<^> /mnt/gitea
```

This command mounts the device specified by its ID to `/mnt/gitea`. The `-o` option specifies the options used when mounting. In this case, you are using the default options which allow for mounting a read/write file system, and the `noatime` option specifies that the kernel shouldn't update the last access time for files and directories on the device in order to be more efficient.

Now that you've mounted your device, it will stay mounted as long as the system is up and running. However, as soon as the system restarts, it will no longer be mounted (though the data will remain on the volume), so you will need to tell the system to mount the volume as soon as it starts using the `/etc/fstab` ('file systems table') file, which lists the available file systems and their mount points in a simple tab-delimited format.

Using `echo` and `tee`, add a new line to the end of `/etc/fstab`:

```command
echo '/dev/disk/by-id/<^>your_disk_id<^> /mnt/gitea ext4 defaults,nofail,noatime 0 0' | sudo tee /etc/fstab
```

This command appends the string `/dev/disk/by-uid/<^>your_disk_id<^>` to the fstab file and to your screen. As with the `mount` command above, it mounts the device onto the mount point using the `defaults`, `nofail`, and `noatime` options. The string `ext4` between the mount point and options specifies the file system type, ext4 in this case, though depending on your volume's file system type, it may be something like xfs or nfs; to check which type your volume uses, run the `mount` command with no options:

```command
mount
```

This will provide you with a line of output for every mounted file system. Since you just mounted yours, it will likely be the last on the list, and look something like this:

```
/dev/sda on /mnt/gitea type ext4 (rw,noatime,discard)
```

which shows that the file system type is `ext4`.

Once your changes have been made to `/etc/fstab`, the kernel will mount your Linux volume on boot.

<$>[note]
**Note:** Storage devices on Linux are very flexible and come in all different types, from a networked file system (NFS) to a plain old hard drive. To learn more about block storage and devices in Linux, you can read up more about storage concepts [here](https://www.digitalocean.com/community/tutorials/an-introduction-to-storage-terminology-and-concepts-in-linux).
<$>

## Step 2 — Configuring Gitea to Store Data on a Linux Volume

Gitea maintains all of its repositories in a central location. This includes repositories from all users and organizations. Unless configured otherwise, all information is kept in a single directory. This directory is named `data` in default installations. For the purposes of this demo, we will be using a version of Gitea running on Docker as in the tutorial linked above.

First of all, let's see what this data directory contains. You can do this by moving to the data directory and running the `ls` command. Using the `-l` format will tell us more information about the files:

```command
cd gitea
ls -l
```

This should provide a listing that looks something like this:

```
[secondary_label Output]
total 20
drwxr-xr-x  5 root  root  4096 Jun 23 22:34 ./
drwxrwxr-x  3 sammy sammy 4096 Jun 26 22:35 ../
drwxr-xr-x  5 git   git   4096 Jun 23 22:42 git/
drwxr-xr-x 12 git   git   4096 Jun 26 22:35 gitea/
drwx------  2 root  root  4096 Jun 23 22:34 ssh/
```

Let's break down the output of this command. It lists one file or directory per line. In this case, it lists five directories. The entry for `.` is a special entry that just means the current directory, and `..` stands for the directory one level up. You can see that the current directory is owned by the `root` user, which is the case in this instance because Docker runs as a privileged user, and the directory one level up is owned by `sammy`.

The `git` directory is important to us because it contains all of the repositories that we might want to store on a separate volume. Within it are two directories of note: the `repositories` directory contains the git repositories managed by Gitea sorted by user/organization, and the `lfs` directory contains data for Git's Large File Storage functionality. The `gitea` directory contains plenty of information that Gitea uses in the background, including archives of old repositories, as well the database that contains information such as users and repository information used by the web service. The `ssh` directory contains various SSH keypairs that are used in Gitea's operation.

Given that all of the information stored in this directory is important to us, we will want to include the entire directory's contents on our attached volume.

### Setting Up a New Installation of Gitea

If you are starting with a brand new installation of Gitea, you can specify where all of your information is stored during the configuration process. For example, if you are setting Gitea up using Docker Compose, you can map the volumes to your attached volume.

Open up the `docker-compose.yml` file with your preferred text editor. The following example uses `nano`. Search for the `volumes` entry in the compose file and modify the mapping on the left side of the `:` to point to appropriate locations on your Linux volume for the Gitea data directory:

```yaml
[label docker-compose.yml]
...

    volumes:
      - <^>/mnt/gitea<^>:/data
      - /home/git/.ssh/:/data/git/.ssh
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro

...
```

<$>[warning]
**Warning:** SSH servers look for the `.ssh` directory, which contains all of the SSH keys that Gitea will use, in the home directory of the Git user (`git`, in this case), so it is not advised to move the mount for this Docker volume. In order to have this location backed up on your Linux volume, it would be best to use another solution such as a `cron` job to back up the directory. For more information on using `cron` to manage scheduled tasks, see [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804).
<$>

When you run `docker-compose` and Gitea installs, it will use your Linux volume to store its data.

If you are not using Docker volumes to manage the locations of your data --- for example, if you are installing Gitea on your server via the binary releases per [these instructions from Gitea](https://docs.gitea.io/en-us/install-from-binary/) --- then you will need to modify the locations within the configuration file (usually `/etc/gitea/app.ini`) when you are setting all of the values. For instance, you might set them as follows:

```ini
[label app.ini]
...

# If you are using SQLite for your database, you will need to change the PATH
# variable in this section
[database]
...
PATH = <^>/mnt/gitea<^>/gitea.db

[server]
...
LFS_CONTENT_PATH = <^>/mnt/gitea<^>/lfs

[repository]
ROOT = <^>/mnt/gitea<^>/gitea-repositories

...
```

<$>[note]
**Note:** Ensure that your git user has write access to this location. You can read up on Linux permissions [here](https://www.digitalocean.com/community/tutorials/an-introduction-to-linux-permissions).
<$>

### Moving an Existing Installation of Gitea

If you already have an instance of Gitea installed and running, you will still be able to store your data on a separate Linux volume, but it will require some care in ensuring that all of your data remains both safe and accessible to Gitea.

<$>[note]
**Note:** As with any operation involving your data, it is important to ensure that you have an up-to-date backup of everything. In this case, this means a back up of all of the files in your data directory (the SSH files, the `gitea-repositories` and `lfs` directories, the database, and so on) to a safe location. 
<$>

There are two options for moving your data to a new volume. The first way is to copy your Gitea data to a secondary location, then turn the original location into a mount point for your Linux volume. When you copy your data back into that location, you will be copying it onto that volume, and no changes will be required within Gitea itself; it will simply continue as it did before. If, however, you do not want to mount the entire device to that destination (for example, if your Gitea data will not be the only thing on that volume), then a second option is to move all of your Gitea data to a new location on that volume and instruct Gitea itself to use that location.

No matter which option you choose, first, stop the Gitea web service.

If you are using Gitea installed via Docker Compose, use `docker-compose` to stop the service. While inside the same directory containing the `docker-compose.yml` file, run:

```command
docker-compose down
```

If you have installed it as a Linux service, use the `systemctl` command:

```command
sudo systemctl stop gitea
```

Once Gitea has been stopped, move the entire contents of the data directory to the mount point made in step 1:

```command
mv * /mnt/gitea
```

Ensure that all files have been moved by listing all of the current directory's contents:

```command
ls -la
```

You should only see the current and parent directory entries. The result should look something like this:

```
total 8
drwxrwxr-x 2 mscottclary mscottclary 4096 Jun 27 13:56 ./
drwxr-xr-x 7 mscottclary mscottclary 4096 Jun 27 13:56 ../
```

Once all of the data has been moved, change to that directory:

```command
cd /mnt/gitea
```

Using `ls`, ensure that everything looks correct. As before, you should see the `ssh`, `git`, and `gitea` directories. If you are using SQLite as a database to manage Gitea, you should also see a file named `gitea.db` in the `gitea` directory.

When you're sure that all data has been moved, it's time to mount the Linux volume to the data directory. 

First, move to the parent directory of the data directory.

```command
cd <^>/home/sammy/gitea/..<^>
```

As before, use the `mount` command, but this time, use the directory you just emptied as the destination:

```command
sudo mount -o defaults,noatime /dev/disk/by-id/<^>your_disk_id<^> gitea
```

Now, when you look inside that directory, you should see all of your files in place:

```command
ls -la gitea
```

This command should output the expected information. Note that, depending on your volume's file system type, you may see an additional directory named `lost+found`; this is normal and part of everyday file system use.

```
total 36
drwxr-xr-x  6 root        root         4096 Jun 27 13:58 ./
drwxrwxr-x  3 mscottclary mscottclary  4096 Jun 27 02:23 ../
drwxr-xr-x  5 git         git          4096 Jun 23 22:42 git/
drwxr-xr-x 12 git         git          4096 Jun 27 00:00 gitea/
drwx------  2 root        root        16384 Jun 27 03:46 lost+found/
drwx------  2 root        root         4096 Jun 23 22:34 ssh/
```

As mentioned, if you would like Gitea to use a directory within the Linux volume, there is an additional step you need to complete before bringing Gitea back up. For example, say that you want to use a folder named `scm` on your volume mounted on `/mnt/gitea`. After moving all of your Gitea data to `/mnt/gitea/scm`, you will need to create a symbolic link from your old data directory to the new one. For this you will use the `ln` command:

```command
sudo ln -s /mnt/gitea/scm gitea
```

At this point, you can restart Gitea. If you are using Gitea as a Linux service, run:

```command
sudo systemctl restart gitea
```

If you are running Gitea as a Docker container using Docker Compose, run:


```command
docker-compose up -d
```

Now that everything is up and running, visit your Gitea instance in the browser and ensure that everything works as expected. You should be able to create new objects in Gitea such as repositories, issues, and so on. If you set Gitea up with an SSH shim, you should also be able to check out and push to repositories using `git clone` and `git push`.

## Conclusion

In this tutorial, you moved all of your Gitea data to a Linux volume. Volumes such as these are very flexible and provide many benefits.such as allowing you to store all of your data on larger disks, RAID volumes, networked file systems, or using block storage such as DigitalOcean Volumes or Amazon EBS to reduce storage expenses. It also allows you to snapshot entire disks for backup so that you can restore their contents in event of a catastrophic failure.
