云服务器上的目录定时同步到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月9日 下午5:01
下一篇 2023年5月15日 下午5:16

相关推荐

  • Linux实验案例:构建DHCP服务器

    公司要求将闲置的一台Linux主机配置为DHCP服务器,以便为局域网内员工的办公用机提供自动分配IP地址服务,提高网络管理和维护的效率。 需求描述 给局域网内各主机自动分配的IP地址范围为192.168.10.20~192.168.10.30 局域网内各主机使用的默认网关地址为192.168.10.2 局域网内各主机使用的DNS服务器地址分别为202.96.…

    2024年6月24日
    2.3K00
  • smokeping修改Ping间隔和Ping包数量

    1、复制 所有带*.dist 2、修改httpd.conf 配置文件 3、修改 /usr/local/smokeping/etc/config

    2022年11月26日
    2.0K00
  • centos7下smartctl安装配置

    安装 使用

    2023年4月13日
    2.1K00
  • Ubuntu 输入正确的密码后,黑屏一闪,重新返回到登陆界面问题解决

    一,问题描述: Ubuntu出现登陆界面后,选择用户名,输入密码,然后登陆画面消失,似乎要进入系统了;但很快,又出现了同样的用户登陆界面,再次选择用户名、输入密码,再次来到这个状态,形成一个死循环。 二,解决办法: 1.若是本地的虚拟机运行的服务: 在登录界面Ctrl+Alt+F1进入命令行界面: 先找到这个文件: /home/user/.xsession-…

    2023年11月29日
    3.7K00
  • Rocky9的安装与基本配置

    安装 Rocky Linux 9.0 设置语言为:中文 — 简体中文(中国) 注意: 默认 Rocky Linux 9.0 安全设置禁用了 SSH 使用 root 密码登录了,所以在设置 root 用户密码的时候,记得勾选[允许 root 用户使用密码进行 SSH 登录] 开始安装 安装完成,点击[重启系统] 内核升级到了5.14 登录成功 防火墙配置 SE…

    2023年4月7日
    3.1K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信