云服务器上的目录定时同步到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如何通过Nexus代理yum源

    一 背景 公司的某些服务器不允许连接外网,造成这些服务器需要安装软件时非常不方便,此文章则是介绍如何通过Nexus3.x代理yum源,做到通过yum install来安装软件。 二 创建yum代理仓库 三 服务器上增加yum源 四 Yum安装软件

    2023年5月15日
    15600
  • 限制登录Linux服务器的几种方式

    在日常运维中,我们通常使用防火墙iptables跟firewalld的方式来实现访问控制,但在实际环境中,开启防火墙可能会对业务造成影响,所以以下整理了三种限制登录Linux服务器的几种方式。分别是修改TCP Wrappers服务访问控制、修改sshd_config配置文件、防火墙策略iptables跟firewalld。以上三种方式可以针对安全厂商做的漏洞…

    2023年2月28日
    17100
  • cobbler搭建

    什么是cobbler Cobbler是一个Linux系统安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。 Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。 Cobbler是较早前的kickstart的升…

    2022年11月26日
    20700
  • Centos7.6安装MySQL(超详细)

    1、查看是否已经安装Mysql rpm -qa | grep mysql 我已经安装过了 如果你查看出来有东西,可以使用下面命令将其删除 rpm -e 文件名 如果提示warning: waiting for transaction lock on /var/lib/rpm/.rpm.lock,则需要执行 2、下载官方Mysql包 wget -i -c ht…

    2022年6月9日
    51000
  • Centos7系统如何查看系统日志

    Systemd是Linux系统工具,Systemd拥有强大的解决与系统日志记录功能-systemd-journald。日志目录一般是在/var/log/journal,记录的是二进制文件,我们可以通过journalctl进行查看。 日志的配置文件是/etc/systemd/journald.conf。 常用的操作有哪些呢 显示所有日志 查看系统本次启动只有的…

    2022年6月15日
    1.3K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信
云服务器上的目录定时同步到Github