demand

  1. Install and set up GitLab on the new server
  2. Manual + Automatic backup of GitLab on old server
  3. Manually + Automatically SCP the GitLab backup package to the new server
  4. Manual + auto restore gitLab backup package on new server
  5. Automatically delete expired backup packages on the old and new servers

The premise

  1. version
  • Gitlab-ce is the community edition
  • Gitlab-ee is the enterprise edition
  1. plan
  • The timer executes shell scripts
  1. tool
  • crontab
  • shell
  • expect
  1. operation
  • If you are not root, +sudo before all commands
  • The gitLab versions of the old and new servers remain the same
  • The old server usedGitlab - ee_10. 7.2 ee. 0 _amd64. DebVersion of GitLab, sample also selected this enterprise edition
  1. Backups of directories can only be handled by root, but gitlab recovery is performed by Git

Therefore, make the /var/opt/gitlab/backups folder readable, write, and executable on both the old and new servers

sudo chmod 777 backups/
Copy the code

From now on, I will operate step by step, leading everyone from installation to automatic backup, and finally realizing the dream of no longer being troubled by gitLab backup and focusing on code typing

Install and set up GitLab on the new server

  1. Installation method 1: Official website installation (slow)
  2. Installation mode two: domestic mirror installation (fast)

Gitlab is installed on the official website

Please poke at the official website of GitLab

Since my operating system is Ubuntu, I selected the Ubuntu installation steps to appear below

  1. Install and configure required dependencies
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates
Copy the code
  1. The installationPostfixTo send a notification email. If you want another way to send mail, after gitLab is installed, the next step is to configure an additional SMTP service.
sudo apt-get install -y postfix
Copy the code

A configuration screen appears when Postfix is installed. Select Internet Site and press Enter. Mail name is your server’s DNS and press Enter. If additional screens appear, continue to accept the default Settings and press Enter.

  1. Add the GitLab installation package repository and install it

Add the GitLab package repository

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
Copy the code
  1. Install the GitLab package. choosehttps://gitlab.example.comChange to the URL to access the GitLab instance. The installation will automatically configure and start GitLab to the URL.

A certificate with Let’s Encrypt will be automatically requested for https://gitlab, which requires pushing HTTP access and a valid host name.

sudo EXTERNAL_URL="https://gitlab.example.com" apt-get install gitlab-ee
Copy the code

At this point you will find that the slow death, the speed is 100K /s, after all, the two separated by a wall, then change the domestic mirror installation

Tsinghua image installation gITLab

Since the official website is too slow, we choose Tsinghua mirror to install GitLab

Tsinghua University open source software mirror station

Gitlab Community Edition Image usage help

There are various versions in it

The backup must be the same as the gitLab version of the original server; otherwise, the installation cannot be performed

A search for GitLab in the search bar will bring up the relevant version of GitLab

Gitlab – ee_10. 7.2 ee. 0 _amd64. Deb version in/ubuntu/pool/bionic/main/g/gitlab – ee/down

Or access gitlab-EE directly,

Direct access to the Gitlab-CE version.

  1. Update the warehouse first
sudo apt-get update
Copy the code
  1. Add GPG public key to GitLab
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
Copy the code
  1. Type in and select your Debian/Ubuntu version

The text box contents written into the/etc/apt/sources. List. D/gitlab – ce. The list,

I was written into the/etc/apt/sources list. D/gitlab – ee. The list,

Write deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/ubuntu xenial main

  1. Update APT repository and install Gitlab-ee
Sudo apt-get update sudo apt-get install gitlab-ee=10.7.2-ee.0Copy the code

Download gitlab – ee_10. 7.2 ee. 0

Version 10.7.2-EEE.0 is now installed.

Configure and start GitLab

sudo gitlab-ctl reconfigure
Copy the code

Whenever the configuration of GitLab is modified, the following command should be executed to restart the configuration and restart GitLab, and then check the status of GitLab

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
sudo gitlab-ctl status
Copy the code

Open your browser and visit http://localhost by default. If the IP address and port are changed, access http://ip:

This is the new GITLab

Problems with gitLab installation

The fracture is occupied

Ports 80 and 8080 are used by Apache, Tomcat, and Nginx services on the Ubuntu server, respectively.

I did this by modifying the /etc/gitlab/gitlab.rb file

vim /etc/gitlab/gitlab.rb
Copy the code

Change the value of external_url

Manually back up GitLab on the old server

Modify the gitlab backup address

The default backup address is var/opt/gitlab/backups

  1. Open the original GitLab server terminal and pass the modificationgitlab.rbConfigure files to change the default directory for storing backup files
sudo vim /etc/gitlab/gitlab.rb
Copy the code
  1. The default is as follows
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
Copy the code
  1. willbackup_pathChange to a custom directory
gitlab_rails['backup_path'] = "/data/gitlab/backups"
Copy the code

  1. Then restart the configuration file
sudo gitlab-ctl reconfigure
Copy the code

In my example, there is no change, but the backup time is changed to 7 days

/etc/gitlab/gitlab.rb

# Change the external_url to the address your users will type in their browser
external_url 'http://192.168.3.2'
unicorn['port'] = 10086
gitlab_rails['manage_backup_path'] =true
gitlab_rails['backup_path'] ="var/opt/gitlab/backups"
gitlab_rails['backup_archive_permissions']=0644
gitlab_rails['backup_keep_time'] = 604800Copy the code

An error occurs during configuration restart

I don’t know how to solve the problem of radis. Some bosses know, please inform, but it does not affect the backup

The problem is that I executed sudo chmod -r 777 gitlab to grant read, write, and execute permissions to all files in the gitlab subdirectory

The solution

sudo chmod -R 2770 /var/opt/gitlab/git-data/repositories
Copy the code

Gitlab starts backup

Backup gitLab data on the original server by command.

sudo gitlab-rake gitlab:backup:create
Copy the code

Backup files are stored in var/opt/gitlab/backups. The backup files are named 1558509153_2019_05_22_10.7.2-ee_gitlab_backup.tar, which contains the backup time and version of Gitlab.

You can see backups under Backups

Manually transfer the GitLab backup package SCP to the new server

Through the SCP command

Please do not poke the Linux SCP command

SCP 1559635752 _2019_06_04_10. 7.2 ee_gitlab_backup. Tar [email protected]: / var/opt/gitlab/backupsCopy the code

New backups appear under /var/opt/gitlab/backups on the new server

Restart found that all backup packages and scripts are cleared

Backups were sent to/TMP due to backups permissions.

When you restart the PC, Linux automatically clears the contents in the/TMP folder

The reason is that TMP is a special folder, the system will automatically clean, so you should not put files in this folder, it will be bad.

Manually restore the GitLab backup package on the new server

Example Modify backup file permissions

If you don’t have permission to modify backups directories, the following problems occur

The following errors occur when you restore hastily

The correct operation is to reduce the backups operation permission


sudo chown git backups/
sudo chmod 700 backups/
Copy the code

In order to avoid the problem of undecompression due to permissions when GitLab is restored, we changed the permissions of backup files to 777(readable and writable).

Chmod 777 1558509153 _2019_05_22_10. 7.2 ee_gitlab_backup. TarCopy the code

Stop related data connection services

sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
Copy the code

Restore Gitlab from backup files

BACKUP equals the gitlab version number,_gitlab_backup.tar is added by default

Sudo gitlab - rake gitlab: backup, restore backup = 1558509153 _2019_05_22_10. 7.2 eeCopy the code

When the installation starts, there are two interactions

  1. Whether to get rid of the warehouse

2. Determine whether to discard the previous key file

3 Installation Succeeded

Open GitLab and access

sudo gitlab-ctl start
Copy the code

Open a browser and visit Gitlab

Automatically back up GitLab on the old server

/var/opt/gitlab directory on the old server

Regular backup

Add a scheduled task to perform a GitLab backup every day at 6am

Add a scheduled task, 6 o 'clock every day, automatically execute the old server on the GitLab backup operation
0  6    * * *   root    /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
Copy the code

After compiling the /etc/crontab file, restart the cron service

Reload the cron configuration file
sudo /usr/sbin/service cron reload
Restart the CRon service
sudo /usr/sbin/service cron restart
Copy the code

Note: 6 is preceded by 0, not * and the cron service needs to be restarted finally

Set the backup expiration time

Even if there were 6 o ‘clock backups every day, that would be 356 copies a year

In this case, you can set the backup expiration time. Only the compressed packages that have not expired are stored in the backup directory, greatly reducing the pressure on the server

Find gitlab_rails[‘ backup_keep_time ‘] by editing the /etc/gitlab/gitlab.rb configuration file

sudo vim /etc/gitlab/gitlab.rb
Copy the code

Change gitlab_rails[‘ backup_keep_time ‘] to gitlab_rails[‘ backup_keep_time ‘]=604800

I set it to expire in 7 days

Automatically transfer the GitLab backup package SCP to the new server

Old and new servers pair keys

Because the SCP is always interactive, it prompts for a password

Plan 1: Pair the password and cancel the password

Scheme 2: Expect automatic interaction

So here we go to plan one

Generating a key pair

On the old server, enter the command

ssh-keygen -t rsa
Copy the code

  1. In the process of generating prompt input key pair save location, directly enter, accept the default value on the line.
  2. The root/.ssh/id_rsa file already exists, so you are prompted whether to overwrite it. Enter y to indicate overwrite
  3. It will then prompt you for a password and press Enter, leaving it empty. Of course, you can also enter a password.
  4. Enter the confirm password, and then enter the key pair.

SSH generates two files id_rsa and id_rsa.pub.

The public key is saved in /root/.ssh/id_rsa.pub, and the private key is saved in /root/.ssh/id_rsa.

cd /home/gushenxing/.ssh/
Copy the code

Cp generates an RSA public key certificate on the old server and sends it to the new server

In /root/. SSH, copy and back up A copy of id_rsa.pub.

Run the cp command to copy data

cp id_rsa.pub id_rsa.pub.A
Copy the code

Execute SCP command transfer

SCP id_rsa. Pub. A [email protected]: / home/gitlab - backup /. SSHCopy the code

Access the new server to view the public key

The SCP command requires the password to be entered. After the third step is performed, the old server does not need to enter the password again if the SCP command is used to copy files to the new server.

Key pairs

Create the authorized_keys file

In the second step, copy the id_rsa.pub.A file from the old server to the /root/.ssh directory on the new server

We create the authorized_keys file under /root/.ssh on the new server using the following command

touch authorized_keys
Copy the code

Append the contents of the id_rsa.pub.A file to the authorized_keys file

Open authorized_keys to view

Change the permission of the authorized_keys file

Change the permission of the authorized_keys file

The authorized_keys file permissions are important, if set to 777, you will still need to provide the password when logging in.

test

Whether a password is still required to copy files from the old server to the new server using the SCP command

SCP 1559635752 _2019_06_04_10. 7.2 ee_gitlab_backup. Tar [email protected]: / var/opt/gitlab/backupsCopy the code

On the new server, you can run the command again and find that you do not need to enter the password

Example Create a Shell periodic remote backup script

Create a scheduled remote backup script on the new server

Create scripts and log directories for automatic SCP

cd /var/opt/gitlab
touch auto_scp.sh
cd /backups
sudo mkdir log
Copy the code

Because we will put the execution time of the scheduled remote backup script auto_scp.sh within one hour after the Gitlab automatic backup script auto_scp.sh, we only need to execute the remote backup script auto_scp.sh every time. Just cp the new Gitlab backup files generated within an hour.

auto_scp.sh

#! /bin/bash

Old and new servers gitlab backup file storage path
BACKUPDIR=/var/opt/gitlab/backups

# Remote backup server login account
RemoteUser=gitlab-backup

# IP address of the remote backup serverRemoteIP = 192.168.3.113# Current system date
DATE=`date "+%Y-%m-%d-%H-%M-%S"`

#Log storage path
LogFile=$BACKUPDIR/log/$DATE.log

Gitlab backup files in local backup directory within 1 day with suffix.tar
BACKUPFILE_SEND_TO_REMOTE=$(find $BACKUPDIR -type f -mmin -1440 -name '*.tar')

Create a log file
touch $LogFile

Append logs to log files
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

echo "gitlab auto backup to remote server, start at $DATE" >> $LogFile

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

Print the filename of each SCP file
echo "the file to scp to remote server is $BACKUPFILE_SEND_TO_REMOTE" >> $LogFile

Backup to remote server
scp $BACKUPFILE_SEND_TO_REMOTE $RemoteUser@$RemoteIP:$BACKUPDIR
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

echo "remote server is $RemoteUser@$RemoteIP:$BACKUPDIR" >> $LogFile

Append logs to log files
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile
~                      
Copy the code

test

Since the auto_scp.sh execution permission is root, you also need to exchange passwords

Example Reduce the auto_scp.sh and log permissions

sudo chown gushenxing:gushenxing auto_scp.sh 
sudo chown gushenxing:gushenxing log
Copy the code
  1. Execute commands, not sudo
bash auto_scp.sh 
Copy the code

No password is required at this point

  1. Viewing log Files

  1. The backup package has been found on the new server

Add scheduled tasks to crontab to automatically execute SCP scripts

sudo vim /etc/crontab
Copy the code

Add the following command

Add scheduled task, 7 o 'clock every day, automatically execute gitLab backup package SCP to the new service7 * * * gushenxing bash /var/opt/gitlab/auto_scp.shCopy the code

After compiling the /etc/crontab file, restart the cron service

Reload the cron configuration file
sudo /usr/sbin/service cron reload
Restart the CRon service
sudo /usr/sbin/service cron restart
Copy the code

The operation on the old server is complete with automatic backup of GitLab on the old server and automatic SCP of gitLab backup package to the new server

Automatically restore gitLab backup packages on new servers

Automatically deletes expired backup package scripts

cd /var/opt/gitlab
sudo touch auto_remove_backup.sh
Copy the code

Fill in the script code

#! /bin/bash

Gitlab remote backup server backup file path
GITLABDIR=/var/opt/gitlab/backups

Gitlab backup files that are more than 7 days old and have file extension.tar on the remote backup path, then delete them
find $GITLABDIR -type f -mtime +7 -name '*.tar' -exec rm {} \;
Copy the code

Add a scheduled task to crontab to automatically execute the script for deleting backup packages

sudo vim /etc/crontab
Copy the code

Add the following command

Add regular task, every day at 8 am, automatically delete expired GitLab backup file operation
0  8    * * *   root    bash /var/opt/gitlab/auto_remove_backup.sh
Copy the code

After compiling the /etc/crontab file, restart the cron service

Reload the cron configuration file
sudo /usr/sbin/service cron reload
Restart the CRon service
sudo /usr/sbin/service cron restart
Copy the code

Automatic recovery script

cd /var/opt/gitlab
sudo touch auto_recovery_backup.sh
Copy the code
#! /bin/bash

# Backup file directory
REMOTEDIRBACKUPS=/var/opt/gitlab/backups

Find the backup file generated on that day
BACKUPFILE=$(sudo find ./ -mtime -1 -name '*.tar')

# Current system date
DATE=`date "+%Y-%m-%d-%H-%M-%S"`

#Log storage path
LogFile=$REMOTEDIRBACKUPS/log/$DATE.log

Create a log file
sudo touch $LogFile

Append logs to the log directory
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

echo "gitlab auto recovery, start at $DATE" >> $LogFile

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

Print the backup files generated on that day
echo "backup files generated on that day is $BACKUPFILE" >> $LogFile


# intercept the main file name
FILE=$(echo ${BACKUPFILE#*backups/})

# intercept recovery file name
RESTOREFILE=$(echo ${FILE%_gitlab*})



echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

Print out the backup file copy destination directory
echo "backup files generated scp to $REMOTEDIRBACKUPS" >> $LogFile

Enter the target directory
cd $REMOTEDIRBACKUPS

Change the backup file permission to readable, writable, and executable
chmod 777 $FILE

Stop the related data connection service
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

# Automated interaction to restore GitLab from backup files
sudo /usr/bin/expect -c " 
set timeout -1
spawn sudo gitlab-rake gitlab:backup:restore BACKUP=$RESTOREFILEexpect { \"yes/no\" {set timeout -1; send \"yes\n\"; exp_continue; } \"yes/no\" {set timeout -1; send \"yes\n\"} } expect eof"
# open gitlab
sudo gitlab-ctl start

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile

Restore gitlab from the backup file
echo "recovery was successful" >> $LogFile

Append logs to the log directory
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile


Copy the code

Add automatic interactive expect

test

The execution result

Add a scheduled task to crontab to automatically execute the backup package restoration script

sudo vim /etc/crontab
Copy the code

Add this scheduled task to crontab

Add scheduled task, every day at 9 am, automatically execute the backup package restore gitlab operation
0  9    * * *   root    bash /var/opt/gitlab/auto_recovery_backup.sh
Copy the code

After compiling the /etc/crontab file, restart the cron service

Reload the cron configuration file
sudo /usr/sbin/service cron reload
Restart the CRon service
sudo /usr/sbin/service cron restart
Copy the code

Full version script

Crontab scripts on older servers

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Add scheduled task, 6 o 'clock every day, automatically execute the old server on the GitLab backup > operation
0  6    * * *   root    /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

Add scheduled task, 7 o 'clock every day, automatically execute gitLab backup package SCP to the new service7 * * * gushenxing bash /var/opt/gitlab/auto_scp.shCopy the code

Crontab script on the new server

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Add regular task, every day at 8 am, automatically delete expired GitLab backup file operation
0  8    * * *   root   bash /var/opt/gitlab/auto_remove_backup.sh

Add scheduled task, every day at 9 am, automatically execute the backup package restore gitlab operation
0  9    * * *   root   bash /var/opt/gitlab/auto_recovery_backup.sh
Copy the code

Automatically SCP the GitLab backup package to the new server scriptauto_scp.sh

#! /bin/bash



Old and new servers gitlab backup file storage path

BACKUPDIR=/var/opt/gitlab/backups



# Remote backup server login account

RemoteUser=gitlab-backup



# IP address of the remote backup serverRemoteIP = 192.168.3.113# Current system date

DATE=`date "+%Y-%m-%d-%H-%M-%S"`



#Log storage path

LogFile=$BACKUPDIR/log/$DATE.log



Gitlab backup files in local backup directory within 1 day with suffix.tar

BACKUPFILE_SEND_TO_REMOTE=$(find $BACKUPDIR -type f -mmin -1440 -name '*.tar')



Create a log file

touch $LogFile



Append logs to log files

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



echo "gitlab auto backup to remote server, start at $DATE" >> $LogFile



echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



echo "remote server is $RemoteUser@$RemoteIP:$BACKUPDIR" >> $LogFile



Append logs to log files

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile
Copy the code

Automatically execute the script to delete expired GitLab backup filesauto_remove_backup.sh

#! /bin/bash

Gitlab remote backup server backup file path
GITLABDIR=/var/opt/gitlab/backups

Gitlab backup files that are more than 7 days old and have file extension.tar on the remote backup path, then delete them
find $GITLABDIR -type f -mtime +7 -name '*.tar' -exec rm {} \;

Copy the code

Automatically execute the restore gitlab script from the backup packageauto_recovery_backup.sh

#! /bin/bash



# Backup file directory

REMOTEDIRBACKUPS=/var/opt/gitlab/backups



Find the backup file generated on that day

BACKUPFILE=$(sudo find ./ -mtime -1 -name '*.tar')



# Current system date

DATE=`date "+%Y-%m-%d-%H-%M-%S"`



#Log storage path

LogFile=$REMOTEDIRBACKUPS/log/$DATE.log



Create a log file

sudo touch $LogFile



Append logs to the log directory

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



echo "gitlab auto recovery, start at $DATE" >> $LogFile



echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



Print the backup files generated on that day

echo "backup files generated on that day is $BACKUPFILE" >> $LogFile





# intercept the main file name
RESTOREFILE=$(echo ${FILE%_gitlab*})


echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



Print out the backup file copy destination directory

echo "backup files generated scp to $REMOTEDIRBACKUPS" >> $LogFile



Enter the target directory

cd $REMOTEDIRBACKUPS



Change the backup file permission to readable, writable, and executable

chmod 777 $FILE
Stop the related data connection service

sudo gitlab-ctl stop unicorn

sudo gitlab-ctl stop sidekiq



# Automated interaction to restore GitLab from backup files

sudo /usr/bin/expect -c " 

set timeout -1

spawn sudo gitlab-rake gitlab:backup:restore BACKUP=$RESTOREFILEexpect { \"yes/no\" {set timeout -1; send \"yes\n\"; exp_continue; } \"yes/no\" {set timeout -1; send \"yes\n\"} } expect eof"

# open gitlab

sudo gitlab-ctl start



echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile



Restore gitlab from the backup file

echo "recovery was successful" >> $LogFile


Append logs to the log directory

echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" >> $LogFile




Copy the code

reference

Gitlab website

Gitlab installation in ubuntu16.04

Gitlab automatically backs up

GitLab failed to be restored on CenterOS: tar: Unpacking backup failed was deleted from GitLab

How to view the GitLab version number

Uninstall Gitlab on Linux

Git learn ——> Gitlab how to backup and restore and migrate?

Install GitLab on CenterOS and use your own domain name to access the GitLab management page

Linux permission supplement: RWT RWT RWS RWS special permission

This article was first published on the public account “Front-end Keep”. Welcome to follow it.

Finally, I hope you must point to like three times.

Follow my blog: blog address