Having installed Ubuntu for the Raspberry PI and configured Intranet penetration and boot up, I can now even connect to the Raspberry PI from the bus using my phone.

Then, the next thing I need is the Docker service.

Install the Docker service

Installing the Docker service requires only one command.

sudo apt install docker.io
Copy the code

After the execution, the Docker service has started by itself, but it still needs to be configured slightly.

The configuration file of the image source is located in /etc/dock/daemon. json. This file does not exist by default. You need to create the file by yourself. Add a mirror source in the Registry -mirrors field, save the file, and restart the service.

ubuntu@ubuntu:~$ sudo vim /etc/docker/daemon.json
ubuntu@ubuntu:~$ cat /etc/docker/daemon.json
{
    "registry-mirrors": [
            "https://registry.docker-cn.com",
            "https://docker.mirrors.ustc.edu.cn",
            "http://hub-mirror.c.163.com"
    ]
}
ubuntu@ubuntu:~$ sudo systemctl daemon-reload
ubuntu@ubuntu:~$ sudo systemctl restart docker
Copy the code

Available domestic mirror sources are as follows:

  • Docker China official image registry.docker-cn.com
  • NetEase Hive Hub-mirror.c.163.com
  • China university of science and technology, docker.mirrors.ustc.edu.cn

After the configuration, I can use the docker pull command to pull the image I want. The images I need are as follows:

Docker pull mariadb:10.6.4 # mariadb database, Docker pull Ubuntu: Focal # Ubuntu Focal version Docker pull Python: 3.9.7-Bullseye # Python images can be used to learn Python, compile so files, and compile readthedocs documents. Docker pull nginx:1.20.1 # nginx image, can do web projects reverse proxyCopy the code

After all images are pulled, you can run the docker images command to view them:

ubuntu@ubuntu:~$ sudo docker images
REPOSITORY   TAG              IMAGE ID       CREATED       SIZE
python       3.9.7-bullseye   1c3e5106c1ab   5 days ago    860MB
mariadb      10.6.4           6819e5d163d1   2 weeks ago   393MB
ubuntu       focal            d5ca7a445605   2 weeks ago   65.6MB
redis        6.2.6            c3d77c9fc5b8   2 weeks ago   107MB
nginx        1.20.1           5eb25865d142   2 weeks ago   126MB
Copy the code

Restore the Mariadb database

MariaDB database management system is a branch of MySQL, mainly maintained by the open source community. Under the GPL license, MariaDB is intended to be fully compatible with MySQL, including apis and command lines, making it an easy replacement for MySQL. Since mysql had already been acquired by Oracle, I had to switch to Mariadb at no cost.

Last time, I installed mariadb database with APT on Raspberry PI and stored some important data, such as the private warehouse data of Gitea. Later, the USB disk where raspbian system is located was broken, but the data directory was saved. This time, I deployed Mariadb 10.6.4 using Docker. Use the data directory for data recovery.

1. Start the database

To start a database service from scratch, you only need one command:

docker run --name recovery_db -d -v /mnt/mysqlrecovery:/var/lib/mysql \ -e MARIADB_ROOT_PASSWORD=jwirLAST -p 3406:3306 Mariadb :10.6.4 \ --character-set-server= UTf8MB4 --collation-server= UTf8MB4_unicode_ci \ --lower_case_table_names=1Copy the code

Command line parameters:

The docker run command is used to run containers.

–name Specifies the name of the container as recovery_db. The name must be unique.

The -d option specifies that the container runs in the background, as opposed to the it option, which specifies that the container runs interactively.

The -v option is used to map file directories. In this case, /var/lib/mysql (data file path) in the container is mapped to /data/mysqlrecovery outside the container. Note That the external directory must be prepared first.

The -e parameter is used to specify environment variables. MARIADB_ROOT_PASSWORD specifies the mariadb root password in the container according to the mariADB docker image. The value can be used continuously as the -v option.

The -p option is used for port mapping. Here, port 3406 of raspberry PI is mapped to port 3306 in the container for mariadb service to monitor and provide services. The -p parameter can also be used continuously.

Mariadb :10.6.4 specifies that the docker image to be used is Mariadb with the tag 10.6.4.

The last part of the command specifies the server’s code set and the table name is case insensitive.

After the command is executed, you can run the docker ps command to see that the container named recovery_db is running. You can run the ls -l /data/mysqlrecovery command to see that the/MNT /mysqlrecovery directory is being initialized. The Mariadb database file appears.

Depending on the I/O efficiency of raspberry PI, you may need to wait two or three minutes. After the data directory is initialized, you can use the heidisQL tool to connect to port 3406 of raspberry PI.

2. Copy files

After previous step operation, mariadb database service has already started to run, can be used normally, after the steps is to do data recovery, I saved before the database file in/home/ubuntu/backups/mysql. Zip, need to unzip the files, stop services, covering, start the service, the export script.

The mkdir/MNT/temp & CD/MNT/temp # create and switch working directory cp/home/ubuntu/backups/mysql. Zip. && unzip - q mysql. Zip # copy and unzip the files silent ls -l / MNT/temp/mysql docker ps - a docker stop recovery_db # stop service | grep recovery_db # confirm the service status cp - rp/MNT/temp/mysql/gitea / MNT/mysqlrecovery gitea # database replication gitea cp/MNT/temp/mysql/ibdata1 / MNT/mysqlrecovery / # ibdata1 file docker start replication table structure Recovery_db docker ps # start the database service - a | grep recovery_db # confirm the service statusCopy the code

After the database is started, reconnect to the database using HeidisQL or another mysql-enabled client, and you can see that the database named Gitea has been restored.

Note When copying database files, pay attention to the file owners and permissions; otherwise, the database cannot be used properly.

3. Delete the old and bring in the new

Run the docker stop recovery_db && docker rm recovery_db command to stop and delete the recovery_db container. Run the rm -rf/MNT /mysqlrecovery command to delete the database files that are no longer used.

Follow step 1 to restart the new database container and wait for the database to become available.

docker run --name work_db -d -v /data/mysqldata:/var/lib/mysql --restart=always \ -e MARIADB_ROOT_PASSWORD=jwirLAST -p 3306:3306 Mariadb :10.6.4 \ --character-set-server= UTf8MB4 --collation server= UTf8MB4_unicode_ci \ --lower_case_table_names=1Copy the code

4. Import the SQL script

Import the data into the new database using the SQL file exported in the previous step.

Also refer to the official tutorial

Restore the Gitea version repository

Gitea is a lightweight code hosting solution driven by the open source community. The backend is written in the Go language under the MIT Open Source license. You can view the full code of Gitea here. Gitea is open source, easy to install, cross-platform and lightweight, and runs perfectly on raspberry PI.

Gitea supports docker deployment and binary deployment, and this paper belongs to the scope of data recovery. Starting gitea service in binary mode is slightly different from redeployment.

Environment to prepare

You need to create a Git account first, and then the Gitea service will start with git account permissions.

Git useradd -d /home/git-m -g git -s /bin/bash git useradd -d /home/git-m -g git -s /bin/bash gitCopy the code

Gitea version of the file backup warehouse before rescue package in the/home/ubuntu/backups/giteadata tar..bz2, you need to use the git user unzip the files to the/home/git/gitea.

cp /home/ubuntu/backups/giteadata.tar.bz2 /home/git/
chown git.git /home/git/giteadata.tar.bz2
su - git
tar xf giteadata.tar.bz2
Copy the code

Decompression, after the completion of the configuration file is located in the/home/git/gitea/custom/conf/app. Ini, version of the warehouse is located in the/home/git/gitea/gitea – repositories.

git@ubuntu:~/gitea$ls -l /home/git/gitea total 97920 DRWXRWXRWX 3 git git 4096 April 22 2020 Custom DRWXRWXRWX 11 git git 4096 11月 6 20:14 data-rwxr-xr-x 1 git git 100246088 11月 8 19:39 gitea DRWXRWXRWX 3 git git 4096 14 月 22 2020 Gitea-repositories DRWXRWXRWX 2 Git git 4096 11月 22 00:09 logCopy the code

Download the latest version of Gitea, raspberry PI 4B uses arm64 architecture CPU, can use the gadEa-1.15.6-Linux-arm64.xz package, Raspberry PI 3B and previous versions, You need to download the gadea-1.15.6-linux-arm-6.xz or Gadea-1.15.6-linux-arm-5.xz file, run the xz -d command to decompress the file, and replace the file with /home/git/gadea/gitea.

Git @ ubuntu: ~ / temp $wget - 2021-11-22 23:34:26 - https://dl.gitea.io/gitea/1.15.6/gitea-1.15.6-linux-arm64.xz https://dl.gitea.io/gitea/1.15.6/gitea-1.15.6-linux-arm64.xz Resolving dl. Gitea. IO (dl. Gitea. IO)... 2606:4700-3034: : ac43: bad3, 2606:4700-3036: : 6815:3 c07 104.21.60.7,... Connecting to dl.gitea.io (dl.gitea.io)|2606:4700:3034::ac43:bad3|:443... connected. HTTP request sent, awaiting response... 307 Temporary Redirect Location: https://storage.gitea.io/gitea-artifacts/gitea/1.15.6/gitea-1.15.6-linux-arm64.xz? X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SU5ZZ3Q6D6AFIQSCOO65%2F20211122%2Fus-east-1%2Fs3%2Faws4_request&X-Amz- Date=20211122T153428Z&X-Amz-Expires=600&X-Amz-SignedHeaders=host&X-Amz-Signature=937898e9a6c24308f6a0a55a496defb94a6cd32 5e2b85498219e3072ad4c5599 [following] --2021-11-22 23:34:28-- https://storage.gitea.io/gitea-artifacts/gitea/1.15.6/gitea-1.15.6-linux-arm64.xz? X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SU5ZZ3Q6D6AFIQSCOO65%2F20211122%2Fus-east-1%2Fs3%2Faws4_request&X-Amz- Date=20211122T153428Z&X-Amz-Expires=600&X-Amz-SignedHeaders=host&X-Amz-Signature=937898e9a6c24308f6a0a55a496defb94a6cd32 5e2b85498219e3072ad4c5599 Resolving storage.gitea.io (storage.gitea.io)... 2604:1380-2000: c600: : 5, 147.75.84.81 Connecting to storage. Gitea. IO (storage. Gitea. IO) | 2604:1380-2000: c600: : | : 443... connected. HTTP request sent, awaiting response... 200 OK Length: 44287796 (42M) [application/x-xz] Saving to: 'gitea 1.15.6 - Linux - arm64. Xz' gitea 1.15.6 - Linux - arm64. Xz 100% [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >] 42.24 M 946 KB/s 53 in the 2021-11-22 s 23:35:22 (815 KB/s) 'Gadea-1.15.6-linux-arm64.xz' saved [44287796/44287796] git@ubuntu:~/temp$ls -l total 43252 -rw-rw-r-- 1 git git 44287796 10月 28 17:08 gitea-1.15.6-linux-arm64.xz git@ubuntu:~/temp$xz -d gitea-1.15.6-linux-arm64.xz git@ubuntu:~/temp$ls -l total 97900-rw-RW-r -- 1 git git 100246088 10月 28 17:08 GitEa-1.15.6-linux-arm64 git@ubuntu:~/temp$mv gadea-1.15.6-linux-arm64 /home/gitea-gitea-git@ubuntu :~/temp$chmod +x /home/gitea-gitea-gitea git@ubuntu:~/temp$Copy the code

Add the configuration

Modify the configuration file/home/git/gitea/custom/conf/app. Ini database configuration and configuration file path consistent with actual situation:

APP_NAME = Gitea: Git with a cup of tea
RUN_USER = git  ; Start as the Git user
RUN_MODE = prod  ; Production environment mode

[oauth2]
JWT_SECRET = BMZHkyeWxyQe*********  ; JWT authentication token

[security]
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5c********
INSTALL_LOCK   = true
SECRET_KEY     = 7x9LiJyJwrteuPmIh5iZAXlEA********

[database]
DB_TYPE  = mysql  ; The value can be PostgresQL, SQLite, or mysql
HOST     = 192.168.3.14:3306  ; Database Service address
NAME     = gitea  ; The database name
USER     = gitea  ; Database account
PASSWD= * * * * *; Database password
SSL_MODE = disable  ; Disable SSL
CHARSET  = utf8mb4   ; The encoding format uses UTF8MB4, which supports emoticons compared to UTF8
PATH     = /home/git/gitea/data/gitea.db  ; The path to the data file when using the SQLite database

[repository]
ROOT = /home/git/gitea/gitea-repositories  ; Version warehouse location

[server]
SSH_DOMAIN       = 192.168.3.14  ; SSH mode address of the Git service
DOMAIN           = 192.168.3.14  ; HTTP address of the Git service
HTTP_PORT        = 3000  ; The Gitea service listens on the port
ROOT_URL         = http://192.168.3.14:3000/
DISABLE_SSH      = false
SSH_PORT         = 22
LFS_START_SERVER = true
LFS_CONTENT_PATH = /home/git/gitea/data/lfs
LFS_JWT_SECRET   = 0zK7KHBPfffQu8i5********
OFFLINE_MODE     = true
LOCAL_ROOT_URL   = http://localhost:3000/

[mailer]
ENABLED = true
HOST    = smtp.qq.com:465  ; The address and port of the MAIL SMTP service
FROM    = [email protected]  ; SMTP sender
USER    = [email protected]  ; SMTP Service Account
PASSWD  = kni********************  ; SMTP Service Password

[service]
REGISTER_EMAIL_CONFIRM            = false
ENABLE_NOTIFY_MAIL                = false
DISABLE_REGISTRATION              = false
ALLOW_ONLY_EXTERNAL_REGISTRATION  = false
ENABLE_CAPTCHA                    = false
REQUIRE_SIGNIN_VIEW               = true
DEFAULT_KEEP_EMAIL_PRIVATE        = false
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
DEFAULT_ENABLE_TIMETRACKING       = true
NO_REPLY_ADDRESS                  = noreply.localhost

[picture]
DISABLE_GRAVATAR        = true
ENABLE_FEDERATED_AVATAR = false

[openid]
ENABLE_OPENID_SIGNIN = true
ENABLE_OPENID_SIGNUP = true

[session]
PROVIDER = file

[log]
MODE      = file
LEVEL     = info  ; The level of logging
ROOT_PATH = /home/git/gitea/log  ; Log File Path
Copy the code

Attached database UTF8MB4 encoding description

Perform/home/git/gitea gitea web – config/home/git/gitea/custom/conf/app. Ini test gitea process whether can normal boot, View/home/git/gitea/log/gitea log log file to confirm whether there is an error, such as database connection fails, abnormal file system permissions, etc.

git@ubuntu:~/gitea$ /home/git/gitea/gitea web --config /home/git/gitea/custom/conf/app.ini 2021/11/22 23:43:42 cmd/web.go:102:runWeb() [I] Starting Gitea on PID: 18955 2021/11/22 23:43:42 cmd/web.go:146:runWeb() [I] Global init 2021/11/22 23:43:42 routers/init.go:74:GlobalInit() [I] Git Version: 2.25.1, Wire Protocol Version 2 Enabled 2021/11/22 23:43:42 routers/init. Go: 77: GlobalInit () [I] AppPath: /home/git/gitea/gitea 2021/11/22 23:43:42 routers/init.go:78:GlobalInit() [I] AppWorkPath: /home/git/gitea 2021/11/22 23:43:42 routers/init.go:79:GlobalInit() [I] Custom path: /home/git/gitea/custom 2021/11/22 23:43:42 routers/init.go:80:GlobalInit() [I] Log path: /home/git/gitea/log 2021/11/22 23:43:42 routers/init.go:81:GlobalInit() [I] Run Mode: ProdCopy the code
git@ubuntu:~/temp$ cat /home/git/gitea/log/gitea.log 2021/11/22 23:43:44 cmd/web.go:196:listen() [I] Listen: http://0.0.0.0:3000 2021/11/22 23:43:44 CMD /web.go:199:listen() [I] LFS server enabled 2021/11/22 23:43:44... S/graceful/server. Go: 62: NewServer () [I] Starting the new Web server: TCP: 0.0.0.0:3000 on PID: 18955Copy the code

At this point, you can visit the Gitea page to view the effect. You can log in with your own account, and the log will be refreshed:

System services

To avoid having to manually start the Gitea process every time you start the Raspberry PI, we can refer to the official documentation to register the Gitea binary as a system service. Since my database is deployed using Docker, the service file is slightly different from the official example:

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target  ; Start the syslog service after it is started
After=network.target  ; Start after the network service is started
After=docker.target  ; Start after the Docker service starts, because the database starts with Docker

[Service]
RestartSec=2s  ; Restart interval
Type=simple
User=git  ; Start the server process using the Git account
Group=git  ; Use git group
WorkingDirectory=/home/git/gitea/  ; Setting the Working Path
ExecStart=/home/git/gitea/gitea web --config /home/git/gitea/custom/conf/app.ini  Process start command
Restart=always  ; Automatic restart upon failure
Environment=USER=git  ; Setting environment Variables
HOME=/home/git  ; Setting the home path

[Install]
WantedBy=multi-user.target  ; The system starts automatically upon startup
Copy the code

To save the above content to the/etc/systemd/system/gitea. Service file (may need root access), and then to set boot from qi and start gitea service.

Systemctl start giteaCopy the code

Run the systemctl status gitea command to check the service status and open the browser to check the page.

Perfect data recovery ~~~