Put com.user.automaticbackups.plist
file into ~/Library/LaunchAgents
directiry.
- Enable:
launchctl load -w ~/Library/LaunchAgents/com.user.automaticbackups.plist
- Disable:
launchctl unload -w ~/Library/LaunchAgents/com.example.app.plist
Put com.user.automaticbackups.plist
file into ~/Library/LaunchAgents
directiry.
launchctl load -w ~/Library/LaunchAgents/com.user.automaticbackups.plist
launchctl unload -w ~/Library/LaunchAgents/com.example.app.plist
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>com.idemchenko.automaticbackups</string> | |
<key>StandardOutPath</key> | |
<string>/tmp/autoBackup.stdout</string> | |
<key>StandardErrorPath</key> | |
<string>/tmp/autoBackup.stderr</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/Users/<user>/backup.sh</string> | |
<string>/Volumes/Macintosh HD/Users/<user>/<path>/</string> | |
<string>/Volumes/EXT HDD</string> | |
<string>some/dir/on/ext/hdd</string> | |
</array> | |
<key>StartOnMount</key> | |
<true/> | |
</dict> | |
</plist> |
#!/bin/bash | |
NOW=$(date) | |
SRC_PATH=$1 | |
DEST_VOLUME=$2 | |
DEST_PATH=$3 | |
FULL_DEST_PATH="$DEST_VOLUME/$DEST_PATH" | |
echo "------------------------------" | |
echo "Backup:.... $NOW" | |
echo "From:...... $SRC_PATH" | |
echo "To:........ $FULL_DEST_PATH" | |
N_FILES_TO_BACKUP=$(diff -qr "$SRC_PATH" "$FULL_DEST_PATH" | grep "$SRC_PATH" | wc -l) | |
echo "Found:..... $N_FILES_TO_BACKUP files to back up" | |
show_start_notification() { | |
osascript - "$N_FILES_TO_BACKUP" <<'EOF' | |
on run argv | |
set n to item 1 of argv as integer | |
display notification "Starting backup of " & n & " files" with title "Autmatic back-up" | |
end run | |
EOF | |
} | |
show_nothing_to_backup_notification() { | |
osascript -e 'display notification "Nothing to backup" with title "Autmatic back-up"' | |
} | |
show_finish_notification() { | |
osascript -e 'display notification "Backup has finished" with title "Autmatic back-up"' | |
} | |
if [ $N_FILES_TO_BACKUP -gt 0 ] | |
then | |
mount | grep "on ${DEST_VOLUME}" > /dev/null | |
if [ $? -eq 0 ] | |
then | |
echo "External HDD has been found" | |
show_start_notification | |
cp -R "$SRC_PATH" "$FULL_DEST_PATH" | |
show_finish_notification | |
else | |
echo "No external HDD has been found" | |
fi | |
else | |
echo "Nothing to backup" | |
show_nothing_to_backup_notification | |
fi | |
echo "Backup complete" | |
echo "------------------------------" |