mvp for backup zshrc plugin

This commit is contained in:
Alia Wilkinson 2024-04-14 17:52:22 -07:00
parent 31f2025e0f
commit 8b95897adb
No known key found for this signature in database
GPG Key ID: 3DC5D55963F0B0DC
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,52 @@
# Oh My Zsh Backup Plugin
This plugin provides a convenient way to back up your `~/.zshrc` file locally and to a GitHub repository. It automatically detects changes in the `.zshrc` file, commits them with a timestamped message, and pushes changes to the specified GitHub repository. If you ever rm your zshrc, simply go to your Github repo and check it out.
## Dependencies
1. Locally installed git with a git config for pulling the repo url and username
2. A repo in Github https://github.com/yourusername/oh-my-zsh-backup
Example: https://github.com/yourusername/oh-my-zsh-backup
3. Push privileges configured to the main/master origin branch
4. Create directory privileges for this script - create "$HOME/projects/backups" manually otherwise
## Installation
1. Clone this repository into your Oh My Zsh custom plugins directory:
```bash
git clone https://github.com/yourusername/oh-my-zsh-backup ~/.oh-my-zsh/custom/plugins/backup
```
2. Add `backup` to the plugins array in your `~/.zshrc` file:
```bash
plugins=(... backup)
```
3. Restart your terminal.
## Usage
Simply open your terminal or source your `~/.zshrc` file, and the backup will be triggered automatically. Any changes detected in `~/.zshrc` will be backed up to the specified GitHub repository.
## Configuration
### GitHub Username
If your GitHub username is not already set globally, the plugin will prompt you to enter it the first time you run it. Alternatively, you can set it manually using:
```bash
git config --global user.name "Your Username"
```
### Branch
By default, the plugin pushes changes to the branch specified GitHub repository is checked out to locally in the "$HOME/projects/backups" backup directory. You can change the target branch by checking out to a new branch in the backup directory.
## Contributions
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.
## License
This plugin is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

View File

@ -0,0 +1,54 @@
function backup_zshrc() {
# Function to back up ~/.zshrc locally and to a GitHub repository
# Runs every time zsh is opened or source ~/.zshrc is run
# Checks for git changes, will commit and push changes if detected
#
local current_dir=$(pwd)
local backup_dir="$HOME/projects/backups"
github_username=$(git config --global user.name | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
local commit_message="Backup ~/.zshrc at $(date +'%Y_%m_%d-%H%M%S')"
# Prompt for GitHub username if not set
if [[ -z "$github_username" ]]; then
read -p "Enter your GitHub username: " github_username
fi
# Create a backup directory if it doesn't exist
mkdir -p "$backup_dir"
cd "$backup_dir" || return
# Backup ~/.zshrc with a timestamp
local timestamp=$(date +'%Y_%m_%d-%H%M%S')
local backup_file="$backup_dir/zshrc_backup_$timestamp"
cp "$HOME/.zshrc" "$backup_dir/.zshrc"
changes=$(git diff --quiet --exit-code -- "$backup_dir" || echo "yes")
if [[ "$changes" == "yes" ]]; then
echo "Changes detected in $backup_dir/.zshrc"
cp "$HOME/.zshrc" "$backup_file"
git add . > /dev/null 2>&1
if git commit -m "$commit_message" > /dev/null 2>&1; then
if git push > /dev/null 2>&1; then
repo_url="https://github.com/$github_username/backups"
echo "Backup successfully pushed to GitHub. View it at: $repo_url"
else
push_error=$(git push 2>&1)
echo "Error: Failed to push backup to GitHub. Error details: $push_error"
fi
else
commit_error=$(git commit -m "$commit_message" 2>&1)
echo "Error: Failed to commit changes for backup. Error details: $commit_error"
fi
else
echo "No changes detected in $backup_dir for .zshrc"
fi
cd "$backup_dir" || return
git add . > /dev/null 2>&1
cd "$current_dir" || return
}
backup_zshrc