AppleScript to automate Time Machine backup on NAS

Hey Team,


I'm trying to create a AppleScript that will


1.) Mount a network share via smb. Lets say the location is smb:\\TITAN\BACKUPS\MANUAL


2.) Prompt the user for input (Lets say the prompt says "Enter the Job Name"), and saves the input as a variable (lets say the variable name is JobName, and has been set to 1234).


3.)Create a folder in smb:\\TITAN\BACKUPS\MANUAL with the variable's value inputted from the user. (smb:\\TITAN\BACKUPS\MANUAL\1234 now would exist)


4.)Create a sparsebundle image at the location we created in step 3. The size will be 1024 gigabytes, the volume name will be the same as what the user set as the job name (1234 in our case), and the sparsebundle will be named the job name as well (1234.sparsebundle).


5.)I will need to then mount the sparsebundle.


6.)I then need to tell time machine to use the said sparsebundle volume to be our backup disk.


7.)I then need to have time machine's properties to open up, so that I can select which files to exclude.


Could someone provide some input? Using the user defined variable to navigate with and name the sparse bundle has me stumped after trying to google solutions.


The last 4 lines of the script are where my main hang up is.

________________________________________________________________________________ ______________________


try

mount volume "smb://TITAN/BACKUPS/MANUAL"

end try


set JobName to text returned of (display dialog "Enter Job Name:" default answer "Job_Name")


tell application "Finder"

make new folder at "smb://TITAN/BACKUPS/MANUAL" with properties{name:JobName}

end tell


cd /Volumes/TITAN/BACKUPS/MANUAL

cd(JobName)


hdiutil create -size 1024g -type SPARSEBUNDLE -fs "HFS+J" -volname "JobName" JobName.sparsebundle


mount volume "/Volume/folkvangr/backups/manual/JobName


sudo tmutil setdestination /volumes/JobName


_______________________________________

MacBook Pro, OS X Yosemite (10.10.5)

Posted on Dec 6, 2015 9:00 PM

Reply
7 replies

Aug 20, 2017 11:41 PM in response to NW Coast

@NW Coast


I have your answer, the code is at the bottom, but I have something to say first.


If there was ever an expert to answer a question on Apple Discussions it would be Linc Davis. If he says something is impossible, then you're probably going to need more than a few lines of AppleScript to make a worthwhile solution. It is not Linc Davis's job or obligation to teach you AppleScript. There is excellent documentation online if you need to learn AppleScript.


A teenager neighbor with a little extra time on their hands could realistically steal every last byte of information on every computer that follows that method. The likelihood of being able to restore from the backups is also very low.


The discussions are for resolving issues, not extending functionality or AppleScript tutorials.


That said here is the AppleScript that would likely do what you want. It runs, but its got problems.


-- First get the name of the backup

set BackupName to text returned of (display dialog "Enter Backup Name:" default answer "New_Backup")

-- FIXME, some special characters can cause issues


-- Then get the name of the server

set ServerName to text returned of (display dialog "Whats the name of your server?:" default answer "YourServerName")

-- FIXME, white space in the server name will break the script

set ServerDirectory to ServerName & "/BACKUPS/MANUAL"



-- Try to connect to the server

-- This tell-to-end tell, is originally from: https://discussions.apple.com/thread/3479873?start=0&tstart=0

tell application "Finder"

set mounted_Disks to list disks

--display dialog result as string

--set mounted_Disks to every disk whose (ejectable is true)

if mounted_Disks does not contain ServerName then

-- NOTE, smb may not work. I have no way of testing it. But this does work for apf

try

mount volume "smb://" & ServerDirectory

end try

end if

end tell



-- find the computer's name:

-- find the hostname

-- then use sed to get rid of the .lan at the end

set ComputerName to (do shell script "hostname | sed s/\\.lan$//")



-- find the computer's unique address:

-- get the ifconfig

-- find the 'ether' line with grep

-- get rid of the begining ' ether' with sed

-- get rid of the ':'s with sed

-- get rid of the trailing space with sed

set TheAddress to (do shell script "ifconfig en0 | grep \"^\\s\\+ether\" | sed \"s/[[:space:]]*ether[[:space:]]//\" | sed \"s/\\://g\" | sed \"s/[[:space:]]//\"")



-- find the home dir

set HomeDirectory to (do shell script "cd;pwd")



-- Then create the sparsebundle on the Desktop

-- cd to Desktop

-- create the sparsebundle with a backup name for the file,

-- and underneath, set it with the ComputerName and Computer address

do shell script "cd " & HomeDirectory & "/Desktop;hdiutil create -size 1024g -type SPARSEBUNDLE -fs \"HFS+J\" -volname \"" & BackupName & "\" " & ComputerName & "_" & TheAddress & ".sparsebundle" with administrator privileges



-- Move the sparsebundle to the server

do shell script "mv \"" & BackupName & "\" /Volumes/" & ServerDirectory

-- FIXME, original sparsebundle needs to be deleted from Desktop



-- Then mount the sparsebundle by opening it

open "/Volumes/" & ServerDirectory



-- Then set the time machine to use that new mounted volume

do shell script "tmutil setdestination \"/Volumes/" & BackupName & "\"" with administrator privileges

Dec 7, 2015 1:51 PM in response to Linc Davis

http://www.makeuseof.com/tag/turn-nas-windows-share-time-machine-backup/


I'm following this guide, with this guide


https://www.win.tue.nl/~keesh/dokuwiki/doku.php?id=mac_wiki:time_machine_on_samb a_disk



Are you sure this is impossible (are you sure you are not mistaken)? I did not think the network protocol would come into play, because the sparsebundle is mounted to mimic a drive.

Dec 7, 2015 6:49 PM in response to Linc Davis

My equipment falls outside of the specifications of the document you have linked.


This IS possible (obviously..), so since that has been settled and I (obviously....) have read/tested the process and confirmed it WORKS (no unintended consequences by using the OS X GUI...), perhaps the AppleScript I originally mentioned/posted in the correct forum location can be evaluated.


Linc, any help with the AppleScript would be appreciated. Is my algorithm incorrect? I apologize for any incorrect syntax, as I a beginner with AppleScript.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to automate Time Machine backup on NAS

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.