Skip to content

Instantly share code, notes, and snippets.

@pancudaniel7
Created December 27, 2023 11:45
Show Gist options
  • Save pancudaniel7/164edca45cbbd2d2231603271ee7bd88 to your computer and use it in GitHub Desktop.
Save pancudaniel7/164edca45cbbd2d2231603271ee7bd88 to your computer and use it in GitHub Desktop.

Daily Git Backup Script

This bash script automatically backs up your last five Git commits to a specified directory. It's designed to be run on macOS using launchd, ensuring your backups are created daily without manual intervention.

Prerequisites

  • macOS operating system.
  • Basic understanding of terminal commands.
  • Git repositories that you want to back up.

Installation

  1. Place the Script: Ensure the script is located in a directory of your choice, for example, $HOME/scripts/git_backup.sh.

  2. Make it Executable:

    chmod +x $HOME/scripts/git_backup.sh

Setting Up with launchd

launchd allows you to schedule scripts on macOS. To set up this script to run daily:

  1. Create a Launch Agent:

    • Create a .plist file in ~/Library/LaunchAgents, for example, com.user.gitbackup.plist.

    • Open the file with a text editor and add the following content, customizing paths as necessary:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
          <key>Label</key>
          <string>com.user.gitbackup</string>
          <key>ProgramArguments</key>
          <array>
              <string>/path/to/your/script/git_backup.sh</string>
          </array>
          <key>StartCalendarInterval</key>
          <dict>
              <key>Hour</key>
              <integer>2</integer> <!-- Set the hour (0-23) for the script to run -->
              <key>Minute</key>
              <integer>0</integer> <!-- Set the minute (0-59) -->
          </dict>
          <key>StandardErrorPath</key>
          <string>/tmp/com.user.gitbackup.err</string>
          <key>StandardOutPath</key>
          <string>/tmp/com.user.gitbackup.out</string>
      </dict>
      </plist>
  2. Load the Launch Agent:

    • To start the scheduled task immediately and automatically load it on login:
      launchctl load ~/Library/LaunchAgents/com.user.gitbackup.plist
    • To unload it (if you need to edit the configuration or remove the task):
      launchctl unload ~/Library/LaunchAgents/com.user.gitbackup.plist

Usage

  • The script will automatically run at the specified time daily.
  • Ensure your Mac is turned on and not in sleep mode at the scheduled time.
  • Check /var/log/git_backup.log for logs and /tmp/com.user.gitbackup.out or /tmp/com.user.gitbackup.err for standard output and errors, respectively.

Troubleshooting

  • Verify that the script has execute permissions.
  • Check the log file for any script-related errors.
  • Ensure the paths in the .plist file are correct and absolute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment