需求
很久就想搞了,每天写的代码晚上12点自动备份到github的私有仓库上去,这样就算电脑坏了或者换电脑了也不用管
- 00:00 git 自动push
- git commit -m 里面有时间信息,类似"auto push 2020 1009"
实现
写个shell
#! /bin/sh
#
# git_auto_push.sh
# Copyright (C) 2020 yons <yons@yons-Z390-AORUS-PRO>
#
# Distributed under terms of the MIT license.
#
git_push(){
echo "start pushing"
modify_time=`stat -c %Y ${1}`
this_time=`date +%s`
cd ${1}
echo "-------change directory------"
echo `pwd`
echo "---------------------"
date=`date "+%Y-%m-%d %H:%M:%S"`
git add .
git commit -m "everyday automatic push $(date)"
echo "git fetch origin master"
git fetch origin master
echo "git merge origin/master"
git merge origin/master
echo "git push origin master:master"
git push origin master:master
}
git_push /home/yons/workplace/markdown
命名为auto_git_push.sh
,当然前提是配好git的ssh,然后sh auto_git_push.sh
测试一下能否正常push,应该没问题
加入定时任务
crontab -e
,加入以下
# every 00:00
0 0 * * * /home/yons/workplace/shell/git_auto_push.sh
启动定时任务
sudo /etc/init.d/cron start
效果
macos
类似操作 [refer](https://stackoverflow.com/questions/51722335/how-to-run-shell-script-daily-in-mac-os-x/51722471_