云服务器上的目录定时同步到Github

具体操作与脚本

第一 在Github上创建私有仓库
这一步相信大家都知道怎么操作

第二 在服务器拉取Github仓库并把.git目录复制到需要同步的目录下
假设服务器需要同步的目录为:/usr/share/nginx/html/imgs

git clone git@github.com:xxxxxx/xxx.git
cd xxx
mv .git /usr/share/nginx/html/imgs/

cd /usr/share/nginx/html/imgs/
git init
git add .
git commit -am 'first commit'
git branch -M master
git push -u origin master

第三 编写定时同步脚本
具体脚本如下,如脚本位置: /root/script/sync_to_github.sh

#!/bin/bash

cd $1

has_push=false
untrack=`git status | grep 'Untracked files'`
if [ -n "$untrack" ]; then
  git add .
  has_push=true
fi
change_staged=`git status | grep 'Changes'`
if [ -n "$change_staged" ]; then
  git commit -am 'auto commit ...'
  has_push=true
fi

if [ "$has_push" = true ]; then
  git push
fi
chmod +x /root/script/sync_to_github.sh

第四 通过Linux的crontab表达式定时执行同步
编辑crontab

# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 3 * * * root /root/script/sync_to_github.sh /usr/share/nginx/html/imgs

重启crond服务

# 我的服务器是Centos7 其他服务器请自行百度重启crond服务的方法
systemctl restart crond

文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/8174

(0)
郭靖的头像郭靖
上一篇 2023年5月11日 下午4:43
下一篇 2023年5月15日 下午5:16

相关推荐

  • MySQL 如何使用离线模式维护服务器

    离线模式 作为 DBA,最常见的任务之一就是批量处理 MySQL 服务的启停或其他一些活动。在停止 MySQL 服务前,我们可能需要检查是否有活动连接;如果有,我们可能需要把它们全部杀死。通常,我们使用 pt-kill 杀死应用连接或使用 SELECT 语句查询准备杀死语句。例如: MySQL 有一个名为 offline_mode 的变量…

    2023年10月20日
    59900
  • CentOS如何修改root用户登录时默认目录

    如何修改root用户默认目录,修改/root/.bash_profile文件,只要加一行cd 路径就可以。 修改其它用户的配置,这个文件应该是在/home/username/.bash_profile(username为登录用户名)。 如何修改用户主机名?

    2023年11月7日
    52000
  • Ping命令详解

    命令简介 ping 命令是 Linux 系统中一个非常常用的网络命令。ping 命令主要用于测试网络的连通性,也可用于测试网络的性能和主机的响应能力。 日常工作中,我们经常会遇到网页无法打开、网址无法请求的情况。这个时候我们的一般操作是 ping 一下网址,比如 ping baidu.com 使用 …

    2023年1月4日
    67500
  • CentOS 7搭建本地yum源和局域网yum源

    看到这个都是情非得已的,话不多说,直接盘 搭建本地yum源 本地yum源,就意味着只有搭建该yum源的这台服务器能使用,其它的服务器都不能使用该yum源。搭建本地yum源的所有步骤如下: #准备好一个centos 的镜像,我这里是CentOS-7-x86_64-DVD-1810.iso # 在/local-yum目录创建挂载镜像的文件夹 # 将iso镜像挂载…

    2023年5月4日
    59700
  • 安装 Smem – Linux工具 安装条件: 现代内核 (版本号高于 2.6.27) 较新的 Python 版本 (2.4 及以后版本) 可选的 matplotlib 库用于生成图表 对于当今的大多数的 Linux 发行版而言,内核版本和 Python 的版本都能够满足需要,所以仅需要为生成良好的图表安装 matplotlib 库。 RHEL,…

    2023年8月25日
    80200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

在线咨询: QQ交谈

邮件:712342017@qq.com

工作时间:周一至周五,8:30-17:30,节假日休息

关注微信