manniru
3/10/2017 - 6:52 AM

backup

backup

<?php
/** http://www.djs-consulting.com/linux/blog/2008/a-handy-php-backup-script
 * Generic Backup Script.
 * 
 * To configure this script for your purposes, just edit the parameters below.
 * Once you have the parameters set properly, when the script executes, it will
 * create an archive file, gzip it, and e-mail it to the address specified.  It
 * can be executed through cron with the command
 * 
 * php -q [name of script]
 * 
 * You are free to use this, modify it, copy it, etc.  However, neither DJS
 * Consulting nor Daniel J. Summers assume any responsibility for good or bad
 * things that happen when modifications of this script are run.
 * 
 * @author Daniel J. Summers <daniel@djs-consulting.com>
 */

// --- SCRIPT PARAMETERS ---

/*  -- File Name --
	This is the name of the file that you're backing up, and should contain no
	slashes.  For example, if you're backing up a database, this might look
	something like...
$sFilename = "backup-my_database_name-" . date("Y-m-d") . ".sql"; */
$sFilename = "backup-[whatever-it-is]-" . date("Y-m-d") . ".[extension]";

/*  -- E-mail Address --
	This is the e-mail address to which the message will be sent. */
$sEmailAddress = "[your e-mail address]";

/*  -- E-mail Subject --
	This is the subject that will be on the e-mail you receive. */
$sEmailSubject = "[something meaningful]";

/*  -- E-mail Message --
	This is the text of the message that will be sent. */
$sMessage = "Compressed database backup file $sFilename.gz attached.";

/*  -- Backup Command --
	This is the command that does the work.

	A note on the database commands - your setup likely requires a password
	for these commands, and they each allow you to pass a password on the
	command line.  However, this is very insecure, as anyone who runs "ps" can
	see your password!  For MySQL, you can create a ~/.my.cnf file - it is
	detailed at http://dev.mysql.com/doc/refman/4.1/en/password-security.html .
	For PostgreSQL, the file is ~/.pgpass, and it is detailed at
	http://www.postgresql.org/docs/8.0/interactive/libpq-pgpass.html .  Both of
	these files should be chmod-ded to 600, so that they can only be viewed by
	you, the creator.

	That being said, some common commands are...

- Backing Up a MySQL Database
$sBackupCommand = "mysqldump -u [user_name] [db_name] > $sFilename";

- Backing Up a PostgreSQL Database
$sBackupCommand = "pg_dump [db_name] -h localhost -U [user_name] -d -O > $sFilename";

- Backing Up a set of files (tar and gzip)
$sBackupCommand = "tar cvf $sFilename [directory]/*";

Whatever command you use, this script appends .gz to the filename after the command is executed.  */
$sBackupCommand = "[a backup command]";

// --- END OF SCRIPT PARAMETERS ---
//
// Edit below at your own risk.  :)

// Do the backup.
$sResult = passthru($sBackupCommand . "; gzip $sFilename");
$sFilename .= ".gz";

// Create the message.
$sMessage = "Compressed database backup file $sFilename attached.";
$sMimeBoundary = "<<<:" . md5(time());
$sData = chunk_split(base64_encode(implode("", file($sFilename))));

$sHeaders = "From: $sEmailAddress\r\n"
		. "MIME-Version: 1.0\r\n"
		. "Content-type: multipart/mixed;\r\n"
		. " boundary=\"$sMimeBoundary\"\r\n";

$sContent = "This is a multi-part message in MIME format.\r\n\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
		. "Content-Transfer-Encoding: 7bit\r\n\r\n"
		. $sMessage."\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Disposition: attachment;\r\n"
		. "Content-Type: Application/Octet-Stream; name=\"$sFilename\"\r\n"
		. "Content-Transfer-Encoding: base64\r\n\r\n"
		. $sData."\r\n"
		. "--$sMimeBoundary\r\n";

// Send the message.
mail($sEmailAddress, $sEmailSubject, $sContent, $sHeaders);

// Delete the file - we don't need it any more.
unlink($sFilename);
?>
#!/usr/bin/perl -w

# SIMPLE FTP BACKUP SCRIPT V0.9c
# COPYRIGHT 2009 - THEWEBHOSTINGHERO.COM
# http://www.thewebhostinghero.com
#http://www.thewebhostinghero.com/articles/simple-ftp-backup-script-0-9b-released.html

use Net::FTP;

# DELETE BACKUP AFTER FTP UPLOAD (0 = no, 1 = yes)
$delete_backup = 1;

# ENTER THE PATH TO THE DIRECTORY YOU WANT TO BACKUP, NO TRAILING SLASH
$directory_to_backup = '/path/to/your/site';

# ENTER THE PATH TO THE DIRECTORY YOU WISH TO SAVE THE BACKUP FILE TO, NO TRAILING SLASH
$backup_dest_dir = '/path/to/backup/directory';

# BACKUP FILE NAME OPTIONS
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
$site_backup_file = "$backup_dest_dir/site_backup-$day-$month-$year.tar.gz";
$full_backup_file = "$backup_dest_dir/full_site_backup-$day-$month-$year.tar.gz";

# MYSQL BACKUP PARAMETERS
$dbhost = 'localhost';
$dbuser = 'username';
$dbpwd = 'password';
$mysql_backup_file = "$backup_dest_dir/mysql_databases-$day-$month-$year.sql.gz";
$backup_all_databases = 'no';

# ENTER DATABASE NAMES TO BACKUP SEPARATED BY SPACES * MUST SET backup_all_databases to 'no' *
$database_names = 'db1 db2 db3';

# FTP PARAMETERS
$ftp_backup = 0;
$ftp_host = "my-ftp-server.com";
$ftp_user = "user";
$ftp_pwd = "password";
$ftp_dir = "/remote_backups";

# SYSTEM COMMANDS
$cmd_mysqldump = '/usr/bin/mysqldump';
$cmd_gzip = '/usr/bin/gzip';

# ----- DO NOT EDIT BELOW THIS LINE -----

# CURRENT DATE / TIME
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;

# BACKUP FILES
$syscmd = "tar --exclude $backup_dest_dir" . "/*  -czf $site_backup_file $directory_to_backup";
system($syscmd);

# MYSQL DATABASE BACKUP
if($backup_all_databases eq 'yes')
{
  $syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --all-databases -c -l --result-file=$mysql_backup_file";
}
else
{
  $syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --databases $database_names -c -l | $cmd_gzip > $mysql_backup_file";
}

system($syscmd);

# CREATING FULL SITE BACKUP FILE
$syscmd = "tar -czf $full_backup_file $mysql_backup_file $site_backup_file";
system($syscmd);

# DELETING SITE AND MYSQL BACKUP FILES
unlink($mysql_backup_file);
unlink($site_backup_file);

# UPLOADING FULL SITE BACKUP TO REMOTE FTP SERVER
if($ftp_backup == 1)
{
	my $ftp = Net::FTP->new($ftp_host, Debug => 0)
	  or die "Cannot connect to server: $@";
	
	$ftp->login($ftp_user, $ftp_pwd)
	  or die "Cannot login ", $ftp->message;
	  
	$ftp->cwd($ftp_dir)
	  or die "Can't CWD to remote FTP directory ", $ftp->message;
	  
	$ftp->binary();
	
	$ftp->put($full_backup_file)
	  or warn "Upload failed ", $ftp->message;
	
	$ftp->quit();
}

# DELETING FULL SITE BACKUP
if($delete_backup = 1)
{
	unlink($full_backup_file);
}
for i in *; do if [ -d "$i" ]; then tar czf "$i".tar.gz  "$i"; fi; done
###
# cPanel Backup via Cron v1.2.1
###
#http://jussi.ruokomaki.fi/tech/automatic-cpanel-backup-via-curl-cron/

###
#  Instructions
###
# Make sure CP_SCP_BACKUP_ROOT directory exists. It is "web_backup" by default.
# Within this directory you'll need a directory for each cPanel account you're
# backing up.
#
# I.e. if you're backing up "example.com" and "foobar.com" and you want them
# to be backed up in the default location of "web_backup", you'll need to
# execute the following commands at the recieving end:
# mkdir ~/web_backup
# mkdir ~/web_backup/example.com
# mkdir ~/web_backup/foobar.com
###


###
# Config begins
### 

#cPanel info -- host that is being backed up
CP_USERNAME=username
CP_PASSWORD=password
CP_DOMAIN=yourdomain.com
CP_SKIN=x3 # plain "x" is probably the most common one

#SCP/FTP info -- host to receive the backup file
CP_SCP_USER=username
CP_SCP_PASS=password
CP_SCP_HOST=backup-host.com
CP_SCP_PORT=22

# see cPanel backup page for alternatives of transfer mode
CP_SCP_MODE=scp

# email address for sending notification after backup is completed
CP_EMAIL=webmaster@example.com

# target directory on remote host; do not include domain names in this one
CP_SCP_BACKUP_ROOT=web_backup%2F  # url encoded, e.g. "/" equals "%2F"

###
# Config ends
### 



# creates the target path (directory and filename)
# if you make changes, remember to url encode slashes, i.e. "/" equals "%2F"
# if --year option is used, script creates a yearly filename, otherwise a monthly one
if [ "$1" = '--year' ]; then
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-year-`date +%Y`.tar.gz
else
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-month-`date +%m`.tar.gz
fi

# initiate backup (the following command should be on one line)
curl --silent --insecure --user $CP_USERNAME:$CP_PASSWORD -d "dest=$CP_SCP_MODE&email=$CP_EMAIL&server=$CP_SCP_HOST&user=$CP_SCP_USER&pass=$CP_SCP_PASS&port=$CP_SCP_PORT&rdir=$CP_SCP_RDIR" https://$CP_DOMAIN:2083/frontend/$CP_SKIN/backup/dofullbackup.html > /dev/null

# gets curl return code
CURL_EXIT=$?

# curl encountered an error
if [ $CURL_EXIT -gt 0 ]; then
	echo curl returned exit status $CURL_EXIT - see curl manual pages for more details
	exit 69
fi

# everything ok
exit 0
<?php

// PHP script to allow periodic cPanel backups automatically.
// Permissions on this file should be 600 
// Place outside your public_html 
// Crontab:  30 3 * * * /usr/local/bin/php /home/username/cpanel_backup.php
// http://www.v-nessa.net/2007/01/03/cpanel-automated-backup-script

// ********* Configuration *********

// Info required for cPanel access
$cpuser = "usern5"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = "yourdomain.com"; // Domain name where CPanel is run
$skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP host
$ftpuser = "username"; // Username for FTP account
$ftppass = "password"; // Password for FTP account
$ftphost = "ftp.yourdomain.com"; // Full hostname or IP address for FTP host
$ftpmode = "passiveftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = "your@email"; // Email address to send results

// Secure or non-secure mode
$secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 1;

// *********** Don't Touch!! *********

if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
  $response = fgets($socket,4096);
  if ($debug) echo $response;
}

fclose($socket);

?>
echo 'backup attached' | mutt -a backup.sql.tar.gz -s “daily backup of database” email@gmail.com
# info: http://it.dennyhalim.com/2009/10/perfect-mysql-backup.html

/bak/mysql/*/*.sql.gz {
        daily
        rotate 5
        nocompress
        postrotate
                export HOME=/root
                for i in `mysql --batch -e 'show databases' | tail -n +2`; do
                        if [ ! -e /bak/mysql/$i ]; then mkdir -m 700 /bak/mysql/$i; fi
                        mysqldump --opt --single-transaction -u LocalUserWithMinimalReadOnlyRight $i | gzip -c > /bak/mysql/$i/$i.sql.gz
                done
        endscript
}

/bak/mysql/*/*.sql.gz.5 {
        weekly
        rotate 4
        nocompress
}

/bak/mysql/*/*.sql.gz.5.4 {
        monthly
        rotate 3
        nocompress
}

# http://it.dennyhalim.com/2009/10/perfect-mysql-backup.html
#
# tes: logrotate -f backup-mysql.logrotate