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

相关推荐

  • 使用 B VPS 中转 A VPS 流量

    引言 由于某些不可抗力原因, 所处网络环境无法访问 A VPS, 因此 增加 能访问到的 B VPS 来中转流量到 A VPS me –x-> A VPS me <—-> B VPS <—-> A VPS 非内网穿透需求, A VPS 有公网ip, 可被公网直接发现 和使用某些客户端 配合 Cloudflare…

    2024年6月28日
    1.5K00
  • Linux显示磁盘空间不足,/tmp文件夹占用100%问题分析和解决方案

    系统运行过程中发现无法上传文件,第一反应是磁盘空间不足导致,登录服务器后查看文件存放的空间充足,但是无法上传文件且命令行的tab键自动补充也提示空间不足。最后经过搜索和分析发现是/tmp 文件夹占用100%导致。以下记录分析和解决流程。 问题分析         服务器环境        …

    2023年12月22日
    1.5K00
  • CentOS添加永久路由方式

    1. CentOS7添加永久路由   ● 方式一:  执行以下命令打开路由配置文件进行编辑: 这里的<interface>是指您要添加路由的网络接口,例如eth0或ens33  在文件中添加路由规则,例如: 其中<gateway>是默认网关的 IP 地址,<interface>是网络接口的名称  例如: 保存并关闭文件,执行以下命令重启网络…

    2023年12月5日
    53500
  • Ping命令详解

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

    2023年1月4日
    73500
  • Centos7忘记root密码怎么办

    重启linux,进入系统的GRUB菜单界面,选择最下方的,按下小写字母e进入编辑界面 按下方向键,找到以字符串Linux16开头的行,光标移动到结尾,然后输入init=/bin/bash enforcing=0 代表登录系统后,加载bash解释器,然后关闭selinux 按下ctrl+x组合键以单用户模式启动Linux 输入如下命令,重新挂载根目录,进入可写…

    2022年6月11日
    92900

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信