hugo 能做web開發嗎_如何自托管Hugo Web應用

hugo 能做web開發嗎

After hosting with Netlify for a few years, I decided to head back to self hosting. There are a few reasons for that, but the main reasoning was that I had more control over how things worked.

在Netlify托管了幾年之后,我決定回到自助托管。 這樣做有幾個原因,但是主要的原因是我對事情的運作方式有更多的控制權。

In this post, I'll show you my workflow for deploying my Hugo generated site (www.jaredwolff.com).

在這篇文章中,我將向您展示部署Hugo生成的網站( www.jaredwolff.com )的工作流程。

Instead of using what most people would go for, I'll be doing all of this using a FreeBSD Jails-based server. Plus I'll show you some tricks I've learned over the years on bulk image resizing and more.

我將不使用大多數人想要的東西,而是使用基于FreeBSD Jails的服務器來完成所有這些工作。 另外,我將向您展示我多年來在批量圖像調整大小上學到的一些技巧。

Let's get to it.

讓我們開始吧。

在哪里托管? (Where to host?)

If you want to host your own service, you'll need a server. That's where a VPS provider like Digital Ocean or Vultr comes in. I've been a fan and have used Digital Ocean for a while now.

如果要托管自己的服務,則需要一臺服務器。 那就是VPS提供商(例如Digital Ocean或Vultr)加入的地方。我一直是粉絲,并且使用Digital Ocean已有一段時間了。

To set up a new server here are some steps:

要設置新服務器,請執行以下步驟:

  1. Login to Digital Ocean. If you don’t have Digital Ocean and would like to support this blog click here to create an account.

    登錄到數字海洋。 如果您沒有Digital Ocean并想支持此博客,請單擊此處創建一個帳戶。

  2. Go to Account Settings -> Security and make sure you have an SSH key setup.

    轉到Account Settings -> Security ,并確保您具有SSH密鑰設置。

  3. Create a new FreeBSD droplet. Make sure you use the UFS version

    Create Droplet

    創建一個新的FreeBSD Droplet。 確保使用UFS版本

    創建液滴
  4. Make sure you select the $5 a month plan. For simple installs, this is more than enough!

    $5 Plan

    確保選擇每月5美元的計劃。 對于簡單的安裝,這已綽綽有余!

  5. Make sure your SSH key is selected

    Select SSH key

    確保選擇了您的SSH密鑰

  6. Finally click that green Create Droplet button!

    Create droplet

    最后,點擊綠色的創建液滴按鈕!

  7. SSH in once you’re done: ssh root@<yourserverip>

    完成后即可使用SSH: ssh root@<yourserverip>

使用Bastille設置FreeBSD服務器 (Setting up your FreeBSD server with Bastille)

images/bastille.png

Up until recently, everything was running on a Docker based platform using Exoframe. It was easy and almost brainless.

直到最近,一切都使用Exoframe在基于Docker的平臺上運行。 這很容易,幾乎沒有頭腦。

The downside was that Docker takes up wayyyy too many resources. Plus managing files within a Docker container is as much or more work than hosting it natively. Oh, and have you checked how much space Docker has been using on your machine lately? On my development machine its was about 19GB of space. 😬

缺點是Docker占用了太多的資源。 另外,在Docker容器中管理文件比在本地托管它要多得多或更多。 哦,您是否檢查了Docker最近在您的計算機上使用了多少空間? 在我的開發機器上,它大約有19GB的空間。 😬

So what's the alternative?

那有什么選擇呢?

FreeBSD Jails using Bastille.

使用巴士底獄的FreeBSD Jails。

I've been playing with Bastille for a few months now. The more I use it, the more it makes 100% sense.

我已經和巴士底獄在一起玩了幾個月。 我使用的越多,它就越具有100%的意義。

Bastille allows you to create (now) portable lightweight FreeBSD based jails. These jails are "containers" that have virtually no overhead. There's no daemon (the operating system is the "daemon"!). Plus, jails are secure compared to the can of worms that Docker is. Yes, you may have to compile and port some utilities. Most though are already supported in FreeBSD's package manager pkg.

Bastille允許您創建(現在)基于FreeBSD的輕量級便攜式監獄。 這些監獄實際上是沒有任何開銷的“容器”。 沒有守護程序(操作系統是“守護程序”!)。 另外,與Docker相比,監獄更安全。 是的,您可能必須編譯并移植一些實用程序。 FreeBSD的軟件包管理器pkg已經支持了大多數。

In this section you'll learn how to get a jail running with caddy so you can securely host your site.

在本部分中,您將學習如何使用caddy進行監禁,以便安全地托管您的網站。

Let's keep the momentum going!

讓我們繼續前進!

Once you get the IP address for your server, you should login:

獲取服務器的IP地址后,您應該登錄:

ssh root@123.456.789.10

You should get a MOTD message and an sh prompt. Woo!

您應該收到一條MOTD消息和一個sh提示。 !

FreeBSD 12.1-RELEASE-p2 GENERICWelcome to FreeBSD!
...#

Let's install a few important bits using pkg (FreeBSD's package manager):

讓我們使用pkg (FreeBSD的軟件包管理器)安裝一些重要的部分:

pkg install restic rsync bastille

We'll be using restic for backups, rsync for transferring files and bastille for jail setup.

我們將使用restic進行備份,使用rsync傳輸文件,使用bastille進行監獄設置。

You also have to set up some static routes in your pf.conf. Here's an example of mine:

您還必須在pf.conf設置一些靜態路由。 這是我的一個例子:

ext_if="vtnet0"# Caddy related
caddy_addr=10.10.2.20set block-policy return
scrub in on $ext_if all fragment reassemble
set skip on lotable <jails> persist
nat on $ext_if from <jails> to any -> $ext_if# container routes
rdr pass inet proto tcp from any to port 80 -> $caddy_addr port 8880
rdr pass inet proto tcp from any to port 443 -> $caddy_addr port 4443# Enable dynamic rdr (see below)
rdr-anchor "rdr/*"block in all
pass out quick modulate state
antispoof for $ext_if inet
pass in inet proto tcp from any to any port ssh flags S/SA keep state

This is a standard pf.conf file for bastille. Make sure you edit caddy_addr to the IP you chose.

這是bastille的標準pf.conf文件。 確保將caddy_addr編輯為caddy_addr的IP。

Now let's start the firewall. You will get kicked out of your ssh session:

現在讓我們啟動防火墻。 您將被踢出ssh會話:

sysrc pf_enable="YES"
service pf start

Then let's get some bastille configuration out of the way:

然后讓我們進行一些bastille配置:

# set up bastille networking
sysrc cloned_interfaces+=lo1
sysrc ifconfig_lo1_name="bastille0"
service netif cloneup# bootstrap the base jail and start bastille
bastille bootstrap 12.1-RELEASE update
sysrc bastille_enable="YES"
service bastille start

This will set up your networking, and fetch the latest default base jail you'll use later.

這將建立您的網絡,并獲取您以后將使用的最新的默認基礎監獄。

Next, let's set up the jail:

接下來,讓我們設置監獄:

bastille create caddy 12.1-STABLE 10.10.2.20
bastille start caddy

Then install caddy

然后安裝caddy

#install the binary
fetch https://github.com/caddyserver/caddy/releases/download/v1.0.4/caddy_v1.0.4_freebsd_amd64.tar.gz
tar xvf caddy_v1.0.4_freebsd_amd64.tar.gz caddy
bastille cp caddy caddy /usr/local/bin/
rm caddy#create the caddy user
bastille cmd caddy pw useradd caddy -m -s /usr/sbin/nologin#install ca root file
bastille pkg caddy install ca_root_nss

When installing ca_root_nss , pkg will have to initialize. Accept the prompts. Once you're done here we'll move on to the next step!

安裝ca_root_nss ,必須初始化pkg 。 接受提示。 完成這里的操作后,我們將繼續下一步!

Once installation is complete, we should also configure caddy to start on boot. The easiest way to do that is use this rc.d script:

安裝完成后,我們還應該將caddy配置為在啟動時啟動。 最簡單的方法是使用以下rc.d腳本:

#!/bin/sh# $FreeBSD: head/net/caddy/files/caddy.in 452063 2017-10-14 12:58:24Z riggs $
#
# PROVIDE: caddy
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# caddy_enable (bool):	Set to NO by default.
#				Set it to YES to enable caddy.
# caddy_user (user):		Set user to run caddy.
#				Default is "caddy".
# caddy_group (group):	Set group to run caddy.
#				Default is "caddy".
# caddy_conf (path):		Path to caddy configuration file.
#				Default is /usr/local/etc/caddyfile.conf. /etc/rc.subrname=caddy
rcvar=caddy_enableload_rc_config $name: ${caddy_enable:="NO"}
: ${caddy_user:="caddy"}
: ${caddy_group:="caddy"}
: ${caddy_conf:="/usr/local/etc/caddyfile.conf"}
: ${caddy_log:="/home/caddy/caddy.log"}
: ${caddy_env:="CADDYPATH=/home/caddy/"}
: ${caddy_https_port:="4443"}
: ${caddy_http_port:="8880"}pidfile="/var/run/caddy.pid"
procname="/usr/local/bin/caddy"
command="/usr/sbin/daemon"
command_args="-f -p ${pidfile} /usr/bin/env ${caddy_env} ${procname} -agree -http-port ${caddy_http_port}  -https-port ${caddy_https_port} -conf=${caddy_conf} -log=${caddy_log} ${caddy_args}"
extra_commands="reload"start_precmd=caddy_startprecmd
reload_cmd=caddy_reloadcmdcaddy_startprecmd()
{if [ ! -e ${pidfile} ]; theninstall -o ${caddy_user} -g ${caddy_group} /dev/null ${pidfile};fi
}caddy_reloadcmd()
{kill -s USR1 $(cat ${pidfile})
}run_rc_command "$1"

Remove the caddy executable if you haven't already. Then create a new file with vi. This will be your rc.d script!

如果還沒有,請刪除caddy可執行文件。 然后使用vi創建一個新文件。 這將是您的rc.d腳本!

vi caddy

Then paste the contents of the above script in there, save and exit.

然后將上面腳本的內容粘貼到其中,保存并退出。

Make sure the file is executable by using chmod and copy to the Caddy container.

通過使用chmod確保文件是可執行文件,并將其復制到Caddy容器中。

chmod +x caddy
bastille cp caddy caddy /usr/local/etc/rc.d/

Finally, we'll need a Caddyfile. Here's an example of one:

最后,我們需要一個Caddyfile。 這是一個例子:

stage.jaredwolff.com {tls hello@jaredwolff.comlog /home/caddy/stage.jaredwolff.com.logroot /var/www/stage.jaredwolff.com/gziplog stderr
}

log refers to this site specific access log.

log是指此站點特定的訪問日志。

root refers to where the root public folder is on your machine. In my case it's the common /var/www/<name of site>. Set your paths and remember them. We'll need them later!

root指的是根public文件夾是你的機器上。 就我而言,這是常見的/var/www/<name of site> 。 設置路徑并記住它們。 我們稍后將需要它們!

To have Caddy generate certs for this subdomain, you'll have to set the tls option. An email is all that's needed.

要讓Caddy為該子域生成證書,您必須設置tls選項。 只需一封電子郵件。

For more on the Caddyfile structure check out the documentation.

有關Caddyfile結構的更多信息, 請查閱文檔。

Make a file called caddyfile.conf and copy it to /usr/local/etc/ in your Caddy container:

創建一個名為caddyfile.conf的文件,并將其復制到Caddy容器中的/usr/local/etc/中:

vi caddyfile.conf
# Paste your caddyfile contents and save
bastille cp caddy caddyfile.conf /usr/local/etc/

You should now redirect your DNS to the server IP. That way Caddy can generate/fetch the correct certificates. Then you can start Caddy with:

現在,您應該將DNS重定向到服務器IP。 這樣,Caddy可以生成/獲取正確的證書。 然后,您可以通過以下方式啟動Caddy:

bastille service caddy caddy start

You can check the log at /usr/home/caddy/caddy.log to make sure that your domain provisioned correctly.

您可以在/usr/home/caddy/caddy.log檢查日志,以確保正確配置了您的域。

Side note: Getting setup with SSL certs is tough at first, especially if you're migrating from another server. Your site will have to go down for a little bit while you switch your DNS settings and start caddy.

旁注:首先,很難通過SSL證書進行設置,特別是如果要從另一臺服務器進行遷移。 當您切換DNS設置并啟動caddy您的站點將不得不關閉一小段時間。

(That's if you're using standard caddy 1.0. You can also use the DNS provider plugins here which make things a little easier.)

(如果您使用的是標準的caddy 1.0,則也可以在此處使用DNS提供程序插件,這使事情變得簡單一些。)

Now that we have caddy up and running it's time to copy our hugo generated assets over using rsync. We're off to the next step!

現在我們已經有了caddy和運行它的時候我們復制hugo產生的資產超過使用rsync 。 我們要進行下一步!

使構建和部署變得容易 (Make building and deploying easy)

images/make.png

I spend a ton of time writing C code, and that means I spend tons of time using Makefiles. For many, make (or gmake for GNU make) is the bane of their existence.

我花費大量時間編寫C代碼,這意味著我花費大量時間使用Makefiles。 對于許多人來說, make (或GNU make的gmake )是它們存在的禍根。

For building and deploying, make makes it easy to create reusable recipes. That way you know you can deploy with confidence every time.

對于構建和部署, make使創建可重復使用的配方變得容易。 這樣,您便知道每次都可以放心地進行部署。

My Makefile borrows from the one that Victoria Drake had posted not too long ago. I changed it up a bit to match my needs.

我的Makefile借鑒了Victoria Drake不久前發布的內容 。 我做了一些修改以滿足我的需求。

Let's take a tour and see what's inside:

讓我們看一下里面的東西:

.POSIX:HUGO_VERSION := 0.66.0OPTIMIZED_DIR := optimized
CONTENT_DIR := content
DEST_DIR := publicSERVER := 123.456.789.10
USER := user

The first section contains all the variables that I use to tell the functions later on what to do. It also has a reference to the .POSIX target. This means that the Makefile will be as portable between different versions of make.

第一部分包含所有我稍后用來告訴函數做什么的變量。 它還具有對.POSIX目標的引用。 這意味著Makefile將在make不同版本之間具有可移植性。

Then, I popped in some logic to determine whether I'm deploying to stage or production:

然后,我彈出一些邏輯來確定是要部署到舞臺還是生產:

# Set the place where it's deployed to.
ifdef PRODUCTION
$(info Building for production. 🚀)
TARGET := www
else
$(info Building for development. 🚀)
BASEURL := --baseURL "https://stage.jaredwolff.com"
TARGET := stage
endif

By default, recipes below will use the development workflow. To use the production workflow, you can invoke make like this:

默認情況下,以下配方將使用開發工作流程。 要使用生產工作流程,您可以這樣調用make

PRODUCTION=1 make build

This does add some extra friction to the deploy process. It's a good step though. That way you're sure the deploy is going to the right place!

這確實給部署過程增加了一些額外的摩擦。 這是一個好步驟。 這樣,您就可以確定部署的正確位置!

# Full path
DEPLOY_DIR := /usr/local/bastille/jails/caddy/root/path/to/$(TARGET).jaredwolff.com

Using the TARGET variable above, I then define the path to my server assets. I'm using Bastille to organize my jails, so the path is extra long. (yea, lengthly long) This allows us to use rsync to deploy the files with ease.

然后,使用上面的TARGET變量,定義服務器資產的路徑。 我正在使用巴士底獄(Bastille)整理監獄,因此路徑很長。 (是的,很長)這使我們可以使用rsync輕松部署文件。

Now here come the fun bits. To do a full bulk resize, I'm using the wildcard functionality of the Makefile.

現在來一些有趣的地方。 要進行完全批量調整,我使用了Makefile的wildcard功能。

IMAGES := \
$(wildcard $(CONTENT_DIR)/*/images/*.jpg) \
$(wildcard $(CONTENT_DIR)/*/images/*.JPG) \
$(wildcard $(CONTENT_DIR)/*/images/*.jpeg) \
$(wildcard $(CONTENT_DIR)/*/images/*.png) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.jpg) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.jpeg) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.png) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.JPG) \

In this case it will create a huge space delimited list of every image that is within my content directory. The biggest drawback of this method is that it's not space tolerant. An easy fix to this is to make sure that all my photos do not have spaces.

在這種情況下,它將為我的內容目錄中的每個圖像創建一個由空格分隔的列表。 這種方法的最大缺點是它不能容忍空間。 一個簡單的解決方法是確保我所有的照片都沒有空格。

Here's a quick and dirty bash command. You can use to rename files that have spaces and replace them with '_' characters:

這是一個快速而骯臟的bash命令。 您可以使用重命名具有空格的文件并將其替換為'_'字符:

for f in *\ *; do mv "$f" "${f// /_}"; done

Next, we rename these entries so the prefix is now the target directory. This will be useful when we want to resize:

接下來,我們重命名這些條目,以使前綴現在是目標目錄。 當我們要調整大小時,這將很有用:

OPTIMIZED_IMAGES := \
$(subst $(CONTENT_DIR)/,$(OPTIMIZED_DIR)/,$(IMAGES))

Now check out the optimize recipe:

現在查看optimize配方:

.PHONY: optimize
optimize: build $(OPTIMIZED_IMAGES)
@echo "🧂 Optimizing images"
rsync -r $(OPTIMIZED_DIR)/ $(DEST_DIR)/
du -sh $(CONTENT_DIR)/
du -sh $(DEST_DIR)/$(OPTIMIZED_IMAGES):
convert -strip -compress JPEG -resize '730>' $(subst $(OPTIMIZED_DIR)/,$(CONTENT_DIR)/,$@) $@

It first calls the build recipe and then also the $(OPTIMIZED_IMAGES) recipe. The later will optimize the image using the convert command from Imagemagick. In this case I'm only resizing files that are larger than 730px wide. Change yours accordingly so you can reap the benefits of an optimized site.

它首先調用build配方,然后調用$(OPTIMIZED_IMAGES)配方。 后者將使用Imagemagick的convert命令優化圖像。 在這種情況下,我只調整大于730px寬的文件的大小。 相應地更改您的站點 ,以便您可以獲取優化站點的好處。

After resizing, the recipe uses rsync to copy the files from the OPTIMIZED_DIR to DEST_DIR.

調整大小后,配方使用rsync將文件從OPTIMIZED_DIR復制到DEST_DIR.

If we take a look at the build recipe, I first building the assets. Then, I copy the photos from the content dir to optimized dir. The nice thing is that rsync will only move files that have changed. Thus it doesn't have to copy the files over and over and over again every time you build.

如果我們看一下build方法,那么我首先構建資產。 然后,我將照片從content目錄復制到optimized目錄。 令人高興的是, rsync將僅移動已更改的文件。 因此,您不必在每次構建時一遍又一遍地復制文件。

Finally, the deploy recipe.

最后, deploy配方。

.PHONY: deploy
deploy:
@echo rsync to $(DEPLOY_DIR)
@rsync -r --del public/ $(USER)@$(SERVER):$(DEPLOY_DIR)/
@echo making restic snapshot
@scp scripts/backup.sh $(USER)@$(SERVER):/root/backup.sh
@ssh $(USER)@$(SERVER) sh /root/backup.sh $(DEPLOY_DIR)
@echo "🚀 Site is deployed!"

You can see again that I'm using rsync to sync the contents of public/ to the server. Make sure you set the USER , SERVER and DEPLOY_DIR. In my case DEPLOY_DIR comes out to /usr/local/bastille/jails/caddy/root/var/www/www.jaredwolff.com

您可以再次看到我正在使用rsync將public/的內容同步到服務器。 確保設置了USERSERVERDEPLOY_DIR 。 在我的情況下, DEPLOY_DIR出現在/usr/local/bastille/jails/caddy/root/var/www/www.jaredwolff.com

When you do finally get a successful deploy you can double check everything is in the correct place. Then once everything looks good you can start up your caddy server using:

當您最終獲得成功的部署時,可以仔細檢查所有內容是否位于正確的位置。 然后,當一切看起來都不錯時,您可以使用以下方法啟動童車服務器:

bastille service caddy caddy start

deploy will also do something extra handy here. It will deploy my restic backup script and run it. I'll talk about this more in the backup section.

deploy也會在這里做些額外的方便。 它將部署我的restic備份腳本并運行它。 我將在備份部分中對此進行更多討論。

All in all, here's the full Makefile:

總而言之,這是完整的Makefile:

.POSIX:HUGO_VERSION := 0.66.0OPTIMIZED_DIR := optimized
CONTENT_DIR := content
DEST_DIR := publicSERVER := 155.138.230.8
USER := root# Set the place where it's deployed to.
ifdef PRODUCTION
$(info Building for production. 🚀)
TARGET := www
else
$(info Building for development. 🚀)
BASEURL := --baseURL "https://stage.jaredwolff.com"
TARGET := stage
endif# Full path
DEPLOY_DIR := /usr/local/bastille/jails/caddy/root/var/www/$(TARGET).jaredwolff.comIMAGES := \
$(wildcard $(CONTENT_DIR)/*/images/*.jpg) \
$(wildcard $(CONTENT_DIR)/*/images/*.JPG) \
$(wildcard $(CONTENT_DIR)/*/images/*.jpeg) \
$(wildcard $(CONTENT_DIR)/*/images/*.png) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.jpg) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.jpeg) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.png) \
$(wildcard $(CONTENT_DIR)/*/*/images/*.JPG) \OPTIMIZED_IMAGES := \
$(subst $(CONTENT_DIR)/,$(OPTIMIZED_DIR)/,$(IMAGES)).PHONY: all
all: build optimize.PHONY: clean
clean:
rm -rf public/
rm -rf optimized/.PHONY: serve
serve:
@hugo serve -D.PHONY: ssh
ssh:
@ssh $(USER)@$(SERVER).PHONY: build
build:
@echo "🍳 Generating site"
hugo --gc --minify -d $(DEST_DIR) $(BASEURL)
rsync -av --del -f"+ */" -f"- *" $(CONTENT_DIR)/ $(OPTIMIZED_DIR)/.PHONY: optimize
optimize: build $(OPTIMIZED_IMAGES)
@echo "🧂 Optimizing images"
rsync -r $(OPTIMIZED_DIR)/ $(DEST_DIR)/
du -sh $(CONTENT_DIR)/
du -sh $(DEST_DIR)/$(OPTIMIZED_IMAGES):
convert -strip -compress JPEG -resize '730>' $(subst $(OPTIMIZED_DIR)/,$(CONTENT_DIR)/,$@) $@.PHONY: deploy
deploy:
@echo rsync to $(DEPLOY_DIR)
@rsync -r --del public/ $(USER)@$(SERVER):$(DEPLOY_DIR)/
@echo making restic snapshot
@scp scripts/backup.sh $(USER)@$(SERVER):/root/backup.sh
@ssh $(USER)@$(SERVER) sh /root/backup.sh $(DEPLOY_DIR)
@echo "🚀 Site is deployed!"

There are a few other handy nuggets in there you may want to use. clean, serve and ssh have been very helpful when testing and connecting.

您可能還需要使用其他一些方便的塊。 cleanservessh在測試和連接時都非常有用。

In the end you'll have a two step deploy process. The first generates your site with optimized images. The second is deploying to a server for static hosting.

最后,您將分兩步進行部署。 第一個生成具有優化圖像的網站。 第二個是部署到服務器以進行靜態托管。

增量備份 (Incremental Backup)

images/Backup.png

After discovering Restic I've been sold on how handy it has been for all my incremental backup needs. In the case of my server, I'm using to back up the root folder of my site. That way, if I need to roll back, I can do so with a few short steps.

在發現Restic之后,我被賣出了滿足我所有增量備份需求的方法。 對于我的服務器,我正在用來備份網站的根文件夾。 這樣,如果我需要回滾,可以通過幾個簡單的步驟來完成。

Here's how you can set up a local restic repo.

這是設置本地restic庫的方法。

設定 (Setting it up)

Initializing the repo is simple. The most important part is making sure you don't lose/forget your password!

初始化倉庫很簡單。 最重要的部分是確保您不會丟失/忘記密碼!

# restic init -r /root/backupsenter password for new repository:enter password again:created restic repository 32e14c7052 at /root/backupsPlease note that knowledge of your password is required to accessthe repository. Losing your password means that your data isirrecoverably lost.

Set the RESTIC_PASSWORD environment variable to avoid entering your password. To make it permanent you'll have to place export RESTIC_PASSWORD="Your password here!" within the .profile file in /root/.

設置RESTIC_PASSWORD環境變量,以避免輸入密碼。 要使其永久存在,您必須export RESTIC_PASSWORD="Your password here!"放置export RESTIC_PASSWORD="Your password here!"/root/.profile文件中。

備份 (Backing Up)

Invoking restic over SSH is tough. So our next best bet?

通過SSH調用restic很難。 那么我們的下一個最佳選擇?

Transfer a (very brief) shell script to the server and run it after a deploy. Here's the contents of what I'm using today:

將(非常簡短的)shell腳本傳輸到服務器并在部署后運行它。 這是我今天使用的內容:

#!/bin/sh
export RESTIC_PASSWORD="Your password here!"
restic backup $1 -r /root/backups/

Side note: As I sit here and look at this script, for security reasons you can replace "Your password here!" with $2 which is the second argument to the script. That way you don't need to commit/push the password stored in a static file!

旁注:當我坐在這里查看此腳本時,出于安全原因,您可以替換“此處的密碼!”。 $ 2是腳本的第二個參數。 這樣,您無需提交/推送存儲在靜態文件中的密碼!

This first sets your backup password. Then it runs restic using the first command line argument as the path. So, to run a backup with this script, it would look something like this:

這首先設置您的備份密碼。 然后使用第一個命令行參數作為路徑運行restic 。 因此,要使用此腳本運行備份,它將類似于以下內容:

./backup.sh /path/to/your/public/folder/

Note: you do need to initialize your restic backup before you start backing up. It will barf at you otherwise!

注意: 開始備份之前,您確實需要初始化restic備份。 否則它會對你大吼!

In my case I'm placing the incremental backups on a different folder of my machine. That way they're easily accessible and fast.

就我而言,我將增量備份放置在計算機的其他文件夾中。 這樣,它們就可以輕松訪問并且快速

查看您的備份 (Viewing your backups)

To view your backups you can run the following command:

要查看備份,可以運行以下命令:

# restic snapshots -r /root/backups -g paths -c
enter password for repository:
repository e140b5e4 opened successfully, password is correct
snapshots for (paths [/usr/local/bastille/jails/caddy/root/var/www/www.jaredwolff.com]):
ID        Time                 Host         Tags
--------------------------------------------------
d3328066  2020-03-10 00:30:58  vultr.guest
f3360819  2020-03-10 04:03:03  vultr.guest
231dd134  2020-03-10 04:44:00  vultr.guest
3c1be26a  2020-03-10 04:56:19  vultr.guest
e96c947c  2020-03-10 05:03:00  vultr.guest
34c3682a  2020-03-10 14:01:37  vultr.guest
fbccdb8c  2020-03-10 14:04:26  vultr.guest
9ce11146  2020-03-10 15:38:49  vultr.guest
046b3da3  2020-03-10 15:47:06  vultr.guest
9c28d4bc  2020-03-10 15:48:25  vultr.guest
469dc228  2020-03-10 15:48:54  vultr.guest
6f78af72  2020-03-10 17:00:21  vultr.guest
29ad17b2  2020-03-10 20:18:23  vultr.guest
ed22ce1f  2020-03-10 20:20:24  vultr.guest
9c8c1b03  2020-03-11 13:56:40  vultr.guest
b6cfcfec  2020-03-11 14:08:14  vultr.guest
e8546005  2020-03-11 14:27:22  vultr.guest
49a134fe  2020-03-17 00:47:58  vultr.guest
c0beb283  2020-03-18 20:44:52  vultr.guest
--------------------------------------------------

You can use this list to determine if you need to roll back a deploy.

您可以使用此列表來確定是否需要回滾部署。

恢復中 (Restoring)

Restoring from a backup, especially in a live environment, needs to be quick. After viewing your backups you can restore a specific backup by using its ID.

從備份還原,尤其是在實時環境中,需要快速進行。 查看備份后,您可以使用其ID還原特定的備份。

restic restore d3328066

This will restore the files back to the backup made on 2020-03-10 00:30:58. Awesome. Plus it won't overwrite every single file. It will only apply the differences from the current state and the stored state.

這會將文件還原回2020-03-10 00:30:58進行的備份 太棒了 另外,它不會覆蓋每個文件。 它將僅應用與當前狀態和存儲狀態的差異。

結論 (Conclusion)

We've covered a ton of ground in this post. You've learned how to:

在這篇文章中,我們已經做了很多工作。 您已經學會了如何:

  • Deploy your own server using Vultr

    使用Vultr部署自己的服務器
  • Use Bastille to create Container-like Jails

    使用巴士底獄創建類似容器的監獄
  • Set up Caddy to serve static file assets with TLS

    設置Caddy以使用TLS提供靜態文件資產
  • Deploy the files using a fairly simple Makefile and rsync

    使用相當簡單的Makefile和rsync部署文件

  • Back up after every deploy using restic

    每次部署后使用restic進行restic

In the end we have a robust, secure and simple platform for hosting static files and services.

最后,我們有了一個強大,安全和簡單的平臺來托管靜態文件和服務。

Stay tuned as there are more posts like this coming your way soon! In the meantime check out my other posts.

請繼續關注,因為很快會有更多此類帖子發布! 同時,請查看我的其他帖子。

Thanks for reading and see you next time! 👍

感謝您的閱讀,下次見! 👍

You can find other articles like this at www.jaredwolff.com.

您可以在www.jaredwolff.com上找到其他類似的文章。

翻譯自: https://www.freecodecamp.org/news/my-latest-self-hosted-hugo-workflow/

hugo 能做web開發嗎

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390395.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390395.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390395.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

資源 | 深度學習課程入門與介紹

【1】Andrew NG Deep Learning.ai http://deeplearning.ai/網易云課堂&#xff08;中文字幕&#xff09;&#xff1a;http://mooc.study.163.com/smartSpec/detail/1001319001.htm推薦理由&#xff1a;Andrew Ng老師是講課的能手&#xff0c;很多人認識他是從Stanford的經典《機…

PostCSS 以及 cssnext語法

本文是對近兩天學習postcss的總結&#xff0c;在這里分享給大家。 如有錯誤&#xff0c;還請指正&#xff01; 什么是postcss postcss 一種對css編譯的工具&#xff0c;類似babel對js的處理&#xff0c;常見的功能如&#xff1a; 1 . 使用下一代css語法 2 . 自動補全瀏覽器前綴…

5187. 收集足夠蘋果的最小花園周長

給你一個用無限二維網格表示的花園&#xff0c;每一個 整數坐標處都有一棵蘋果樹。整數坐標 (i, j) 處的蘋果樹有 |i| |j| 個蘋果。 你將會買下正中心坐標是 (0, 0) 的一塊 正方形土地 &#xff0c;且每條邊都與兩條坐標軸之一平行。 給你一個整數 neededApples &#xff0c…

虛擬機 VMware Workstation12 安裝OS X 系統

Windows下虛擬機安裝Mac OS X —– VMware Workstation12安裝Mac OS X 10.11本文即將介紹WIN虛擬MAC的教程。完整詳細教程&#xff08;包含安裝中的一些問題&#xff09;【并且適用其他mac os x版本】Windows下 VM12虛擬機安裝OS X 10.11(詳細教程) 工具/原料 Mac OS X 10.11 鏡…

aws dynamodb_DynamoDB備忘單–您需要了解的有關2020 AWS認證開發人員助理認證的Amazon Dynamo DB的所有信息

aws dynamodbThe emergence of cloud services has changed the way we build web-applications. This in turn has changed the responsibilities of a Web Developer. 云服務的出現改變了我們構建Web應用程序的方式。 反過來&#xff0c;這改變了Web開發人員的職責。 We use…

北大CIO走進龍泉寺交流研討會圓滿舉行

緣起 2016年4月16日&#xff0c;北京大學信息化與信息管理研究中心秘書長姚樂博士與國家非物質文化遺產蔚縣剪紙傳承人周淑英女士一起在龍泉寺拜見了中國佛教協會會長、龍泉寺主持學誠法師。在拜見學誠法師時&#xff0c;姚樂博士與學誠法師聊到了“賢二機器僧”和人工智能。姚…

負載均衡種類

http://blog.csdn.net/zhoudaxia/article/details/23672319DNS DNS輪詢是最簡單的負載均衡方式。以域名作為訪問入口&#xff0c;通過配置多條DNS A記錄使得請求可以分配到不同的服務器。DNS輪詢沒有快速的健康檢查機制&#xff0c;而且只支持WRR的調度策略導致負載很難“均衡”…

代碼流星雨是什么形式_為什么要在2020年與流星合作

代碼流星雨是什么形式Meteor, an allegedly dead development platform, is still alive and can bring massive value to your everyday coding experience.Meteor&#xff0c;據稱已失效的開發平臺&#xff0c;仍然有效&#xff0c;可以為您的日常編碼體驗帶來巨大的價值。 …

Centos7 Docker私有倉庫搭建

Centos7 Docker私有倉庫搭建 倉庫&#xff1a;集中存放鏡像的地方&#xff0c;可分為公共倉庫和私有倉庫&#xff08;公共倉庫"http://hub.docker.com"或國內的"http://www.daocloud.io"&#xff09; Registry&#xff1a;注冊服務器才是存放倉庫具體的服務…

MySQL觸發器使用詳解

MySQL包含對觸發器的支持。觸發器是一種與表操作有關的數據庫對象&#xff0c;當觸發器所在表上出現指定事件時&#xff0c;將調用該對象&#xff0c;即表的操作事件觸發表上的觸發器的執行。 創建觸發器在MySQL中&#xff0c;創建觸發器語法如下&#xff1a; 代碼如下: CREATE…

java中訪問修飾符_Java中的訪問修飾符介紹

java中訪問修飾符什么是訪問修飾符&#xff1f; (What are Access Modifiers?) Have you ever wanted to define how people would access some of your properties? You would not want anyone using your underwear. However, your close friends and relatives can use yo…

VIM 編輯器

2019獨角獸企業重金招聘Python工程師標準>>> VIM 相對于VI 的提升 VIM 支持多級撤銷VIM 可以跨平臺運行VIM 支持語法高亮VIM 支持圖形界面VIM 編輯器的操作模式 Command Mode -命令模式Insert Mode -輸入模式Last Lin Mode -底行模式#使用yum 命令安裝vim 軟件&…

/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用

轉載自&#xff1a;http://blog.csdn.net/u013968345/article/details/21262033 /etc/profile:此文件為系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行. 并從/etc/profile.d目錄的配置文件中搜集shell的設置. /etc/bashrc:為每一個運行bash shell的用戶執行此文件…

python初學者_終極Python初學者手冊

python初學者Python has become one of the fastest-growing programming languages over the past few years. 在過去的幾年中&#xff0c;Python已成為增長最快的編程語言之一。 Not only it is widely used, it is also an awesome language to tackle if you want to get …

z-index

z-index 這個東西非常簡單&#xff0c;它有四大特性&#xff0c;每個特性你記住了&#xff0c;頁面布局就不會出現找不到盒子的情況。 z-index 值表示誰壓著誰&#xff0c;數值大的壓蓋住數值小的&#xff0c;只有定位了的元素&#xff0c;才能有z-index,也就是說&#xff0c;不…

大型運輸行業實戰_day12_1_權限管理實現

1.業務分析 權限說的是不同的用戶對同一個系統有不同訪問權限,其設計的本質是:給先給用戶分配好URL,然后在訪問的時候判斷該用戶是否有當前訪問的URL. 2.實現 2.1數據庫設計標準5表權限結構 2.2.sql語句實現,根據用戶id查詢該用戶所有的資源 sql語句: SELECT ur.user_id, r.u…

aws python庫_如何使用Python,AWS和IEX Cloud創建自動更新股市數據的Excel電子表格

aws python庫Many Python developers in the financial world are tasked with creating Excel documents for analysis by non-technical users.金融界的許多Python開發人員的任務是創建Excel文檔&#xff0c;以供非技術用戶進行分析。 This is actually a lot harder than i…

37)智能指針(就是自動delete空間)

1&#xff09;問題引入&#xff1a; 在java或者在C中&#xff0c;一旦你new一個東西&#xff0c;那么必然有一個delete與之對應&#xff0c;比如&#xff1a; 1 int main&#xff08;&#xff09;2 {3 int* p new int&#xff08;&#xff09;&#xff1b;4 5 *…

linux 安裝maven

2019獨角獸企業重金招聘Python工程師標準>>> 目錄:/usr/local/maven 1.下載 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz 2.解壓 tar -zxvf apache-maven-3.5.3-bin.tar.gz 3.配置 vi /etc/profile #講下面…

自由開發者怎么生存_如何作為自由開發者生存

自由開發者怎么生存It’s been 8 weeks since we started experiencing the dramatic impact of the COVID-19 pandemic. In that time, we’ve all borne witness to how this virus can impact our families, our communities, and our livelihood. 自我們開始體驗COVID-19大…