前言
分别在个人电脑、个人服务器、码云三个地方建立了数据仓库用于保存自己的各种数据,通过git
+shell
进行数据同步。
此举不仅可以避免因存储损坏、版本更迭、数据误操作等因素带来的各种麻烦,也能实现各种环境下的便捷开发、合作开发。
这篇博客属于事件记录,因此只贴出代码,望诸君见谅!
建立仓库
服务端(Ubuntu18.04):
# 为git仓库创建管理员
su root
adduser git
... # 输入密码信息,也可以在root下输入命令 sudo passwd ${username} 重置密码
su git
cd ~
# 在git用户目录下建立仓库,假设git已安装好
mkdir db_1;cd db_1;
git --bare init; # 远程仓库必须加--bare参数,否则以后会出问题
# 创建存放密钥认证文件,并放入客户端公钥(不演示)
vim ~/.ssh/authorized_keys;
客户端(windows_7):
# 创建仓库
git init
# 推送文件或下拉文件,在此以shell脚本形式贴出
码云:
码云需要创建仓库,同上面一样建立两个分支,然后将公钥加入到个人信息中,具体自查。
数据同步
# push.sh
# ========================================================================= #
#!/bin/bash
remoteAd_1=giteeAddress;
remoteAd_2=serverAddress;
remoteBranch=develope;
name=user;
time=`date '+%Y-%m-%d %H:%M:%S'`
# according to the second parameter, choose the branch
if [ -n "${2}" ]; then
if [ ${2} == "m" ]; then
echo -e "\e[37;41m remoteBranch is [ master !!!] ... \e[0m";
remoteBranch=master;
fi
else
echo -e "\e[37;41m remoteBranch is [ develope !!!] ... \e[0m";
fi
# send file to remote database
function push(){
echo -e "\e[32;40m \n step 1: git add .\n \e[0m"; # shell字体颜色设置
git add .;
echo -e "\e[32;40m \n step 2: git commit -m \n \e[0m"
git commit -m "${time} -- ${name}";
echo -e "\e[32;40m \n step 3: push the studyNote to my server... \n \e[0m"
git pull ${remoteAd_1} ${remoteBranch}:master;
git push ${remoteAd_1} master:${remoteBranch};
echo -e "\e[32;40m \n step 4: push the studyNote to my server... \n \e[0m"
git pull ${remoteAd_2} ${remoteBranch}:master;
git push ${remoteAd_2} master:${remoteBranch};
}
# get file from remote database
function get(){
echo -e "\e[32;40m \n step 1: get the studyNote from gitee....\n \e[0m"
git pull ${remoteAd_1} ${remoteBranch}:master;
}
# choose the way of p(push) or g(pull)
if [ -n "${1}" ]; then
if [ ${1} == "p" ]; then
push
elif [ ${1} == "g" ]; then
get
fi
else
echo -e "\e[31;40m \n ======= please input your args !!!!!! ======= \e[0m";
fi
# ========================================================================= #