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.
- macOS operating system.
- Basic understanding of terminal commands.
- Git repositories that you want to back up.
-
Place the Script: Ensure the script is located in a directory of your choice, for example,
$HOME/scripts/git_backup.sh
. -
Make it Executable:
chmod +x $HOME/scripts/git_backup.sh
launchd
allows you to schedule scripts on macOS. To set up this script to run daily:
-
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>
-
-
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
- To start the scheduled task immediately and automatically load it on login:
- 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.
- 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.