diff options
| -rw-r--r-- | README.md | 15 | ||||
| -rw-r--r-- | config.example | 6 | ||||
| -rwxr-xr-x | dot-backup.sh | 36 |
3 files changed, 54 insertions, 3 deletions
@@ -64,6 +64,7 @@ git push -u origin master -v, --verbose Print each file as rsync transfers it -r, --restore Restore dotfiles from backup to original locations -q, --quiet Suppress stdout; write output to log instead + -p, --push Push to remote after commit -h, --help Show this help ``` @@ -82,12 +83,20 @@ Or automate with cron (silent, logs to `~/.local/share/dot-backup/backup.log`): 0 * * * * /path/to/dot-backup.sh -q ``` -After backup, push manually: +Push manually after backup: ```bash cd ~/Programming/GIT/my-dotfiles && git push ``` +Or set `GIT_REMOTE` in config and use `-p` to push automatically: + +```bash +./dot-backup.sh -p +# or combined with cron: +./dot-backup.sh -q -p +``` + ## Restoring on a New Machine Preview what would be restored first: @@ -118,8 +127,12 @@ Available options: DEFAULT_OUTPUT_DIR="${HOME}/Programming/GIT/my-dotfiles" LOG_FILE="${HOME}/.local/share/dot-backup/backup.log" DOTFILES_LIST="${HOME}/.config/dot-backup/files.list" +GIT_REMOTE="git@github.com:you/my-dotfiles.git" +GIT_BRANCH="master" ``` +`GIT_REMOTE` — if set, script ensures it is registered as `origin` on every run. Required for `--push`. `GIT_BRANCH` defaults to the current branch if unset. + ## Adding Files **Edit the script** — add paths to the `DOTFILES` array in `dot-backup.sh`. diff --git a/config.example b/config.example index f4b98f5..79e716d 100644 --- a/config.example +++ b/config.example @@ -9,3 +9,9 @@ # External dotfiles list (one path per line, # for comments) #DOTFILES_LIST="${HOME}/.config/dot-backup/files.list" + +# Git remote URL — if set, script ensures this is registered as 'origin' +#GIT_REMOTE="git@github.com:you/my-dotfiles.git" + +# Branch to push to when using -p/--push (defaults to current branch if unset) +#GIT_BRANCH="master" diff --git a/dot-backup.sh b/dot-backup.sh index dd40f25..82b6053 100755 --- a/dot-backup.sh +++ b/dot-backup.sh @@ -29,6 +29,8 @@ DEFAULT_OUTPUT_DIR="${HOME}/Programming/GIT/my-dotfiles" LOG_FILE="${HOME}/.local/share/dot-backup/backup.log" DOTFILES_LIST="${HOME}/.config/dot-backup/files.list" CONFIG_FILE="${HOME}/.config/dot-backup/config" +GIT_REMOTE="" +GIT_BRANCH="" # Bootstrap config and log directories on first run FIRST_RUN=false @@ -49,6 +51,7 @@ DRY_RUN=false VERBOSE=false RESTORE=false QUIET=false +PUSH=false usage() { echo "Usage: $0 [options]" @@ -56,6 +59,7 @@ usage() { echo " -v, --verbose Print each file as it is processed" echo " -r, --restore Restore dotfiles from backup to their original locations" echo " -q, --quiet Suppress stdout; write output to log instead" + echo " -p, --push Push to remote after commit (requires GIT_REMOTE in config)" echo " -h, --help Show this help" exit 0 } @@ -66,6 +70,7 @@ for arg in "$@"; do -v|--verbose) VERBOSE=true ;; -r|--restore) RESTORE=true ;; -q|--quiet) QUIET=true ;; + -p|--push) PUSH=true ;; -h|--help) usage ;; *) echo -e "${RED}Unknown option: $arg${NC}"; usage ;; esac @@ -298,7 +303,7 @@ if [[ -d $DEFAULT_OUTPUT_DIR ]]; then if [[ $(git rev-parse --is-inside-work-tree) == "true" ]]; then echo -e "${GREEN}Git Repository already initialized.${NC}" else - echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}" + echo -e "${YELLOW}Initializing Git Repository.${NC}" git init git add . fi @@ -307,13 +312,29 @@ else if [[ "$DRY_RUN" == false ]]; then echo -e "${YELLOW}creating our backup directories${NC}" mkdir -p $DEFAULT_OUTPUT_DIR/{home,system} - echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}" + echo -e "${YELLOW}Initializing Git Repository.${NC}" cd $DEFAULT_OUTPUT_DIR git init git add . fi fi +# Ensure remote is registered if GIT_REMOTE is configured +if [[ -n "$GIT_REMOTE" && "$DRY_RUN" == false ]]; then + cd $DEFAULT_OUTPUT_DIR + if git remote get-url origin &>/dev/null; then + current=$(git remote get-url origin) + if [[ "$current" != "$GIT_REMOTE" ]]; then + echo -e "${YELLOW}Updating remote origin: $GIT_REMOTE${NC}" + git remote set-url origin "$GIT_REMOTE" + fi + else + echo -e "${YELLOW}Adding remote origin: $GIT_REMOTE${NC}" + git remote add origin "$GIT_REMOTE" + fi + cd $WORKDIR +fi + if [[ "$DRY_RUN" == false ]]; then lastupdate fi @@ -360,6 +381,17 @@ else echo -e "${GREEN}Committed.${NC}" fi +if [[ "$PUSH" == true ]]; then + if [[ -z "$GIT_REMOTE" ]] && ! git remote get-url origin &>/dev/null; then + echo -e "${RED}--push requested but no remote configured. Set GIT_REMOTE in config.${NC}" + exit 1 + fi + branch="${GIT_BRANCH:-$(git symbolic-ref --short HEAD)}" + echo -e "${GREEN}Pushing to origin/$branch...${NC}" + git push origin "$branch" + echo -e "${GREEN}Pushed.${NC}" +fi + echo -e $NC cd $WORKDIR if [[ "$QUIET" == true ]]; then echo "Log written to: $LOG_FILE" >&3; fi |
