docker版gitlab备份脚本

创建脚本 gitlabRepBackup.sh

#!/bin/bash
#set -x

starttime=`date +'%Y-%m-%d %H:%M:%S'`

local_bak_path=/home/gestep/gitlab_backup/backups
gitlab_bak_path=/var/opt/gitlab/backups

grb=/etc/gitlab/gitlab.rb
gngx=/var/opt/gitlab/nginx/conf
tname=$(date +'%Y-%m-%d-%H')

if [[ ! -d ${local_bak_path} ]]; then
	mkdir -p ${local_bak_path}
fi

# delete local backup files
function remove_local_bak()
{
	echo "--------------------------------删除本地历史备份---------------------------------"
	num=$(ls $local_bak_path | wc -l)
	# save 15 file
	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] exec delete..........."
	if [[ $num > 15 ]]; then
		find $local_bak_path -name "*.tar" -mtime +7 -exec rm -f {} \;
		if [[ $? -eq 0 ]]; then
			echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] delete files successfuily."
		fi
	else
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] No need to delete."
	fi
}

# docker gitlab data backup
function remove_history()
{
	echo "--------------------------------删除容器历史备份---------------------------------"
	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] start remove history file......"
	sudo docker exec gitlab find $gitlab_bak_path -name "*.tar" -exec rm -f {} \;
	sudo docker exec gitlab find $gitlab_bak_path -name "*.tar.gz" -exec rm -f {} \;
	hnum=$(docker exec gitlab ls $gitlab_bak_path | wc -l)
	if [[ $hnum -eq 0 ]]; then
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] remove history file successfuily."
	else
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] remove history file faild!"
	fi
}

# backup config 
function backup_config()
{
	echo "--------------------------------归档配置文件---------------------------------"
	sudo docker exec gitlab tar -zcPf $gitlab_bak_path/config-$tname.tar.gz $grb $gngx 
	cnum=$(docker exec gitlab ls $gitlab_bak_path/config-$tname.tar.gz | wc -l)
	if [[ $cnum -gt 0 ]]; then
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] create config backup file successfuily."
	fi
}

# create backup file
function create_bak_file()
{
	echo "-------------------------------开始备份(生成)---------------------------------"
	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] start create gitlab backup file......"
	docker exec gitlab gitlab-rake gitlab:backup:create
	hnum=$(docker exec gitlab ls $gitlab_bak_path | wc -l)
	if [[ $hnum -gt 0 ]]; then
                echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] create backup file successfuily."
        fi
}

# copy backup file
function copy_backup_file()
{
	echo "-----------------------------从容器COPY备份到宿主机-----------------------------"
	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] start copy backup file......"
	sudo docker cp gitlab:$(sudo docker exec gitlab find $gitlab_bak_path -type f -mtime -1 -name '*.tar') $local_bak_path
	sudo docker cp gitlab:$(sudo docker exec gitlab find $gitlab_bak_path -type f -mtime -1 -name 'config-*') $local_bak_path
	gitlab_conf_tar=$(find $local_bak_path -type f -mtime -1 -name 'config-*' | wc -l)
	gitlab_tar=$(find $local_bak_path -type f -mtime -1 -name '*.tar' | wc -l)
	if [[ $gitlab_tar -gt 0 ]]; then
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] copy backup file successfuily."
	fi
	if [[ $gitlab_conf_tar -gt 0 ]]; then
		echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] copy config file successfuily."
	fi
}

# Initiate a remote backup
function remote_backups()
{
	echo "--------------------------------远程备份---------------------------------"
	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] start remote backups......."
	TAR=$(date +'%Y-%m-%d')
	# gitlab_file=`find $local_bak_path -type f -mtime -1 -name '*.tar'`
	gitlab_file=`find $local_bak_path -maxdepth 1 -name '*.tar' -newermt $TAR`
	echo $gitlab_file
	./expect_scp $gitlab_file
	./expect_scp $local_bak_path/config-$tname.tar.gz
}

function run()
{
	echo "--------------------------------开始运行---------------------------------"
	remove_local_bak
	remove_history
	backup_config
	create_bak_file
	copy_backup_file
	remote_backups

	endtime=`date +'%Y-%m-%d %H:%M:%S'`
	start_seconds=$(date --date="$starttime" +%s);
	end_seconds=$(date --date="$endtime" +%s);

	echo "["$(date +%Y-%m-%d" "%H:%M:%S)"] 本次运行时间: "$((end_seconds-start_seconds))"s"
}

run

创建脚本 expect_scp

#!/usr/bin/expect

if {$argc < 1} {
    send_user "usage: $argv0 src_file\n"
    exit
}
set src_file [lindex $argv 0]
set timeout -1

spawn scp -pr $src_file gitlab@192.168.2.28:/home/gestep/gitlab-backups
expect {
        "(yes/no)?"
        {
                send "yes\n"
                expect "*assword:" {
                        send "123456@2021%&\n"
                }
        }
        "*assword:"
        {
                send "123456@2021%&\n"
        }
        eof
        {
                send_user "eof"
        }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值