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

相关推荐

  • Rocky9的安装与基本配置

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

    2023年4月7日
    1.7K00
  • Zabbix的Mysql数据库内存占用高问题分析与处理

    问题:1、Zabbix内存使用率高;2、MariaDB没有开启独享表空间 ibdata1是InnoDB的共有表空间,默认情况下会把表空间存放在一个文件ibdata1中,会造成这个文件越来越大. 原因1:使用InnoDB共享表空间存储数据 参数innodb_file_per_table,控制innodb引擎采用共享表空间存储还是独立表空间存储。 参数innod…

    2022年12月30日
    1.1K00
  • ipmitool 工具使用教程

    IPMI全称为Intelligent Platform Management Interface(智能平台管理接口),原本是一种Intel架构的企业系统的周边设备所采用的一种工业标准。IPMI亦是一个开放的免费标准,用户无需支付额外的费用即可使用此标准。IPMI 能够横跨不同的操作系统、固件和硬件平台,可以智能的监控、控制和自动回报大量服务器的运作状况,以降…

    2024年3月25日
    1.3K00
  • IP addr添加网络的使用

    IP addr 添加网络的使用 添加IP到网口 查看添加的IP 移除添加的IP 启用网口IP 禁用网口 检测路由 添加路由 移除路由 添加网关 移除网关

    2024年7月5日
    1.7K00
  • nginx使用htpasswd实现站点账密登录

    一:介绍 在Web应用程序的开发中,安全性是一项至关重要的任务。当用户需要访问敏感信息或执行特定操作时,需要使用账号和密码进行身份验证,htpasswd是Apache HTTP服务器下的一款用于HTTP用户basic认证工具,可用来建立和更新存储用户名、密码,同时生产用于 http 基本认证的工具 二:htpasswd安装 三:htpasswd配置密码 该命…

    2024年6月3日
    91800

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信