Backup Script

Hey Everyone, I am trying to create a backup script. I am choosing two folders one as the backup location and one as the folder to backup, I am getting the contents of each folder for a comparison and if the the original folder contents do not match the backup location contents to put the files missing in to a list and then duplicate the files to the backup location.

What I am having issues with is after the initial backup which seems to work. I either get and error about alias types for my contents of the backup location that is set to a list or if I remove the “as alias” from the list properties it says there is nothing to backup and i delete the files already duplicated so the folder are not the same and it still says nothing to backup.

here the section I am having issue with

set the item_list to entire contents of original_folder
set the backup_item_list to entire contents of backup_location
set the backup_items to {}
set already_backup to {backup_item_list}

repeat with i from 1 to the number of items in item_list
	set listed_item to (item i of item_list)
	try
		if not (exists listed_item in already_backup) then
			set the end of the backup_items to listed_item
		else
			if exists listed_item in already_backup then
			end if
			
		end if
	end try
end repeat

if anyone could help with just a simple comparison of the contents of two folders that will fit in to my script I would be greatful

Mogley

sorry i forgot:

set already_backup to {backup_item_list} as alias

Works one and backs up all files and folders but after the first time gives error about alias types and list all files and folders it is trying to get

Hi Mogley,

I would use this, it does exactly what you’re going to accomplish

do shell script "/usr/bin/rsync -atE " & quoted form of POSIX path of original_folder & space & quoted form of POSIX path of backup_location

if you want, that items at destination should be deleted if they don’t exist any more at source, use this:

do shell script "/usr/bin/rsync -atE --delete " & quoted form of POSIX path of original_folder & space & quoted form of POSIX path of backup_location

Mogley:

I agree with Stefan with a BIG word of caution. Shell scripting is very powerful and very unforgiving! I have been hacking rsync for the past two weeks for a workflow I’m building. Not paying attention I pointed it at one of my externals figuring it would be my pseudoServer and within 30 seconds I had deleted 4 years worth of collected reference files! No do-overs, just gone… to the point that the disk space was IMMEDIATELY reclaimed!! Just imagine if I had pulled this stunt at work instead of my home system! :frowning:

(Stefan’s script looks simple enough but it’s a hell of a lot more powerful than it appears!!!) A few words of advice to save you some grief:

  1. Read the man page for Rsync. Then read it again. Lather. Rinse. Repeat.

  2. Fire up Terminal and do your testing there.

  3. Use the -n or --dry-run options while testing. This will only report what Rsync will do. (If I had known this before I started dinking around I would not have lost all my data!)

  4. Trailing backslashes on the Source directory mean everything! If you want to sync the contents of your source folder then include the trailing backslash. If you want to transfer the entire folder intact (contents AND enclosing folder) then you have to take out the trailing backslash. (This is the other thing that could have saved my data!)

  5. There are other options to look at as well: cp (cpio), tar, etc. Read about your options before you commit to rsync.

  6. If you’re set on rsync and don’t want to script it, check out rsyncX. I’m a roll-your-own kind of guy so I haven’t tried it but many people seem to like it.

Once again, BE CAREFUL. I cannot stress this enough. UNIX has no sympathy for dabblers and people playing with hand grenades. (On the other hand, you’ll find it hard to wipe the smile off your face when you realize the power you now wield! It sucked to lose all that data but what a rush to know I could do it again - in one line!! And yes, I’m a recent convert to shell scripting. :smiley: it’s the perfect complement to straight Applescript but also great fun on its own!)

Enjoy (and be careful):stuck_out_tongue:
Jim Neumann
BLUEFROG