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

相关推荐

  • Centos7下FTP安装与配置

    一、前述 企业中linux搭建ftp服务器还是很实用的,所以本文基于Centos7搭建服务器教程做个总结。 FTP协议占用两个端口号: 21端口:命令控制,用于接收客户端执行的FTP命令。 20端口:数据传输,用于上传、下载文件数据。 二、服务端安装 1.安装vsftpd yum -y install vsftpd 2.配置vsftp vim /etc/vs…

    2022年6月15日
    1.7K00
  • 二进制部署Mysql8.0.31

    二进制部署Mysql8.0.31 一、软件包下载 企业版:Enterprise , 互联网行业一般不选择. 社区版本:选择 源码包 编译安装: source code .tar.gz 通用二进制 公司用什么版本数据库? 具体什么小版本号? 5.6.20 5.6.34 5.6.36 5.6.38 5.6.40 5.7.18 5.7.20 5.7.22 5.7.…

    2023年1月20日
    1.4K00
  • rsync+sersync实现实时监控备份同步

    rsync+sersync简介 sersync是基于inotify开发的,类似于inotify-tools的工具。所以同样内核需先支持inotify,才能搭建。 sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录。 搭建环境:2台Linux(ce…

    2023年3月29日
    1.4K00
  • linux手误rm后恢复文件

    误删文件还原可以分为两种情况 今天只分析文件被删除后,相关进程还存在的情况 这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID依旧存在。这也是有些服务器删除一些文件但是磁盘不释放的原因 案例演示 创建一个文件 用tail -f打开rumenz.txt文件 目的是让rumenz.txt被删除后,进程依然存在 新开一个终端删除rumenz…

    2023年10月17日
    2.3K00
  • OpenSuSe系统登录密码忘了如何进行重置

    如果你的OpenSusE系统密码忘记了,又不想重装系统,那么,你可以通过如下几个步骤来强制重置root密码 1、重新启动机器,在出现grub引导界面后,按“e”,出现启动Linux的选项,移动光标至第一个选项上再次按“e”出现一个编辑字符的界面在字符后面加入“init=/bin/bash”字段,然后回车。按Ctrl+X,通过给内核传递init=/bin/ba…

    2022年11月24日
    1.8K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信