如何使用 Github 备份/同步所有点文件
作为开发人员,我们每天面临的挑战之一是将所有dot(.)
文件备份或同步到新环境或其他环境。我的机器里有很多点文件,例如、.alias
、.vimrc
等等…….zshrc
.gitconfig
由于每个人的环境不同,我不会详细解释如何备份所有文件,所以我会解释如何备份 VS-Code 设置。我知道有一个 VS-Code 扩展可以将您的设置与 Github 同步。我们的目标是了解如何备份 #vscode 设置、代码片段、扩展,以及如何手动将其与 Github 同步。
创建脚本
- 在您的 Github.com 中创建一个新的 Github 存储库
- 在本地机器上,转到桌面并在终端中打开一个空文件夹
-
使用以下命令启动一个空的 Git 存储库,
$ git init
并使用以下命令将本地存储库连接到新创建的存储库$ git remote add git@github.com:yourusername/yourrepo.git
-
创建一个 shell 脚本并随意命名,
backup.sh
然后将以下代码粘贴到该文件上。
#!/bin/bash
# check to see is git command line installed in this machine
IS_GIT_AVAILABLE="$(git --version)"
if [[ $IS_GIT_AVAILABLE == *"version"* ]]; then
echo "Git is Available"
else
echo "Git is not installed"
exit 1
fi
# copy Vs-Code files
cp $HOME/Library/Application\
Support/Code/User/{keybindings.json,settings.json,spellright.dict} .
# copy snippets folder
cp -r $HOME/Library/Application\ Support/Code/User/snippets .
# copy list of extensions that currently installed
code --list-extensions --show-versions >> ../../vscode/extensions.txt
# copy other dot files
cp $HOME/{.zshrc,.vimrc} .
# Check git status
gs="$(git status | grep -i "modified")"
# echo "${gs}"
# If there is a new change
if [[ $gs == *"modified"* ]]; then
echo "push"
fi
# push to Github
git add -u;
git commit -m "New backup `date +'%Y-%m-%d %H:%M:%S'`";
git push origin master
- 使用 更改脚本权限
$ chmod +x backup.sh
。 - 使用以下命令运行脚本
$ ./backup.sh
如果您想在后台运行服务:
crontab -e
并添加您想要运行脚本的路径和时间,例如
$ crontab -e # this is open the cronjobs table in vim mode use i for insert mode
# this is run the script every minutes
$ 1 * * * * cd /Users/macp15/Projects/dotfiles/scripts/backup && ./backup.sh
# display list of cron jobs
$ crontab -l
完成(Khalas)。
鏂囩珷鏉ユ簮锛�https://dev.to/jeffshomali/how-to-backup-sync-all-of-your-dotfiles-with-github-e1c