Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora Resources > Guides & Solutions (Not For Questions)
FedoraForum Search

Forgot Password? Join Us!

Guides & Solutions (Not For Questions) Post your guides here (No links to Blogs accepted). You can also append your comments/questions to a guide, but don't start a new thread to ask a question. Use another forum for that.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11th July 2010, 10:33 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfedorafirefox
Generic PHP CL backup script

Here's a PHP5 script I wrote to do automated backups from my main hard drive, to a separate backup hard drive. I run it overnight using a cron job.

I tried to write it so it is clonable and easily modified for making backups of your system. Please take it and modify it to suit your needs.

This script will copy the contents of a directory to the backup drive. It creates a unique directory backup name, based on the current time. When there are more than NUMBER_REQUIRED_BACKUPS in the backed-up directory $BACKUP_GROUPNAME, the oldest backup will be deleted. This allows you to keep n number of the latest backups. You will need to create the $BACKUP_GROUPNAME directory on the backup drive, so the script has somewhere to write the backed-up directories to.

PHP Code:

#! /usr/bin/php

<?php

# last updated: Sun Jul 11 20:54:29 BST 2010

# PHP script to backup the contents of /etc directory
# to /backup/system/fedora/12/etc

#------------------------------------------------------#
# how many backups to keep before deleting the oldest backup
define("NUMBER_REQUIRED_BACKUPS"4);

$OS_NAME="fedora";
$OS_VERSION="12";

#------------------------------------------------------#
# source directory/files that need backing up.

# the source directory to be backed up.
$SOURCE_DIR="/etc";

# the partition label id of the directory/files
# that need backing up. May not be required.
$SOURCE_LABEL_ID="NONE";

# the directory mountpoint of the directory/files
# that need backing up. May not be required.
$SOURCE_MOUNTPOINT="NONE";

echo 
"\$SOURCE_DIR is: $SOURCE_DIR";
echo
"\n\n";

echo 
"\$SOURCE_LABEL_ID is: $SOURCE_LABEL_ID";
echo
"\n\n";

echo 
"\$SOURCE_MOUNTPOINT is: $SOURCE_MOUNTPOINT";
echo
"\n\n";

#------------------------------------------------------#
# destination for the backups

# the backup partition label id.
$BACKUP_LABEL_ID="backups";

# the backup partition directory mountpoint.
$BACKUP_MOUNTPOINT="/backup";

# the backup group name
$BACKUP_GROUPNAME="$SOURCE_DIR";

# the backup group full pathname
$BACKUP_PATH "/backup/system/{$OS_NAME}/{$OS_VERSION}{$BACKUP_GROUPNAME}";

echo 
"\$BACKUP_LABEL_ID is: $BACKUP_LABEL_ID";
echo
"\n\n";

echo 
"\$BACKUP_MOUNTPOINT is: $BACKUP_MOUNTPOINT";
echo
"\n\n";

echo 
"\$BACKUP_GROUPNAME is: $BACKUP_GROUPNAME";
echo
"\n\n";

echo 
"\$BACKUP_PATH is: $BACKUP_PATH";
echo
"\n\n";

#------------------------------------------------------#

# get the mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";

# mount the backup partition by label id.
$bash_command = `mount -v -L $BACKUP_LABEL_ID`;
echo 
"$bash_command \n";
echo
"\n";

# display currently mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";
echo
"\n";

#############################################################
# NOTE: tar does not like filenames with a ':' (colon)
# so DO NOT use as part of the filename extension

$CURRENT_DATE=`date +'-%Y-%m-%d_%H.%M.%S'`;

echo 
"CURRENT_DATE is: " $CURRENT_DATE;
echo
"\n";

$DESTINATION_DIR="{$BACKUP_PATH}{$BACKUP_GROUPNAME}{$CURRENT_DATE}";

echo 
"DESTINATION_DIR IS: $DESTINATION_DIR \n";

# shell command to copy files across to backup partition
# don't dereference symlinks
# preserve mode & ownership attrs
$bash_command = `cp -R --preserve=mode,ownership $SOURCE_DIR $DESTINATION_DIR`;
echo 
"$bash_command \n";

#############################################################

# check the copied directories with diff

#$bash_command = `diff -r $SOURCE_DIR $DESTINATION_DIR`;
#echo "$bash_command \n";

# display currently mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";

$backup_dir "{$BACKUP_PATH}/";
$dir_name_count 0;

# Open the backup directory, and proceed to read its contents
if (is_dir($backup_dir)) {
    if (
$dh opendir($backup_dir)) {
        while ((
$file readdir($dh)) !== false) {
            if (!
is_dir($file) && ".." <> $file) {
                
// build an array of directory names
                
$directory_name_array[] = $file;
                
$dir_name_count ++;
            }
        }
        
closedir($dh);
    }
}

# sort the array so the oldest backup is first in the array,
# and the newest backup is last in the array.
sort($directory_name_array);

echo 
"SORTED contents of \$directory_name_array[] \n";
while(list(
$key$value) = each($directory_name_array)) {
   echo 
"$key => $value \n";
   }

echo
"\n";

# get the oldest backup
$oldest_backup $directory_name_array[0];

# print the name of the oldest backup directory in the array
echo "Oldest backup dir is: $oldest_backup \n\n";

# print number of backups available
echo "There are $dir_name_count backups available. \n\n";

# print how many backups are required
echo 'NUMBER_REQUIRED_BACKUPS: ' NUMBER_REQUIRED_BACKUPS "\n\n";

$BACKUP_DELETION_DIR "{$BACKUP_PATH}/$oldest_backup \n";

echo 
"The oldest backup dir to be deleted is: \n";
echo 
"$BACKUP_DELETION_DIR \n";

# delete the oldest backup dir if necessary
if ($dir_name_count NUMBER_REQUIRED_BACKUPS) {
    
# force recursive deletion of directory
    
$bash_command = `rm -fR $BACKUP_DELETION_DIR`;

    echo 
"There are more than " .NUMBER_REQUIRED_BACKUPS" backups in this directory, \n";
    echo 
"and oldest backup $oldest_backup has been deleted. \n\n";
    }
else
{
    echo 
"There are " .NUMBER_REQUIRED_BACKUPS" or less backups in this directory. \n";
    echo 
"No old backups have been deleted. \n\n";
}

echo 
"Contents of $BACKUP_PATH backup dir is: \n\n";
$bash_command = `ls $BACKUP_PATH`;
echo 
"$bash_command \n";
echo
"\n";

# unmount the backup partition by it's mount point.
$bash_command = `umount -v $BACKUP_MOUNTPOINT`;
echo 
"$bash_command \n";
  
# end of script ?>
Example output from running the script:

Code:
[root@nowhere.com /root-admin-scripts/etc]# ./backup-etc-dir.php

$SOURCE_DIR is: /etc

$SOURCE_LABEL_ID is: NONE

$SOURCE_MOUNTPOINT is: NONE

$BACKUP_LABEL_ID is: backups

$BACKUP_MOUNTPOINT is: /backup

$BACKUP_GROUPNAME is: /etc

$BACKUP_PATH is: /backup/system/fedora/12/etc

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  378M   15G   3% /var/log
 
/dev/sdb6 on /backup type ext3 (rw,noexec)
 

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  378M   15G   3% /var/log
/dev/sdb6             197G   28G  160G  15% /backup
 

CURRENT_DATE is: -2010-07-11_21.02.13

DESTINATION_DIR IS: /backup/system/fedora/12/etc/etc-2010-07-11_21.02.13
 
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  378M   15G   3% /var/log
/dev/sdb6             197G   28G  160G  15% /backup
 
SORTED contents of $directory_name_array[] 
0 => etc-2010-07-04_02.20.02 
1 => etc-2010-07-11_02.20.02 
2 => etc-2010-07-11_20.53.06 
3 => etc-2010-07-11_21.00.10 
4 => etc-2010-07-11_21.02.13 

Oldest backup dir is: etc-2010-07-04_02.20.02 

There are 5 backups available. 

NUMBER_REQUIRED_BACKUPS: 4

The oldest backup dir to be deleted is: 
/backup/system/fedora/12/etc/etc-2010-07-04_02.20.02 
 
There are more than 4 backups in this directory, 
and oldest backup etc-2010-07-04_02.20.02 has been deleted. 

Contents of /backup/system/fedora/12/etc backup dir is: 

etc-2010-07-11_02.20.02
etc-2010-07-11_20.53.06
etc-2010-07-11_21.00.10
etc-2010-07-11_21.02.13
 
/dev/sdb6 umounted
Attached Files
File Type: txt etc-backups.php.txt (4.8 KB, 149 views)

Last edited by CD-RW; 27th September 2013 at 05:12 PM. Reason: corrected typo
Reply With Quote
  #2  
Old 11th July 2010, 10:47 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfedorafirefox
Re: Generic PHP CL backup script

Here is a modified version of the backup script, for making backups of individual files:

PHP Code:

#! /usr/bin/php

<?php

# last updated: Fri Apr  4 14:24:21 BST 2008

# PHP script to backup konqueror bookmarks files

#------------------------------------------------------#
# how many backups to keep before deleting the oldest backup
define("NUMBER_REQUIRED_BACKUPS"5);

#------------------------------------------------------#
# the source-file that needs backing up.

# full path to the file to be backed up.
# $SOURCE_FILE="/home/keith/.kde/share/apps/konqueror/bookmarks.xml";

# full path to the file to be backed up.
# Currently running as root user due to sound problems.
$SOURCE_FILE="/root/.kde/share/apps/konqueror/bookmarks.xml";

echo 
"\$SOURCE_FILE is: $SOURCE_FILE";
echo
"\n\n";

/*
# the partition label id of the file(s)
# that need backing up. May not be required.
$SOURCE_LABEL_ID="NONE";

# the directory mountpoint of the file(s)
# that need backing up. May not be required.
$SOURCE_MOUNTPOINT="NONE";

echo "\$SOURCE_LABEL_ID is: $SOURCE_LABEL_ID";
echo"\n\n";

echo "\$SOURCE_MOUNTPOINT is: $SOURCE_MOUNTPOINT";
echo"\n\n";
*/

#------------------------------------------------------#
# destination for the backups

# the backup partition label id.
$BACKUP_LABEL_ID="backups";

# the backup partition directory mountpoint.
$BACKUP_MOUNTPOINT="/backup";

# the backup group name
$BACKUP_GROUPNAME="/bookmarks";

# the backup group full pathname
$BACKUP_PATH "{$BACKUP_MOUNTPOINT}{$BACKUP_GROUPNAME}";

echo 
"\$BACKUP_LABEL_ID is: $BACKUP_LABEL_ID";
echo
"\n\n";

echo 
"\$BACKUP_MOUNTPOINT is: $BACKUP_MOUNTPOINT";
echo
"\n\n";

echo 
"\$BACKUP_GROUPNAME is: $BACKUP_GROUPNAME";
echo
"\n\n";

echo 
"\$BACKUP_PATH is: $BACKUP_PATH";
echo
"\n\n";

#------------------------------------------------------#

# get the mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";

# mount the backup partition by label id.
$bash_command = `mount -v -L $BACKUP_LABEL_ID`;
echo 
"$bash_command \n";
echo
"\n";

# display currently mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";
echo
"\n";

#############################################################
# NOTE: tar does not like filenames with a ':' (colon)
# so DO NOT use as part of the filename extension

$CURRENT_DATE=`date +'-%Y-%m-%d_%H.%M.%S'`;

echo 
"CURRENT_DATE is: " $CURRENT_DATE;
echo
"\n";

$DESTINATION_DIR="{$BACKUP_PATH}{$BACKUP_GROUPNAME}{$CURRENT_DATE}";

echo 
"DESTINATION_DIR IS: $DESTINATION_DIR \n";

# shell command to create the backup group directory
$bash_command = `mkdir $DESTINATION_DIR`;

# shell command to copy konqueror bookmarks file to backup parition
$bash_command = `cp -v --preserve=mode,ownership $SOURCE_FILE  $DESTINATION_DIR`;
echo 
"$bash_command \n";

# shell command to copy konqueror bookmarks.bak file to backup parition
$bash_command = `cp -v --preserve=mode,ownership $SOURCE_FILE.bak  $DESTINATION_DIR`;
echo 
"$bash_command \n";

#############################################################

# check the copied directories with diff
#$bash_command = `diff "$SOURCE_FILE" "$DESTINATION_DIR/bookmarks.xml"`;
#echo "$bash_command \n";

# display currently mounted partitions
$bash_command = `df -h`;
echo 
"$bash_command \n";

$backup_dir "{$BACKUP_PATH}/";
$dir_name_count 0;

# Open the backup directory, and proceed to read its contents
if (is_dir($backup_dir)) {
    if (
$dh opendir($backup_dir)) {
        while ((
$file readdir($dh)) !== false) {
            if (!
is_dir($file) && ".." <> $file) {
                
// build an array of directory names
                
$directory_name_array[] = $file;
                
$dir_name_count ++;
            }
        }
        
closedir($dh);
    }
}

# sort the array so the oldest backup is first in the array,
# and the newest backup is last in the array.
sort($directory_name_array);

echo 
"SORTED contents of \$directory_name_array[] \n";
while(list(
$key$value) = each($directory_name_array)) {
   echo 
"$key => $value \n";
   }

echo
"\n";

# get the oldest backup
$oldest_backup $directory_name_array[0];

# print the name of the oldest backup directory in the array
echo "Oldest backup dir is: $oldest_backup \n\n";

# print number of backups available
echo "There are $dir_name_count backups available. \n\n";

# print how many backups are required
echo 'NUMBER_REQUIRED_BACKUPS: ' NUMBER_REQUIRED_BACKUPS "\n\n";

$BACKUP_DELETION_DIR "{$BACKUP_PATH}/$oldest_backup \n";

echo 
"The oldest backup dir to be deleted is: \n";
echo 
"$BACKUP_DELETION_DIR \n";

# delete the oldest backup dir if necessary
if ($dir_name_count NUMBER_REQUIRED_BACKUPS) {
    
# force recursive deletion of directory
    
$bash_command = `rm -fR $BACKUP_DELETION_DIR`;

    echo 
"There are more than " .NUMBER_REQUIRED_BACKUPS" backups in this directory, \n";
    echo 
"and oldest backup $oldest_backup has been deleted. \n\n";
    }
else
{
    echo 
"There are " .NUMBER_REQUIRED_BACKUPS" or less backups in this directory. \n";
    echo 
"No old backups have been deleted. \n\n";
}

echo 
"Contents of $BACKUP_PATH backup dir is: \n\n";
$bash_command = `ls $BACKUP_PATH`;
echo 
"$bash_command \n";
echo
"\n";

# unmount the backup parition by it's mount point.
# $bash_command = `umount -v $BACKUP_MOUNTPOINT`;
# echo "$bash_command \n";
  
# end of script 
?>
And the output from this script is:

Code:
[root@nowhere.com /root-admin-scripts/bookmarks]# ./backup-konqueror-bookmarks.php

$SOURCE_FILE is: /root/.kde/share/apps/konqueror/bookmarks.xml

$BACKUP_LABEL_ID is: backups

$BACKUP_MOUNTPOINT is: /backup

$BACKUP_GROUPNAME is: /bookmarks

$BACKUP_PATH is: /backup/bookmarks

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  382M   15G   3% /var/log
 
/dev/sdb6 on /backup type ext3 (rw,noexec)
 

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  382M   15G   3% /var/log
/dev/sdb6             197G   28G  160G  15% /backup
 

CURRENT_DATE is: -2010-07-11_22.37.57

DESTINATION_DIR IS: /backup/bookmarks/bookmarks-2010-07-11_22.37.57
 
`/root/.kde/share/apps/konqueror/bookmarks.xml' -> `/backup/bookmarks/bookmarks-2010-07-11_22.37.57/bookmarks.xml'
 
`/root/.kde/share/apps/konqueror/bookmarks.xml.bak' -> `/backup/bookmarks/bookmarks-2010-07-11_22.37.57/bookmarks.xml.bak'
 
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              25G  9.5G   14G  41% /
tmpfs                 1.3G   12K  1.3G   1% /dev/shm
/dev/sda5              15G  2.3G   12G  16% /home
/dev/sda7             5.0G  162M  4.6G   4% /tmp
/dev/sda8              30G  592M   28G   3% /usr/local
/dev/sda9              50G   23G   25G  48% /downloads
/dev/sda10            138G  219M  131G   1% /var/lib/databases
/dev/sda11             99G  471M   93G   1% /srv
/dev/sda12             50G  1.8G   45G   4% /gospel-media
/dev/sda13             16G  382M   15G   3% /var/log
/dev/sdb6             197G   28G  160G  15% /backup
 
SORTED contents of $directory_name_array[] 
0 => bookmarks-2010-07-07_00.15.02 
1 => bookmarks-2010-07-08_00.15.02 
2 => bookmarks-2010-07-09_00.15.01 
3 => bookmarks-2010-07-10_00.15.02 
4 => bookmarks-2010-07-11_00.15.02 
5 => bookmarks-2010-07-11_22.37.57 

Oldest backup dir is: bookmarks-2010-07-07_00.15.02 

There are 6 backups available. 

NUMBER_REQUIRED_BACKUPS: 5

The oldest backup dir to be deleted is: 
/backup/bookmarks/bookmarks-2010-07-07_00.15.02 
 
There are more than 5 backups in this directory, 
and oldest backup bookmarks-2010-07-07_00.15.02 has been deleted. 

Contents of /backup/bookmarks backup dir is: 

bookmarks-2010-07-08_00.15.02
bookmarks-2010-07-09_00.15.01
bookmarks-2010-07-10_00.15.02
bookmarks-2010-07-11_00.15.02
bookmarks-2010-07-11_22.37.57
The only other thing to do next, is to make regular backups from the backup hard drive, to CD or DVD. I implemented this backup strategy after loosing 2 WD hard drives, and my 3 partitions with website code, home directory, and my databases on them
Attached Files
File Type: txt konq-bookmarks-backup.php.txt (5.1 KB, 154 views)

Last edited by CD-RW; 3rd August 2011 at 09:22 PM. Reason: added uploaded script
Reply With Quote
  #3  
Old 31st July 2011, 10:39 AM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfirefox
Re: Generic PHP CL backup script

Here's another generic version of these scripts to make backups of mysql databases overnight.

Basically It shuts apache down first, waits a short while, then stops mysql, then removes the old log files, and restarts mysql again, then apache.

So you should be able to backup mysql without the tables being modified while the back is running.

You will need to modify this script to suit your needs, and ensure it works as you want, before using it on 'real world' databases.

I accept no responsibility for you using this script - ***USE AT YOUR OWN RISK ONLY!!!***

PHP Code:

#! /usr/bin/php

<?php

# last updated: Thu May 29 15:52:14 BST 2008

# PHP script to backup *all* MySQL databases to /backup-databases/mysql
# this is the version for a default Fedora 8 installation
# of mysql

#------------------------------------------------------#
# how many backups to keep before deleting the oldest backup
define("NUMBER_REQUIRED_BACKUPS"7);

#------------------------------------------------------#
# source directory/files that need backing up.

# the source directory to be backed up.
$SOURCE_DIR="/var/lib/databases/mysql";

# the partition label id of the directory/files
# that need backing up. May not be required.
$SOURCE_LABEL_ID="NONE";

# the directory mountpoint of the directory/files
# that need backing up. May not be required.
$SOURCE_MOUNTPOINT="NONE";

echo 
"\$SOURCE_DIR is: $SOURCE_DIR";
echo
"\n\n";

echo 
"\$SOURCE_LABEL_ID is: $SOURCE_LABEL_ID";
echo
"\n\n";

echo 
"\$SOURCE_MOUNTPOINT is: $SOURCE_MOUNTPOINT";
echo
"\n\n";

#------------------------------------------------------#
# destination for the backups

# the backup partition label id.
$BACKUP_LABEL_ID="database-backups";

# the backup partition directory mountpoint.
$BACKUP_MOUNTPOINT="/backup-databases";

# the backup group name
$BACKUP_GROUPNAME="/mysql";

# the backup group full pathname
$BACKUP_PATH "{$BACKUP_MOUNTPOINT}{$BACKUP_GROUPNAME}";

echo 
"\$BACKUP_LABEL_ID is: $BACKUP_LABEL_ID";
echo
"\n\n";

echo 
"\$BACKUP_MOUNTPOINT is: $BACKUP_MOUNTPOINT";
echo
"\n\n";

echo 
"\$BACKUP_GROUPNAME is: $BACKUP_GROUPNAME";
echo
"\n\n";

echo 
"\$BACKUP_PATH is: $BACKUP_PATH";
echo
"\n\n";

#------------------------------------------------------#

function showMountedPartitions() {
  
# display currently mounted partitions
  
$bash_command = `df -h`;
  echo 
"Mounted Partitions: \n$bash_command \n";
}

showMountedPartitions();

# mount the backup destination partition by label id.
$bash_command = `mount -v -L $BACKUP_LABEL_ID`;
echo 
"$bash_command \n";

showMountedPartitions();

# seconds to wait for apache to shutdown
$APACHE_GRACE_TIME=10;

# output the current version of apache
$bash_command = `apachectl -v`;
echo 
"$bash_command \n";

# gracefully shut-down the apache web server
$bash_command = `apachectl graceful-stop`;
echo 
"$bash_command";
echo 
"Gracefully shutting down Apache web server...\n\n";

echo 
"Waiting $APACHE_GRACE_TIME seconds for Apache web server to finish...\n\n";

# wait for apache to shutdown properly
sleep($APACHE_GRACE_TIME);

echo 
"Apache shutdown OK! \n\n";

# stop the mysql database server
$bash_command = `/etc/init.d/mysqld stop`;
echo 
"$bash_command";

#############################################################
# NOTE: tar does not like filenames with a ':' (colon)
# so DO NOT use as part of the filename extension

$CURRENT_DATE=`date +'-%Y-%m-%d_%H.%M.%S'`;

echo 
"CURRENT_DATE is: " $CURRENT_DATE;
echo
"\n";

$DESTINATION_DIR="{$BACKUP_PATH}{$BACKUP_GROUPNAME}{$CURRENT_DATE}";

echo 
"DESTINATION_DIR IS: $DESTINATION_DIR";

# shell command to copy mysql databases to /backup-databases/mysql
$bash_command = `cp -R --preserve=mode,ownership $SOURCE_DIR $DESTINATION_DIR`;

#############################################################

# check the copied directories with diff
$bash_command = `diff -r $SOURCE_DIR $DESTINATION_DIR`;
echo 
"$bash_command \n";

$backup_dir "{$BACKUP_PATH}/";
$dir_name_count 0;

# Open the backup directory, and proceed to read its contents
if (is_dir($backup_dir)) {
    if (
$dh opendir($backup_dir)) {
        while ((
$file readdir($dh)) !== false) {
            if (!
is_dir($file) && ".." <> $file) {
                
// build an array of directory names
                
$directory_name_array[] = $file;
                
$dir_name_count ++;
            }
        }
        
closedir($dh);
    }
}

# sort the array so the oldest backup is first in the array,
# and the newest backup is last in the array.
sort($directory_name_array);

echo 
"SORTED contents of \$directory_name_array[] \n";
while(list(
$key$value) = each($directory_name_array)) {
   echo 
"$key => $value \n";
   }

echo
"\n";

# get the oldest backup
$oldest_backup $directory_name_array[0];

# print the name of the oldest backup directory in the array
echo "Oldest backup dir is: $oldest_backup \n\n";

# print number of backups available
echo "There are $dir_name_count backups available. \n\n";

# print how many backups are required
echo 'NUMBER_REQUIRED_BACKUPS: ' NUMBER_REQUIRED_BACKUPS "\n\n";

$BACKUP_DELETION_DIR "{$BACKUP_PATH}/$oldest_backup \n";

echo 
"The oldest backup dir to be deleted is: \n";
echo 
"$BACKUP_DELETION_DIR \n";

# delete the oldest backup dir if necessary
if ($dir_name_count NUMBER_REQUIRED_BACKUPS) {
    
# force recursive deletion of directory
    
$bash_command = `rm -fR $BACKUP_DELETION_DIR`;

    echo 
"There are more than " .NUMBER_REQUIRED_BACKUPS" backups in this directory, \n";
    echo 
"and oldest backup $oldest_backup has been deleted. \n\n";
    }
else
{
    echo 
"There are " .NUMBER_REQUIRED_BACKUPS" or less backups in this directory. \n";
    echo 
"No old backups have been deleted. \n\n";
}

echo 
"Contents of $BACKUP_PATH backup dir is: \n\n";
$bash_command = `ls $BACKUP_PATH`;
echo 
"$bash_command \n";

showMountedPartitions();

# before starting mysql, remove old mysql binary log files
echo "Removing old mysql binary log files... \n\n";
$bash_command = `rm -vf /var/lib/databases/mysql/karsites-bin.*`;
echo 
"$bash_command \n";

# before starting mysql, remove old mysql text log files
echo "Removing old mysql text log files... \n\n";
$bash_command = `rm -vf /var/lib/databases/mysql/5-0-45.*`;
echo 
"$bash_command";

# start the mysql database server
$bash_command = `/etc/init.d/mysqld start`;
echo 
"$bash_command";

# output the current version of apache
$bash_command = `apachectl -v`;
echo 
"$bash_command \n";

# start the apache web server
$bash_command = `apachectl start`;
echo 
"$bash_command";
echo 
"Restarting Apache web server... \n\n";

# unmount the /backup-databases partition by it's mount point.
$bash_command = `umount -v $BACKUP_MOUNTPOINT`;
echo 
"$bash_command \n";

showMountedPartitions();

# end of script ?>
Here is the complete PHP5 code for the above script. Obviously you will need to rename it to remove the .txt extension after downloading it
Attached Files
File Type: txt backup-mysql-databases.php.txt (6.0 KB, 147 views)

Last edited by CD-RW; 31st July 2011 at 10:43 AM.
Reply With Quote
  #4  
Old 20th August 2011, 11:51 AM
salemeni Offline
Registered User
 
Join Date: Aug 2011
Posts: 64
linuxchrome
Re: Generic PHP CL backup script

Great work.
Thank you

comparing objects

Last edited by salemeni; 6th December 2011 at 08:34 AM.
Reply With Quote
  #5  
Old 20th August 2011, 08:19 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfirefox
Smile Re: Generic PHP CL backup script

Thanks salemeni. I'm pleased you like it!
Reply With Quote
Reply

Tags
backup, generic, php, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Backup Script scott36 Programming & Packaging 9 9th December 2009 01:42 PM
shell script + backup mohdfarah Programming & Packaging 1 16th June 2006 01:05 AM
Bootable Backup Script redbeardz Links 0 26th October 2005 06:12 PM
backup script ieuuk Programming & Packaging 3 8th November 2004 08:57 PM


Current GMT-time: 05:27 (Tuesday, 27-06-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat