Sorting for Backups

I have to backup data each week, but manually sorting folders into folders that are 4.2Gb is time consuming. The Applescript below fills up a folder, keeps track of the space in that folder for more folders to be added. The problem is, that usually we move folders here-and-there until we get the best fit. Right now, the code grabs the next folder in line without figuring the best place to put it

It basically will fill up the backup folders without logically selecting folders that would fit better somewhere else. For example: let’s say it moves the first folder which is 600Mb, leaving 3.6Gb. Next, it might move a 2Mb file, then a 200Mb file and so on. If a folder is too big to fit, it will skip to the next one until it finds a size that will fit.

It will leave a lot of holes to fill up if all the small folders are moved in the beginning. Later, I will have it create subsequent folders for each DVD. I don’t want to create 20 folders when 10 will do. I need the files sorted logically amongst the 10 hypathetical folders.

I wonder if I grab the biggest files first, would it leave behind the small files for last? Should I populate the 200+ Mb files first, leaving about 100Mb in each folder, then have it populate the small folders amongst the folders?

What logic would you recommend?


(*Sorts job folders for Backing up to DVD getting folder size and creating backup folders*)

tell application "Finder"
	set MyToBackupFolder to folder "ToBackup" of folder "Rampage_RSI_Archive2Bup" of disk "RamVol" -- This is the folder which contains all the job folder to backup to DVD
	set CountOfMyToBackupFolder to count of every folder of MyToBackupFolder -- gets initial count of job folders to backup
	set BupFolder to folder "RA0000056" of folder "Ready2Burn" of folder "Rampage_RSI_Archive2Bup" of disk "RamVol" -- Sets up first backup folder, Later we'll ask for the folder name fron user, then increment by one and create the folders necessary.
	set FolderName to "" -- initialize variable
	set FolderSize to 0 -- initialize variable
	set BupSize to 4.2E+9 -- initialize variable. Max size of DVD media
	
	-- BEGIN --- Parses folder routine
	
	repeat until CountOfMyToBackupFolder = 0
		set i to CountOfMyToBackupFolder - CountOfMyToBackupFolder + 1 -- gets folder 1 no matter how many folders are left
		set FolderName to folder i of MyToBackupFolder as alias -- sets the 1st folder as alias
		set FolderSize to size of folder i of MyToBackupFolder -- gets the folder size of 1st folder
		delay 3 -- waits 3 seconds. Errors if we go too fast, since it takes time to get sizes.
		set FolderSize to FolderSize as real -- sets the byte size of folder to a real number
		
		if FolderSize > 4.2E+9 then
			move FolderName to "RamVol:Rampage_RSI_Archive2Bup:bigones" -- the big ones that have to broken down to smaller pieces are moved out to a speacial folder for hand processing
		else
			if FolderSize ≤ BupSize then -- checks the current folder size to see if it will fit . NOTE that BupSize will be decremented later by the size of the current folder that is moved there so we will always know how much space is available in that backup folder
				move FolderName to BupFolder -- move the folder if size is right
				set BupSize to BupSize - FolderSize -- here we decrement by current folder size to keep track of available space in the back up folder
				set CountOfMyToBackupFolder to CountOfMyToBackupFolder - 1 -- decrements the count of folders by one folder
				log "CountOfMyToBackupFolder: " & CountOfMyToBackupFolder
				log "FolderName: " & FolderName
				
			end if
		end if
	end repeat
	
	
	
	
	
	
	
end tell


It sounds as if you have made for yourself a version of the bin packing problem. It is an NP-hard computation problem. Your guess about copying the large folders first is a good idea, it is referred to as the “first fit decreasing” strategy in the above linked page. Packing the largest item first should result in a more efficient packing, but it does not guarantee optimality. Check the link for other references ideas.