云服务器上的目录定时同步到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 centOS虚拟机出现entering emergency mode解决方案

    1、centos打开之后出现这个问题,迟迟打不开 按他的操作输入journalctl之后输入shift+g到日志最后查看报错发现是xfs(dm-0有问题) 输入xfs_repair -v -L /dev/dm-0因此修复 /dev/dm-0就可以了 -L 选项指定强制日志清零,强制xfs_repair将日志归零,即使它包含脏数据(元数据更改)。 #重启虚拟机…

    2024年1月11日
    28600
  • mysql5.7忘记密码时,修改root密码

    推荐方法二 方法一: 1、修改mysql 配置文件,添加 skip-grant-tables vim /etc/my.cnf [myslqd] ……………….skip-grant-tables……………….…

    2023年3月7日
    38200
  • Linux如何调整Swap空间大小

    查看当前Swap文件位置以及大小 要查看当前正在使用的swap文件或分区的位置,您可以使用swapon命令。 执行以下命令: 这将显示所有活动的swap空间,包括其类型(例如文件或分区)、大小、使用情况和位置。 如果您只对文件路径或分区路径感兴趣,您可以通过/proc/swaps文件查看相同的信息: 这将列出系统上所有的swap空间,包括它们的位置。 设置一…

    2023年11月6日
    25900
  • Linux JumpServer 堡垒机远程访问

    前言JumpServer 是广受欢迎的开源堡垒机,是符合 4A 规范的专业运维安全审计系统。JumpServer 帮助企业以更安全的方式管控和登录所有类型的资产,实现事前授权、事中监察、事后审计,满足等保合规要求。 下面介绍如何简单设置即可使本地jump server 结合cpolar 内网穿透实现远程访问jump server 管理界面. 安装环境后.使用…

    2023年12月20日
    20600
  • rsync的搭建与使用

    rsync是数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。 已支持跨平台,可以在Windows与Linux间进行数据同步。 Ubuntu安装 Centos安装 基本用法 rsync语法有三种,分别是本地到本地形式Local to Local,本地到远程Local to Remot…

    2022年11月19日
    41600

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信