云服务器上的目录定时同步到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

相关推荐

  • CentOS 7 安装 JDK 11

    现在很多编译器和软件都开始要求使用 JDK 11 了。 因此我们希望在 CentOS 上安装 JDK 11。 运行下面的命令: 如果你的系统中还装有不同版本的 JDK 的话。你需要运行: 来选择默认的 JDK。 随后你就可以验证安装的 JDK 了。

    2023年3月11日
    27600
  • Sar—Linux 上最为全面的系统性能分析工具之一

    sar(System Activity Reporter 系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁盘 I/O、CPU 效率、内存使用状况、进程活动及 IPC 有关的活动等。 我们可以使用sar命令来获得整个系统性能的报告。这有助于我们定位系统性能的瓶…

    2023年4月13日
    17800
  • Linux服务器疑似被入侵,教你怎么去排查

    随着开源产品的越来越盛行,作为一个Linux运维工程师,能够清晰地鉴别异常机器是否已经被入侵了显得至关重要,个人结合自己的工作经历,整理了几种常见的机器被黑情况供参考。 背景信息:以下情况是在CentOS 6.9的系统中查看的,其它Linux发行版类似。 1、入侵者可能会删除机器的日志信息,可以查看日志信息是否还存在或者是否被清空,相关命令示例: [root…

    2023年2月23日
    38400
  • Linux 修改系统时间的两种方式

    我们一般使用“date -s”命令来修改系统时间。比如将系统时间设定成2010年4月5日的命令如下。 [root@rhel ~]# date -s 20100405 Mon Apr  5 00:00:00 CST 2010 将系统时间设定成14点31分0秒的命令如下 [root@rhel ~]# date -s 14:31:00 Mon Apr&n…

    2023年7月27日
    25500
  • 提高终端生产力神器 – tmux

    tmux github 一键安装 centos 系列可以直接用 yum debian 系列可以直接用 apt-get 编译安装 编译 libevent 编译 tmux 编译 tmux 还需要用到 ncurses 验证 tmux 是不是可用 如果出现报错:tmux: error while loading shared libraries: libevent_…

    2023年11月10日
    16600

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信