Quick script that can backup a file you are working on.
Make sure you change the shebang #!/usr/local/bin/php to point to your php executable. Read about the -Cq on this page below.
As of version 1.1, there is a bug when performing an action like the following.
$ php -f bakme.php path/to/file path/to/bak/folder
This will not work. Until I get this fixed, the backup folder and the file being backed up must be in the same location. The syntax right now must be,
$ php -f bakme.php file bak_folder
#!/usr/local/bin/php -Cq
<?php
###
# Version: 1.1
#
# Description: This file creates the directory bak unless you specify one, and then
# copies the file you provide to that directory appending an ascending number.
#
# Usage: bakme [options] <file_to_backup> [directory_to_put_backup_file_in]
#
# Examples: file_to_backup=report.txt && directory_to_put_backup_file_in=backup_dir
#
# ---Backup report.txt to default directory---
# $ bakme report.txt
#
# ---Backup report.txt to your defined directory (backup_directory)---
#
# $ bakme report.txt backup_dir
#
# ---Backup report.txt to default directory and gzip the file---
# $ bakme -g report.txt
#
# ---Backup report.txt to your defined directory (backup_directory) and gzip the file---
# $ bakme -g report.txt backup_dir
###
$arg1 = $_SERVER['argv'][1]; #> your file to backup (or options)
$arg2 = $_SERVER['argv'][2]; #> directory to backup in [default is bak] or (your file to backup)
$arg3 = $_SERVER['argv'][3]; #> your directory to backup in
$options = '';
$gzip = '/bin/gzip';
$backup_file_extension = 'bak';
$default_backup_location = 'bak';
$eol = "\n";
define('FILE_APPEND', 1);
if(!function_exists("file_put_contents"))
{
function file_put_contents($n, $d, $flag = false) {
$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
$f = @fopen($n, $mode);
if ($f === false) {
return 0;
} else {
if (is_array($d)) $d = implode($d);
$bytes_written = fwrite($f, $d);
fclose($f);
return $bytes_written;
}
}
}
if($arg1 != '' && $arg2 != '' && $arg3 != ''){
$options = $arg1;
$file = $arg2;
$dir = $arg3;
}
elseif($arg1 != '' && $arg2 != '' && $arg3 == ''){
if(eregi("-",$arg1)){
$options = $arg1;
$file = $arg2;
$dir = $default_backup_location;
}
else{
$file = $arg1;
$dir = $arg2;
}
}
elseif($arg1 != '' && $arg2 == '' && $arg3 == ''){
$file = $arg1;
$dir = $default_backup_location;
}
else{
die("I don't know what to do with your input!");
}
if($options != ''){
if(eregi("g",$options)){
$zip = 'gzip';
}
}
if(!file_exists($file)){
die("Your file, {$file}, DNE" . $eol);
}
if(!file_exists($dir)){
if(!mkdir($dir)){
die("Couldn't make directory {$dir}" . $eol);
}
}
$x = 1;
while(file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}") || file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}.gz")){
$x += 1;
}
if(!file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}")){
if(copy("{$file}","{$dir}/{$file}.{$backup_file_extension}{$x}")){
if($zip == 'gzip'){
exec("{$gzip} {$dir}/{$file}.{$backup_file_extension}{$x}");
$append_ext = '.gz';
}
else{
$append_ext = '';
}
if(!file_exists("{$dir}/.htaccess"))
{
$htaccess = <<<eof
Order Deny, Allow
Deny from all
eof;
if(!file_put_contents("{$dir}/.htaccess",$htaccess))
{
print("Could not create {$dir}/.htaccess\n");
}
if(!chmod("{$dir}/.htaccess",0755))
{
print("Could not change mode of {$dir}/.htaccess");
}
}
print <<<eof
{$file} copied to {$dir}/{$file}.{$backup_file_extension}{$x}{$append_ext}
eof;
}
else{
die("I couldn't not create your file {$dir}/{$file}.{$backup_file_extension}{$x}{$append_ext}" . $eol);
}
}
?>
<?php
###
# Version: 1.1
#
# Description: This file creates the directory bak unless you specify one, and then
# copies the file you provide to that directory appending an ascending number.
#
# Usage: bakme [options] <file_to_backup> [directory_to_put_backup_file_in]
#
# Examples: file_to_backup=report.txt && directory_to_put_backup_file_in=backup_dir
#
# ---Backup report.txt to default directory---
# $ bakme report.txt
#
# ---Backup report.txt to your defined directory (backup_directory)---
#
# $ bakme report.txt backup_dir
#
# ---Backup report.txt to default directory and gzip the file---
# $ bakme -g report.txt
#
# ---Backup report.txt to your defined directory (backup_directory) and gzip the file---
# $ bakme -g report.txt backup_dir
###
$arg1 = $_SERVER['argv'][1]; #> your file to backup (or options)
$arg2 = $_SERVER['argv'][2]; #> directory to backup in [default is bak] or (your file to backup)
$arg3 = $_SERVER['argv'][3]; #> your directory to backup in
$options = '';
$gzip = '/bin/gzip';
$backup_file_extension = 'bak';
$default_backup_location = 'bak';
$eol = "\n";
define('FILE_APPEND', 1);
if(!function_exists("file_put_contents"))
{
function file_put_contents($n, $d, $flag = false) {
$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
$f = @fopen($n, $mode);
if ($f === false) {
return 0;
} else {
if (is_array($d)) $d = implode($d);
$bytes_written = fwrite($f, $d);
fclose($f);
return $bytes_written;
}
}
}
if($arg1 != '' && $arg2 != '' && $arg3 != ''){
$options = $arg1;
$file = $arg2;
$dir = $arg3;
}
elseif($arg1 != '' && $arg2 != '' && $arg3 == ''){
if(eregi("-",$arg1)){
$options = $arg1;
$file = $arg2;
$dir = $default_backup_location;
}
else{
$file = $arg1;
$dir = $arg2;
}
}
elseif($arg1 != '' && $arg2 == '' && $arg3 == ''){
$file = $arg1;
$dir = $default_backup_location;
}
else{
die("I don't know what to do with your input!");
}
if($options != ''){
if(eregi("g",$options)){
$zip = 'gzip';
}
}
if(!file_exists($file)){
die("Your file, {$file}, DNE" . $eol);
}
if(!file_exists($dir)){
if(!mkdir($dir)){
die("Couldn't make directory {$dir}" . $eol);
}
}
$x = 1;
while(file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}") || file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}.gz")){
$x += 1;
}
if(!file_exists("{$dir}/{$file}.{$backup_file_extension}{$x}")){
if(copy("{$file}","{$dir}/{$file}.{$backup_file_extension}{$x}")){
if($zip == 'gzip'){
exec("{$gzip} {$dir}/{$file}.{$backup_file_extension}{$x}");
$append_ext = '.gz';
}
else{
$append_ext = '';
}
if(!file_exists("{$dir}/.htaccess"))
{
$htaccess = <<<eof
Order Deny, Allow
Deny from all
eof;
if(!file_put_contents("{$dir}/.htaccess",$htaccess))
{
print("Could not create {$dir}/.htaccess\n");
}
if(!chmod("{$dir}/.htaccess",0755))
{
print("Could not change mode of {$dir}/.htaccess");
}
}
print <<<eof
{$file} copied to {$dir}/{$file}.{$backup_file_extension}{$x}{$append_ext}
eof;
}
else{
die("I couldn't not create your file {$dir}/{$file}.{$backup_file_extension}{$x}{$append_ext}" . $eol);
}
}
?>
[Click to add or edit comments])
Please prepend comments below including a date