## Selective backup/copy script for moving files into production

Tags: my software php tool

Whenever our web site needs to undergo re-branding, there is always the hassle of creating both the selective package of files to move into production from development and the selective back-up of the necessary files already on the production server. While this is not a soul-crushingly elaborate and tedious task, it still takes time that could be better spent elsewhere in the project. With this in mind, I created a simple PHP script that will mirror the directory structure and only those files that will be changed in the process as it copies the files from development into production.

PHP Code:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102  $backup\n"); if(! mkdir($backup, 0777, true)) { logthis("[ERROR] Unable to make mirror directory $backup\n"); exit(); } process_directory($filename); } // otherwise, back up the file else { logthis("Backing up: $original ==>$backup\n"); // check to see if it exists in the original before backing it up if(! file_exists($original)) logthis("[NOTICE] File not present in old directory\n"); // back it up, die on error else if(! copy($original, $bakdir . DIRECTORY_SEPARATOR .$file)) { logthis("[ERROR] Unable to backup file\n"); exit(); } } } closedir($dh); } else { logthis("[ERROR] Unable to open directory:$dir\n"); exit(); } } else { logthis("[ERROR] Not a directory: $dir\n"); exit(); } } // check for invocation errors if($argc < 3) die("\nSyntax: {$argv[0]} \n"); // pull parameters$basedir = $argv[1];$repdir = $argv[2];$bakdir = $argv[3];$log = fopen("{$argv[0]}.log", "a"); echo "\n"; // ready, set, go! process_directory($basedir); fclose(\$log); 

While it's certainly not perfect—the str_replace calls are not sensitive to the folder name's location in the string, for example—it gets the job done in most imaginable circumstances. To use it, call it like so:

php backupscan.php c:\path\to\production c:\path\to\development c:\path\to\backup