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

相关推荐

  • Linux 查看内核以及系统版本的3种方法

    1、使用uname命令查看 uname命令显示多个系统信息,包括Linux内核体系结构,名称版本和发行版。查看Linux内核版本,输入以下命令: uname -a uname -srm uname -r 2、使用hostnamectl命令查看 3、通过查看 /proc/version 文件确认 /proc 目录包含虚拟文件,其中包含有关系统内存,CPU内核,…

    2023年6月13日
    88600
  • Ubuntu篇—文件压缩与解压

    查看文件夹占用磁盘大小 du -sh 一:Ubuntu下解压和压缩tgz文件 参数: -c :create 建立压缩档案的参数; -x : 解压缩压缩档案的参数; -z : 是否需要用gzip压缩; -v: 压缩的过程中显示档案; -f: 置顶文档名,在f后面立即接文件名,不能再加参数 1 将tgz文件解压到指定目录 2 将指定目录压缩到指定文件 二:ubu…

    2022年6月22日
    1.2K00
  • find命令排除某些目录或文件

    使用-prune开关。例如,如果要排除misc目录,只需将a添加-path ./misc -prune -o到您的find命令中: find . -path ./misc -prune -false -o -name ‘*.txt’ 这是带有多个目录的示例: find . -type d \( -path dir1 -o -path dir2 -o -pat…

    2023年6月16日
    1.3K00
  • Linux SSH 登录失败多少次禁止该IP访问 防止暴力破解

    Linux 系统SSH 登录失败的内容会记录到/var/log/secure文件,通过查找关键字 Failed,可以定位到这些异常的IP地址,比如: 比如这里,明显这个IP地址在进行SSH 扫描,不断的更换端口和用户进行暴力测试。 在Linux里面有两个相关的文件: /etc/hosts.allow: 允许哪些IP访问主机 /etc/hosts.deny 禁…

    2023年3月1日
    1.2K00
  • HBase 高可用集群详细图文安装部署

    一、HBase 安装部署 1.1 Zookeeper 正常部署 首先保证 Zookeeper 集群的正常部署,并启动之。 1.2 Hadoop 正常部署  Hadoop 集群的正常部署并启动。 1.3 HBase 安装  HBase 官方下载地址:Apache Download Mirrors 1.4 HBase 的配置文件  1.4.1 hbas…

    2023年12月15日
    91200

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信