zk_html/work/gitea-2.html

205 lines
20 KiB
HTML

<!doctype html>
<html>
<head>
<title>Zk | gitea-2</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Gentium+Plus&family=Lato&family=Ubuntu+Monodisplay=swap" />
<link rel="stylesheet" type="text/css" href="/style.css" />
<meta name="viewport" content="width=device-width" />
<meta charset="utf-8" />
</head>
<body>
<main>
<header>
<h1>Zk | gitea-2</h1>
</header>
<article class="content">
<p><a href="https://gitea.io">Gitea</a> is a source code repository based on the version control system, <a href="https://git-scm.org">Git</a>. 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.</p>
<p>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.</p>
<p>With Gitea and the ability to decide where your source code is stored, it&rsquo;s easy to ensure that your projects and files have room to expand.</p>
<h2 id="prerequisites">Prerequisites</h2>
<p>Before you get started, you will need the following:</p>
<ul>
<li>A server running Ubuntu 20.04</li>
<li>An installation of Gitea. If you&rsquo;d like a quick way to get started, you can see this tutorial on <a href="link-goes-here.html">Installing Gitea on Ubuntu 20.04 Using Docker</a>. <!-- TODO --></li>
<li>An additional Linux volume; for example, DigitalOcean&rsquo;s <a href="https://docs.digitalocean.com/products/volumes/">block storage volumes</a> attached to a Droplet.</li>
</ul>
<h2 id="step-1-mounting-a-linux-volume">Step 1 — Mounting a Linux Volume</h2>
<p>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&rsquo;s Volumes or Amazon&rsquo;s EBS. In both cases, storage is mounted on a system using the <code>mount</code> command.</p>
<p>These volumes will be visible as a device, files which live in <code>/dev</code>. 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 <strong>mount</strong> them using the <code>mount</code> command.</p>
<p>First, you will need to create a <strong>mount point</strong> &mdash; 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 <code>/mnt</code> directory.</p>
<p>Create a mount point named <code>gitea</code> as you would create a normal directory using the <code>mkdir</code> command:</p>
<div class="codehilite"><pre><span></span><code>sudo mkdir /mnt/gitea
</code></pre></div>
<p>From here, we can mount the device to that directory in order to use it to access that storage space. Use the <code>mount</code> command to mount the device:</p>
<div class="codehilite"><pre><span></span><code>sudo mount -o defaults,noatime /dev/disk/by-id/&lt;^&gt;your_disk_id&lt;^&gt; /mnt/gitea
</code></pre></div>
<p>This command mounts the device specified by its ID to <code>/mnt/gitea</code>. The <code>-o</code> 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 <code>noatime</code> option specifies that the kernel shouldn&rsquo;t update the last access time for files and directories on the device in order to be more efficient.</p>
<p>Now that you&rsquo;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 <code>/etc/fstab</code> (&lsquo;file systems table&rsquo;) file, which lists the available file systems and their mount points in a simple tab-delimited format.</p>
<p>Using <code>echo</code> and <code>tee</code>, add a new line to the end of <code>/etc/fstab</code>:</p>
<div class="codehilite"><pre><span></span><code>echo &#39;/dev/disk/by-id/&lt;^&gt;your_disk_id&lt;^&gt; /mnt/gitea ext4 defaults,nofail,noatime 0 0&#39; | sudo tee /etc/fstab
</code></pre></div>
<p>This command appends the string <code>/dev/disk/by-uid/&lt;^&gt;your_disk_id&lt;^&gt;</code> to the fstab file and to your screen. As with the <code>mount</code> command above, it mounts the device onto the mount point using the <code>defaults</code>, <code>nofail</code>, and <code>noatime</code> options. The string <code>ext4</code> between the mount point and options specifies the file system type, ext4 in this case, though depending on your volume&rsquo;s file system type, it may be something like xfs or nfs; to check which type your volume uses, run the <code>mount</code> command with no options:</p>
<div class="codehilite"><pre><span></span><code>mount
</code></pre></div>
<p>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:</p>
<div class="codehilite"><pre><span></span><code>/dev/sda on /mnt/gitea type ext4 (rw,noatime,discard)
</code></pre></div>
<p>which shows that the file system type is <code>ext4</code>.</p>
<p>Once your changes have been made to <code>/etc/fstab</code>, the kernel will mount your Linux volume on boot.</p>
<p>&lt;$&gt;[note]
<strong>Note:</strong> 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 <a href="https://www.digitalocean.com/community/tutorials/an-introduction-to-storage-terminology-and-concepts-in-linux">here</a>.
&lt;$&gt;</p>
<h2 id="step-2-configuring-gitea-to-store-data-on-a-linux-volume">Step 2 — Configuring Gitea to Store Data on a Linux Volume</h2>
<p>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 <code>data</code> 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.</p>
<p>First of all, let&rsquo;s see what this data directory contains. You can do this by moving to the data directory and running the <code>ls</code> command. Using the <code>-l</code> format will tell us more information about the files:</p>
<div class="codehilite"><pre><span></span><code>cd gitea
ls -l
</code></pre></div>
<p>This should provide a listing that looks something like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">[secondary_label Output]</span><span class="w"></span>
<span class="na">total 20</span><span class="w"></span>
<span class="na">drwxr-xr-x 5 root root 4096 Jun 23 22:34 ./</span><span class="w"></span>
<span class="na">drwxrwxr-x 3 sammy sammy 4096 Jun 26 22:35 ../</span><span class="w"></span>
<span class="na">drwxr-xr-x 5 git git 4096 Jun 23 22:42 git/</span><span class="w"></span>
<span class="na">drwxr-xr-x 12 git git 4096 Jun 26 22:35 gitea/</span><span class="w"></span>
<span class="na">drwx------ 2 root root 4096 Jun 23 22:34 ssh/</span><span class="w"></span>
</code></pre></div>
<p>Let&rsquo;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 <code>.</code> is a special entry that just means the current directory, and <code>..</code> stands for the directory one level up. You can see that the current directory is owned by the <code>root</code> user, which is the case in this instance because Docker runs as a privileged user, and the directory one level up is owned by <code>sammy</code>.</p>
<p>The <code>git</code> 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 <code>repositories</code> directory contains the git repositories managed by Gitea sorted by user/organization, and the <code>lfs</code> directory contains data for Git&rsquo;s Large File Storage functionality. The <code>gitea</code> 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 <code>ssh</code> directory contains various SSH keypairs that are used in Gitea&rsquo;s operation.</p>
<p>Given that all of the information stored in this directory is important to us, we will want to include the entire directory&rsquo;s contents on our attached volume.</p>
<h3 id="setting-up-a-new-installation-of-gitea">Setting Up a New Installation of Gitea</h3>
<p>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.</p>
<p>Open up the <code>docker-compose.yml</code> file with your preferred text editor. The following example uses <code>nano</code>. Search for the <code>volumes</code> entry in the compose file and modify the mapping on the left side of the <code>:</code> to point to appropriate locations on your Linux volume for the Gitea data directory:</p>
<div class="codehilite"><pre><span></span><code><span class="p p-Indicator">[</span><span class="nv">label docker-compose.yml</span><span class="p p-Indicator">]</span><span class="w"></span>
<span class="nn">...</span><span class="w"></span>
<span class="w"> </span><span class="nt">volumes</span><span class="p">:</span><span class="w"></span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">&lt;^&gt;/mnt/gitea&lt;^&gt;:/data</span><span class="w"></span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/home/git/.ssh/:/data/git/.ssh</span><span class="w"></span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/etc/timezone:/etc/timezone:ro</span><span class="w"></span>
<span class="w"> </span><span class="p p-Indicator">-</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">/etc/localtime:/etc/localtime:ro</span><span class="w"></span>
<span class="nn">...</span><span class="w"></span>
</code></pre></div>
<p>&lt;$&gt;[warning]
<strong>Warning:</strong> SSH servers look for the <code>.ssh</code> directory, which contains all of the SSH keys that Gitea will use, in the home directory of the Git user (<code>git</code>, 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 <code>cron</code> job to back up the directory. For more information on using <code>cron</code> to manage scheduled tasks, see <a href="https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804">this tutorial</a>.
&lt;$&gt;</p>
<p>When you run <code>docker-compose</code> and Gitea installs, it will use your Linux volume to store its data.</p>
<p>If you are not using Docker volumes to manage the locations of your data &mdash; for example, if you are installing Gitea on your server via the binary releases per <a href="https://docs.gitea.io/en-us/install-from-binary/">these instructions from Gitea</a> &mdash; then you will need to modify the locations within the configuration file (usually <code>/etc/gitea/app.ini</code>) when you are setting all of the values. For instance, you might set them as follows:</p>
<div class="codehilite"><pre><span></span><code><span class="k">[label app.ini]</span><span class="w"></span>
<span class="na">...</span><span class="w"></span>
<span class="c1"># If you are using SQLite for your database, you will need to change the PATH</span><span class="w"></span>
<span class="c1"># variable in this section</span><span class="w"></span>
<span class="k">[database]</span><span class="w"></span>
<span class="na">...</span><span class="w"></span>
<span class="na">PATH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&lt;^&gt;/mnt/gitea&lt;^&gt;/gitea.db</span><span class="w"></span>
<span class="k">[server]</span><span class="w"></span>
<span class="na">...</span><span class="w"></span>
<span class="na">LFS_CONTENT_PATH</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&lt;^&gt;/mnt/gitea&lt;^&gt;/lfs</span><span class="w"></span>
<span class="k">[repository]</span><span class="w"></span>
<span class="na">ROOT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&lt;^&gt;/mnt/gitea&lt;^&gt;/gitea-repositories</span><span class="w"></span>
<span class="na">...</span><span class="w"></span>
</code></pre></div>
<p>&lt;$&gt;[note]
<strong>Note:</strong> Ensure that your git user has write access to this location. You can read up on Linux permissions <a href="https://www.digitalocean.com/community/tutorials/an-introduction-to-linux-permissions">here</a>.
&lt;$&gt;</p>
<h3 id="moving-an-existing-installation-of-gitea">Moving an Existing Installation of Gitea</h3>
<p>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.</p>
<p>&lt;$&gt;[note]
<strong>Note:</strong> 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 <code>gitea-repositories</code> and <code>lfs</code> directories, the database, and so on) to a safe location.
&lt;$&gt;</p>
<p>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.</p>
<p>No matter which option you choose, first, stop the Gitea web service.</p>
<p>If you are using Gitea installed via Docker Compose, use <code>docker-compose</code> to stop the service. While inside the same directory containing the <code>docker-compose.yml</code> file, run:</p>
<div class="codehilite"><pre><span></span><code>docker-compose down
</code></pre></div>
<p>If you have installed it as a Linux service, use the <code>systemctl</code> command:</p>
<div class="codehilite"><pre><span></span><code>sudo systemctl stop gitea
</code></pre></div>
<p>Once Gitea has been stopped, move the entire contents of the data directory to the mount point made in step 1:</p>
<div class="codehilite"><pre><span></span><code>mv * /mnt/gitea
</code></pre></div>
<p>Ensure that all files have been moved by listing all of the current directory&rsquo;s contents:</p>
<div class="codehilite"><pre><span></span><code>ls -la
</code></pre></div>
<p>You should only see the current and parent directory entries. The result should look something like this:</p>
<div class="codehilite"><pre><span></span><code>total 8
drwxrwxr-x 2 mscottclary mscottclary 4096 Jun 27 13:56 ./
drwxr-xr-x 7 mscottclary mscottclary 4096 Jun 27 13:56 ../
</code></pre></div>
<p>Once all of the data has been moved, change to that directory:</p>
<div class="codehilite"><pre><span></span><code>cd /mnt/gitea
</code></pre></div>
<p>Using <code>ls</code>, ensure that everything looks correct. As before, you should see the <code>ssh</code>, <code>git</code>, and <code>gitea</code> directories. If you are using SQLite as a database to manage Gitea, you should also see a file named <code>gitea.db</code> in the <code>gitea</code> directory.</p>
<p>When you&rsquo;re sure that all data has been moved, it&rsquo;s time to mount the Linux volume to the data directory. </p>
<p>First, move to the parent directory of the data directory.</p>
<div class="codehilite"><pre><span></span><code>cd &lt;^&gt;/home/sammy/gitea/..&lt;^&gt;
</code></pre></div>
<p>As before, use the <code>mount</code> command, but this time, use the directory you just emptied as the destination:</p>
<div class="codehilite"><pre><span></span><code>sudo mount -o defaults,noatime /dev/disk/by-id/&lt;^&gt;your_disk_id&lt;^&gt; gitea
</code></pre></div>
<p>Now, when you look inside that directory, you should see all of your files in place:</p>
<div class="codehilite"><pre><span></span><code>ls -la gitea
</code></pre></div>
<p>This command should output the expected information. Note that, depending on your volume&rsquo;s file system type, you may see an additional directory named <code>lost+found</code>; this is normal and part of everyday file system use.</p>
<div class="codehilite"><pre><span></span><code>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/
</code></pre></div>
<p>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 <code>scm</code> on your volume mounted on <code>/mnt/gitea</code>. After moving all of your Gitea data to <code>/mnt/gitea/scm</code>, you will need to create a symbolic link from your old data directory to the new one. For this you will use the <code>ln</code> command:</p>
<div class="codehilite"><pre><span></span><code>sudo ln -s /mnt/gitea/scm gitea
</code></pre></div>
<p>At this point, you can restart Gitea. If you are using Gitea as a Linux service, run:</p>
<div class="codehilite"><pre><span></span><code>sudo systemctl restart gitea
</code></pre></div>
<p>If you are running Gitea as a Docker container using Docker Compose, run:</p>
<div class="codehilite"><pre><span></span><code>docker-compose up -d
</code></pre></div>
<p>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 <code>git clone</code> and <code>git push</code>.</p>
<h2 id="conclusion">Conclusion</h2>
<p>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.</p>
</article>
<footer>
<p>Page generated on 2023-05-10</p>
</footer>
</main>
<script type="text/javascript">
document.querySelectorAll('.tag').forEach(tag => {
let text = tag.innerText;
tag.innerText = '';
tag.innerHTML = `<a href="/tags.html#${text}">${text}</a>`;
});
</script>
</body>
</html>