Solved

Backup script to run on mac

Posted on 2014-04-21
10
2,200 Views
Last Modified: 2014-04-24
I have on batch file on my pc to backup everything from my pc to my external drive. I am using xcopy example (xcopy c:\users\Administrator\desktop\*.* /D /E /C /Y e:\backup\Administrator\desktop\) How would I do this on a mac for the different folder directories?
0
Comment
Question by:jaxon_b
  • 4
  • 3
  • 3
10 Comments
 
LVL 28

Expert Comment

by:serialband
ID: 40013494
You should probably use robocopy.exe instead of xcopy if you're synchronizing the files.  It works much better than xcopy if you're copying large amounts of data.  That way you're not copying stuff that doesn't need to be copied and you can save some time if it already exists on your external disk.

robocopy.exe c:\users\Administrator\desktop\ e:\backup\Administrator\desktop\ /mir /copyall /w:1 /r:1

Open in new window



On Mac you would just use Time Machine.  If you want the command line, use cp to copy.

cp -pr /Users/AdminAccountName/Desktop /Volumes/NAME_OF_CIFS_SHARE/AdminAccountName/Desktop
cp -pr /Users/AdminAccountName/Desktop /Volumes/NAME_OF_EXTERNAL_DISK/AdminAccountName/Desktop

Open in new window


You would use rsync if you want to do robocopy.exe and not copy files that have already been copied.

rsync -az /Users/AdminAccountName/AdminAccountName/Desktop /Volumes/NAME_OF_CIFS_SHARE/AdminAccountName/Desktop
rsync -az /Users/AdminAccountName/AdminAccountName/Desktop /Volumes/NAME_OF_EXTERNAL_DISK/AdminAccountName/Desktop

Open in new window

0
 
LVL 10

Expert Comment

by:EdTechy
ID: 40013539
rsync -a /Users/Administrator/Desktop /VolumeNameOfBackup/BackupFolder
rsync -a /Users/Administrator/Documents /VolumeNameOfBackup/BackupFolder
repeat for any folder in the home directory that you want to backup.
This will create a folder in BackupFolder called Desktop and recursively copy all files and folders in archive mode.
Subsequent use will only copy files that have been changed.

Open terminal and type "man rsync"
You will see all of the options for rsync.

In a nutshell rsync -options sourcedir destinationdir
0
 

Author Comment

by:jaxon_b
ID: 40013937
I am having a hard time trying to understand the rsync process with a external drive. Can I have the script on the desktop to run (like a batch) or can it automatic detect a mounted drive then run?
0
 
LVL 10

Expert Comment

by:EdTechy
ID: 40014905
Yes you could write a script similar to a batch script. OS X is built on UNIX so there are various scripting languages you could use. Most common is bash.
here is a sample script.
Open a text editor create the following:
#!/bin/sh
rsync -a /Users/Administrator/Desktop /VolumeNameOfBackup/BackupFolder
rsync -a /Users/Administrator/Documents /VolumeNameOfBackup/BackupFolder

Open in new window

Save this as backup.sh
The first line is essential and must be line 1.
Once the file is saved you must make it executable. Open terminal and type
chmod +x /pathtofile/filename

Open in new window

Now in terminal you can execute to script by typing it's location eg. /pathtofile/filename.sh and the script will run.
Or, you can have the file open in terminal (right click>Open with.../Utilities/Terminal.app).
Put backup.sh on your desktop and double click to run.
0
 

Author Comment

by:jaxon_b
ID: 40014984
Is there anyway that the profile name can be read as wild card so it can apply to multiple machine names without being specific?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 28

Expert Comment

by:serialband
ID: 40015031
If you want the script to be clickable without having to right-click, then click open, rename it to backup.command or create a link to backup.sh named backup.command and put it on the desktop.

ln -s ~/backup.sh ~/Desktop/backup.commnad


The default mount point of your disk is in /Volumes/.  OSX automounts external disks and remote shares there.


You could also semi-automate your script with a check for the external disk and copy only when it detects the external disk, then schedule it to run periodically.
#!/bin/bash
if [ -d "/Volumes/NAME_OF_EXTERNAL_DISK" ]; then
    rsync -a /Users/AdminAccountName/AdminAccountName/Desktop /Volumes/NAME_OF_EXTERNAL_DISK/AdminAccountName/Desktop
fi

Open in new window


You could make it automatic, but that requires a little more work.  I don't suggest this unless you have time to learn a bit more about the underpinnings.  You'll have to write a launch daemon to watch the folder or usb event.  You can to look in /System/Library/LaunchDaemons/ for examples, such as com.apple.backupd-auto.plist or com.apple.usbd.plist, to create the launchd and use it to call the script you've made.  I suggest copying the plist files out of there to your local folder to work on, so you don't accidentally trash any of the critical system files.  Don't change an existing launch daemon, create a new one with a new name and register it.
0
 
LVL 10

Expert Comment

by:EdTechy
ID: 40015374
Jaxon, Not sure what you are asking. The scripts we have suggested are not machine specific. As long as the external disk is the same (or at least the same name) for each computer, the scripts would run fine on each. If by profile name you mean username. Yes you could have the script get the current logged in user. Or you could backup up the whole /Users folder catching all of the users that have logged into the computer.
Are you wanting to backup up multiple computers or multiple users on a single computer - or both?
0
 

Author Comment

by:jaxon_b
ID: 40015426
both
0
 
LVL 28

Expert Comment

by:serialband
ID: 40015804
You should copy the script below and change the folder on the external disk for each computer in case you have duplicate account names.  That way you don't wipe data between systems.

You don't need a wildcard to copy all user data.
To back up multiple user folders:
#!/bin/bash
if [ -d "/Volumes/NAME_OF_EXTERNAL_DISK" ]; then
    rsync -a /Users/ /Volumes/NAME_OF_EXTERNAL_DISK/Users_Computer1/
fi

Open in new window

If you really only want Desktop items, then you'd do something like this:
if [ -d "/Volumes/NAME_OF_EXTERNAL_DISK" ]; then
    cd /Users; for USER_LIST * ; do rsync -a /Users/$USER_LIST/Desktop /Volumes/NAME_OF_EXTERNAL_DISK/Users_Computer1/$USER_LIST/Desktop
fi

Open in new window

0
 
LVL 10

Accepted Solution

by:
EdTechy earned 500 total points
ID: 40015884
If you use the same disk on multiple computers the script will work to back up the specified user.
If you want to back up the user that is logged in, you could use ~/Desktop, ~/Documents etc.
~ is the indication for the current user home directory path.
If you are backing up multiple computers, you could modify the script to get the machine name.
scutil --get ComputerName

Open in new window

#!/bin/bash
MachineName = `scutil --get ComputerName`
rsync -a ~/Desktop /Volumes/NameOfBackup/$MachineName
rsync -a ~/Documents /Volumes/NameOfBackup/$MachineName

Open in new window

This would create a folder on the backup disk with the current machine name and backup the current user's Desktop and Documents to it.
I noticed in my previous posts, I left out the "/Volumes" part of the path to the backup disk.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create a default user profile for Mac OS X 10.7/10.8 Create a user account on OS X that will be a template for every other user of that computer. I usually call it “profile” and make it an administrator account for the time being. 1. Install a…
iCloud Drive was introduced after iOS 8 was launched last year. This drive is Apple’s online storage device that lets users sync their files and access them from all their Apple devices.   There is a lot of data that is not automatically backed up…
Users will learn how to set proper sequence settings, scale images, paste attributes, add transitions, fades, and music. Open up Final Cut Pro 7 and Create a new Project: Set the Sequence Settings. a) Click File > Easy Setup > Format > Apple ProRe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now