定时上传 Linux 服务器代码到 GitHub

使用国外的服务器不知道什么时候就会出现服务器崩溃停机、数据全清等问题,为防止丢失数据需要随时保证自己有一套完整的代码和数据库。

操作之前备份,操作之前备份,操作之前备份

注意服务器端最好是单独一个仓库只用来服务器备份,否则容易出现版本冲突需要手动合并,那就得不偿失了

实现:将服务器上面的代码上传到 GitHub , 使用 crontab 定时备份上传代码,备份数据库到项目目录,让 MySQL 跟随项目代码一起上传到 GitHub

一、 服务器上生成秘钥

(执行第二条命令时需要输入密码,直接忽略点击回车再回车)

# 进入目录
cd ~/.ssh
# 生成证书,邮箱改为自己 GitHub 绑定的邮箱
ssh-keygen -f id_rsa -C "***@***.com"

二、 将公钥上传到 GitHub

# 使用 cat 查看公钥内容,复制保存到 GitHub SSH keys 中
cat id_rsa.pub

步骤:GitHub==> 点击右上角头像 ==> Settings ==> SSH and GPG keys ==> New SSH keys

三、验证是否添加成功

# ssh -T git@github.com

[root@HKLite981478 .ssh]# ssh -T git@github.com
Hi YaLe1213! You've successfully authenticated, but GitHub does not provide shell access.

四、创建 GitHub 私人仓库

五、 上传代码到仓库(远程仓库为 SSH 链接)

# 进入站点目录
cd /www/wwwroot/test-app-mysql/
# 初始化
git init
# 配置用户名密码
git config --global user.email "***@***.com"
git config --global user.name "***"
# 关联到远程仓库
git remote add origin git@github.com:***/***.git

# 将文件添加到缓存区
git add .
# 提交缓存区的文件
git commit -m "backup"
# 上传到 github 的主分支
git push -u origin master

六、 添加计划任务定时上传

#! /bin/bash
cd /www/wwwroot/test-app-mysql
git add .
git commit -m "`date '+%D'`"
git push origin master

七、 完整过程

[root@HKLite981478 test-app-mysql]# cd ~/.ssh
[root@HKLite981478 .ssh]# ls
known_hosts
[root@HKLite981478 .ssh]# ssh-keygen -f id_rsa -C "***@***.com"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
SHA256:XtqDXoIo8P3pSTJWBoLdVgCS05bvyPswf/CxNE/U2zg ***@***.com
The key's randomart image is:
+---[RSA 2048]----+
|.o.o...          |
|o++. .           |
|.oo.+    .       |
|   o..  . .      |
|.. o  o.S .+     |
| oo.o++o.=E .    |
|  =.*+o*= +.     |
|  .* =++.o .     |
|   .oo= .        |
+----[SHA256]-----+
[root@HKLite981478 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@HKLite981478 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EA****************fBzreNG5w354+vYC7w8Hn ***@***.com
[root@HKLite981478 .ssh]# ssh -T git@github.com
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
[root@HKLite981478 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@HKLite981478 .ssh]# cd /www/wwwroot/test-app-mysql/
[root@HKLite981478 test-app-mysql]# ls
id_rsa  id_rsa.pub  mysql
[root@HKLite981478 test-app-mysql]# touch 111 readme.md
[root@HKLite981478 test-app-mysql]# ls
111  id_rsa  id_rsa.pub  mysql  readme.md
[root@HKLite981478 test-app-mysql]# git add .
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like '****' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

Run 'git status' to check the paths you removed from your working tree.

[root@HKLite981478 test-app-mysql]# git commit -m "test commit"
[master cd7976e] test commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 111
 create mode 100644 readme.md
[root@HKLite981478 test-app-mysql]# git push origin
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: To git@github.com:***/test-app-mysql.git
   f321c72..cd7976e  master -> master
[root@HKLite981478 test-app-mysql]# 

如何将 MySQL 导出到项目目录?

在这里我先说一下我的同步方案,首先是需要考虑导出,我使用的是宝塔面板直接使用计划任务定时备份数据库(备份文件会保存到 /www/backup/database 这个目录内);
再一个问题就是同步到站点目录,我使用的是 “文件同步工具” 创建一个本地同步,任务名称随便写,从 /www/backup/database 备份目录 同步到 /www/wwwroot/test-app-mysql/mysql/ 站点目录下的 MySQL目录,创建成功后只要数据库备份目录有添加或修改都会实时同步到项目下的 MySQL 目录;以上两个问题都解决后就可以直接使用 Git 将代码和 MySQL 上传到 GitHub 了。

后续再出命令导出 MySQL 到项目目录的笔记

posted @ 2023-07-08 03:03  孤陌  阅读(61)  评论(0编辑  收藏  举报