DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2010-06-14 14:56:37

sulan
Member
Registered: 2010-04-02
Posts: 17

Backup GUI for OSX

First of all I just want to thank everyone in this forum for much interesting information in previous posts. Based on this I have developed an incremental backup scheme for my OSX system using hard links. I have also extended this with a very simple GUI in the form of an apple script dialog box that asks for the user's permission to succeed. Based on this I though I might share my experiences in this great forum.

The backup script is executed daily through apple's LaunchAgents which is posted below (replaces the more traditional cron job in OSX). Attached is the GUI that should be placed in the ~/Library/Scripts folder (don't forget to unzip it first) together with the actual backup script . So the script executes locally on my MacbookPro laptop and the backup is sent to my DNS-323 using SSH. The script looks like this:

************************************
********** backup-client.sh ************
************************************
<SCRIPT START - Not included in script>
#!/bin/sh
# Main script for backing up "SRC" to a remote location "DST". This script make use of two
# apple scripts (dialog-request.scpt and dialog-failed.scpt) for asking users permission
# and for notifying the user if backup fail. The script is automatically launched using a
# LaunchAgent (e.g. ~/Library/LaunchAgent/com.your-name.backup).
# Date: 2010-06-13
# E-mail: swebichon@yahoo.se
#

# Make sure another copy of the backup script isn't running
if [ `ps aux | grep backup-client.sh | grep -v grep | wc | awk '{print $3}'` -ne "196"  ]; then
        logger "Backup-client.sh: Another copy of the backup script is running. Aborting."
        echo "Backup-client.sh: Another copy of the backup script is running. Aborting."
        exit 1
fi

# To backup multiple directories surround them the list with single quotes, END WITH '/' !!!!
SRC='/Users/your-name/ /additional-dir1/ /additional-dir2/ ...'

# Set the Destination Path
DEST='/mnt/HD_a2/Backup/MacbookPro'

# Remote backup server
BACKUPSERVER='192.168.0.104'
BACKUPSERVER_USER="root"
EXCLUDES='/Users/your-name/Library/Scripts/EXCLUDES.txt'
date=`date "+%Y%m%d_%H%M%S"`

# Check that we are on our home network ...
ownip1=`/sbin/ifconfig en1 | /usr/bin/grep 192.168.0.103`
ownip2=`/sbin/ifconfig en0 | /usr/bin/grep 192.168.0.102`
if [ -z "${ownip1}" ]; then
    if [ -z "${ownip2}" ]; then
        /usr/bin/logger "Backup-client.sh : not on home network. Aborting backup!"
        exit 2
    fi   
fi

# ... and that the BACKUPSERVER is online ...
pingtest=`ping -c 4 $BACKUPSERVER`
pingsuccess="$?"
if [ "$pingsuccess" -ne 0 ]; then
    /usr/bin/logger "Backup-client.sh : backup server not online. Aborting backup!"
    exit 2
fi

# ... before finally checking so that SSH is available on the BACKUPSERVER.
sizecheck=`ssh ${BACKUPSERVER_USER}@${BACKUPSERVER} "df -h | grep '/mnt/HD_a2'"`
sshsuccess="$?"
if [ "$sshsuccess" -ne "0" ]; then
    /usr/bin/logger "Backup-client.sh : SSH server on backup server not online. Aborting backup!"
    exit 2
fi

# Sleep for a couple of seconds
/bin/sleep 2

# Check so there is at least 9Gb of storage left
sizecheck=`echo $sizecheck | /usr/bin/awk '{print $4}' | /usr/bin/grep "G" | /usr/bin/sed 's/\..*//'`
if [ -z "${sizecheck}" ]; then
    /usr/bin/logger "Backup-cl ent.sh : Not enough storage space on backup server ($sizecheck). Aborting backup!"
    exit 2
fi

# Ask user if backup should be started or not
proceed=`/usr/bin/osascript /Users/your-name/Library/Scripts/dialog-request.scpt | grep "Start backup"`
if [ -z "${proceed}" ]; then
    /usr/bin/logger "Backup-client.sh : User chose to cancel this backup (1)."
    exit 0
fi

logger "Backup-client.sh : Starting backup with rsync to remote host ${BACKUPSERVER}."

# Store size of disk as reference that later allow to calculate the size of this backup
/usr/bin/ssh "${BACKUPSERVER_USER}@${BACKUPSERVER}" "df -m /dev/sda2 > /tmp/BEFORE"

# Carry out the backup with the rsync command
/usr/local/bin/rsync -av --update --delete --exclude-from ${EXCLUDES} ${SRC} ${BACKUPSERVER_USER}@${BACKUPSERVER}:${DEST}/current >/var/log/rsync.log 2>&1

rsyncreturn=$?
if [ "${rsyncreturn}" -ne 0 ]; then
    logger "Backup-client.sh : rsync failed and exited with code  <${rsyncreturn}>. Backup failed."
    /usr/bin/ssh "${BACKUPSERVER_USER}@${BACKUPSERVER}" "cd ${DEST} && rm -rf current"
    # Notify user that this backup failed
    /usr/bin/osascript /Users/your-name/Library/Scripts/dialog-failed.scpt
    exit 2
fi

# Copy all new files as HARD LINKS to the new directory we just created. This only increase the link counter for each file and doesn't affect any file- or meta-data.
/usr/bin/ssh "${BACKUPSERVER_USER}@${BACKUPSERVER}" "cd ${DEST} && cp -al current ${date}"

sshreturn=$?
if [ "${rsyncreturn}" -ne 0 ]; then
    logger "Backup-client.sh : SSH failed and exited with code <${sshreturn}>. Backup failed."
    # Notify user that this backup failed
    /usr/bin/osascript /Users/your-name/Library/Scripts/dialog-failed.scpt
    exit 2
fi

# Calculate size of backup
/usr/bin/ssh "${BACKUPSERVER_USER}@${BACKUPSERVER}" "df -m /dev/sda2 > /tmp/AFTER"
scp  ${BACKUPSERVER_USER}@${BACKUPSERVER}:/tmp/BEFORE ${BACKUPSERVER_USER}@${BACKUPSERVER}:/tmp/AFTER /tmp
SIZE_BEFORE=`cat /tmp/BEFORE | grep '/dev/sda2' | awk '{print $3}'`
SIZE_AFTER=`cat /tmp/AFTER | grep '/dev/sda2' | awk '{print $3}'`

# Log message that backup was successfull
logger "Backup-client.sh : Backup successfull!, rsync exited with ${rsyncreturn}. Size: `expr $SIZE_AFTER - $SIZE_BEFORE` MB."
<SCRIPT END - Not included in script>

************************************
************ LaunchAgent *************
************************************

To launch the script automatically each day I use the following LaunchAgent (just an editable text file) place in ~/Library/LaunchAgents. My script request to start executing by asking the user with the GUI at 17:55. Then the user has 7h to answer before the GUI times out, and backup is postponed till the next day.

<START - Not included in script>
<?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">
<dict>
        <key>Label</key>
        <string>com.your-name.backup</string>
        <key>LowPriorityIO</key>
        <true/>
        <key>Program</key>
        <string>/Users/your-name/Library/Scripts/backup-client.sh</string>
        <key>ProgramArguments</key>
        <array>
                <string>backup-client.sh</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>17</integer>
                <key>Minute</key>
                <integer>55</integer>
        </dict>
</dict>
</plist>
<END - Not included in script>

Last edited by sulan (2010-06-14 15:05:53)


Attachments:
Attachment Icon dialog-request.scpt.zip, Size: 2,131 bytes, Downloads: 204

Offline

 

#2 2010-06-14 15:08:50

sulan
Member
Registered: 2010-04-02
Posts: 17

Re: Backup GUI for OSX

Here is the GUI dialog box notifying the user upon a failed backup, if anyone is interested. Place it in the same directory as main script and don't forget to unzip first.


Attachments:
Attachment Icon dialog-failed.scpt.zip, Size: 691 bytes, Downloads: 219

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB