首页 > 解决方案 > 如何在 Git 中将我的 dotenv 文件和配置文件从一台计算机复制/备份到另一台计算机

问题描述

在 git 中存储秘密(.env 文件)通常不是一个好习惯。但是,当我需要移动计算机时,我通常必须将它们一个一个地复制和粘贴。

我不想复制整个 git 存储库,因为它还包含node_modules并且将永远通过 Google。如何仅复制我的 env 文件?

标签: git

解决方案


在您的站点目录中运行它

setopt sh_word_split # For zsh

dirs=$(find . -maxdepth 1 -type d -execdir test -d {}/.git \; -prune -print 2>/dev/null)

# store dotfiles here
[[ ! -d dotfiles ]] && mkdir dotfiles
dotfiles_path=$(cd dotfiles && pwd)

for dir in $dirs; do
    originalPwd=$(pwd)
    cd $dir
    ignored=$(git status --ignored)
    for file in $ignored; do
        if [[ -f $file ]]; then
            new_location="$dotfiles_path/$dir/$(dirname $file)"
            mkdir -p $new_location
            cp $file $new_location
        fi
    done
    cd $originalPwd
done

将文件复制到 Google Drive:

cp -r dotfiles ~/Google\ Drive\

等待文件同步

在新计算机上,转到您的~/Sites目录并执行

cp -r ~/Google\ Drive/dotfiles/* .

一切都使用最小的带宽同步:)

您也可以将其放入脚本和 cron 作业中以自动备份

env EDITOR=nano crontab -e

粘贴:

*/10 * * * * ~/Sites/backupEnv.sh

推荐阅读