Solved

Backup script to run on mac

Posted on 2014-04-21
10
2,155 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 27

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

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 27

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
A common question or need, when setting-up a new Mac for someone would be to make all of the applications, installed, available from the dock. Many people often do not realize an application is installed unless it is in the dock. Creating a custo…
Users will learn how resize a batch of photos from a single command in Photoshop via Photoshop's Image Processor. Open up an Image you'd like to resize in Adobe Photoshop: Adjust the image size according to your preferences. Image > Adjustments > …
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…

760 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