Link to home
Start Free TrialLog in
Avatar of jaxon_b
jaxon_b

asked on

Backup script to run on mac

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?
Avatar of serialband
serialband
Flag of Ukraine image

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

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
Avatar of jaxon_b
jaxon_b

ASKER

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?
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.
Avatar of jaxon_b

ASKER

Is there anyway that the profile name can be read as wild card so it can apply to multiple machine names without being specific?
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.
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?
Avatar of jaxon_b

ASKER

both
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

ASKER CERTIFIED SOLUTION
Avatar of EdTechy
EdTechy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial