Forum Moderators: bakedjake
The only thing I know is that I need to exclude /proc /dev /tmp /mnt and below is something I found as an example...
tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz \
--directory / --exclude=mnt --exclude=proc --exclude=var/spool/squid .
When I do back it up, where does it go? To my local computer or somewhere on the server?
tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz --directory / --exclude=mnt --exclude=proc --exclude=var/spool/squid .
This backs up the contents of "." -- the current directory -- into a file in /archive with a name like full-backup-30-December-2003. You only need to exclude mnt, proc, etc. if . is / (you run the command in the root directory), but if you do that you definitely want --exclude=archive as well, otherwise you'll end up backing up your previous backup files!
I haven't looked at rsync yet but will.
I've seen scripts that'll do it but they pretty much require you to be an expert server admin to set it up, which I'm not. Since I only have a small number of sites, tar should work and everyone knows how to do it theoretically but can't really tell me how.
Is there an easier solution? I'm about on my last straw and I guess I'm asking for someone to spell it out for me.
I'm sure it's not possible but I wish that I could just go in there and type in "backup all" and then download it, but of course nothing is that easy.
I've been looking at this below to backup but have given up on trying to figure it out.
#!/usr/bin/php
<?#####################Customize############################
//ftp settings
$ftp=1;//ftp send (0 disable, 1 enable)
$ftphost=""; //ftp host
$ftpuser=""; //ftp user
$ftppw=""; //ftp password
$ftpfolder=""; //ftp folder (without slash infront and after)
$ftpdeleteold=1; //delete old backups (0 disable, 1 enable)
$ftpdays=7; //number of days for backup to become "old"
//save settings
$local=1; //store locally (0 disable, 1 enable)
$savepath="/backup/"; //path to store files locally
$deleteold=1; //delete old backups (0 disable, 1 enable)
$days=2; //number of days for backup to become "old"
//mysql settings
$illegaldatabases=1; //backup illegal ensim databases (0 disable, 1 enable)
$userdatabases=1; //backup all databases user has rights to (0 disable, 1 enable)
$excludelogs=1; //exclude logs from backup. (0 disable, 1 enable)
$excludeext=""; //extensions (without a dot) seperated with a space excluded from backup
$date=date("m-d-Y"); //date format of folder *NOTE* if changed change in function deleteold() also.
$tmppath="/tmp"; //temporary store directory
##########################################################
echo "Ensimbackup version 1.1\n";
switch($argv[1]){
case "-d":
if(isset($argv[2])){
backup($argv[2]);
deleteold();
}
else
echo"You need to specify a domain\n";
break;
case "-n":
if(isset($argv[2])){
$domain=trim(`/usr/local/bin/sitelookup -s $argv[2]¦awk -F\, {'print $1'}`);
backup($domain);
deleteold();
}
else
echo"You need to specify a site\n";
break;
case "-a":
backupall();
deleteold();
break;
case "-l":
backuplist($argv[2]);
deleteold();
break;
default:
echo "usage $argv[0]\n";
echo "-d [domain] domain backup\n";
echo "-n [site#] site backup\n";
echo "-a all sites\n";
echo "-l [/path/file] list file (one domain each line)\n";
break;
}
exit;
function getpw(){
$pw=trim(`cd /usr/lib/opcenter/mysql/;
python2.1 -c 'from mysqlbe import *; print read_mysqlpass();'`);
return $pw;
}
function removedirr($conn,$dirname){
$ar_files = ftp_nlist($conn, $dirname);
if(is_array($ar_files)){
for ($i=0;$i<count($ar_files);$i++){
$st_file = $ar_files[$i];
if (ftp_size($conn, $st_file) == -1){
$this->removedirr( $st_file);
} else {
ftp_delete($conn, $st_file);
}
}
}
ftp_rmdir($conn, $dirname);
}
function deleteold(){
global $savepath,$local,$deleteold,$ftpdeleteold;
global $ftp,$ftphost,$ftpuser,$ftppw,$ftpfolder;
global $days, $ftpdays;
$m=date("m");$d=date("d");$y=date("Y");
if($ftpdeleteold AND $ftp){
$conn_id = ftp_connect($ftphost);
// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppw);
$dirs=ftp_nlist($conn_id,"$ftpfolder");
foreach($dirs as $dir){
$del=1;
if(ereg("^$ftpfolder/[0-9]{2}-[0-9]{2}-[0-9]{4}$",$dir)){
for ($i=0; $i<$ftpdays; $i++) {
$checkdate=date("m-d-Y", mktime (0,0,0,$m,$d-$i,$y));
if($dir=="$ftpfolder/$checkdate")
$del=0;
}
if(($del)AND($dir!="")){
echo"Removing on ftp $dir.. ";
removedirr($conn_id,$dir);
echo"done\n";
}
}
}
}
if($deleteold AND $local){
unset($dirs);
$dirs=array();
if ($handle = opendir($savepath)) {
while (false!== ($file = readdir($handle))) {
if (ereg("^[0-9]{2}-[0-9]{2}-[0-9]{4}$",$file)){
$dirs[]=$file;
}
}
closedir($handle);
}
foreach($dirs as $dir){
$del=1;
for ($i=0; $i<$days; $i++) {
$checkdate=date("m-d-Y", mktime (0,0,0,$m,$d-$i,$y));
if($dir==$checkdate)
$del=0;
}
if(($del)AND($dir!="")){
echo"Removing $savepath/$dir.. ";
`rm -rf $savepath/$dir`;
echo"done\n";
}
}
}
}
function xmldump($domain,$site,$path){
global $tmppath;
echo "Dumping account details.. ";
`python2.1 -c "
import sys;
from vh3 import virthost;
xml_dict = {'doctype':'backup','author':'appliance','type':'site','site':'$site'}
xml_list=virthost.create_site_xml(xml_dict)
print (xml_list)
" > $tmppath/$domain.xml`;
`cd $tmppath; gzip $domain.xml`;
echo "done\n";
}
function checkmysql($site){
global $addmysql;
$addmysql="";
$pw=getpw();
$check = file ("/home/virtual/$site/info/current/mysql");
$check[1]=trim($check[1]);
if($check[1]=="enabled = 1"){
return 1;
}
else{
return 0;
}
}
function mysqlbackup($path,$db,$site,$domain){
global $addmysql,$illegaldatabases,$userdatabases;
$pw=getpw();
$check = file ("/home/virtual/$site/info/current/mysql");
$dbadmin=str_replace("dbaseadmin = ","",trim($check[3]));
$connect = mysql_connect("localhost", "root", $pw);
mysql_select_db("mysql",$connect);
$result=mysql_query("select password from user where user='$dbadmin'");
$line=mysql_fetch_object($result);
`echo '$dbadmin\n$line->password' > $path/mysql`;
if($userdatabases){
unset($databases);
$grants=trim(`mysql -e 'SHOW GRANTS FOR \`$dbadmin\`@localhost' -u root --password=$pw`);
preg_match_all("/ON `?(.*?)`?\.\* TO/",$grants, $databases);
foreach($databases[1] as $database){
if($database!=$db AND $database!="*"){
$database=str_replace('\_','_',$database);
echo "Dumping database $database.. ";
`mysqldump -u root --password=$pw --add-drop-table --add-locks --quote-names --extended-insert $database > $path/$database.sql`;
echo "done\n";
}
}
}
if($illegaldatabases){
unset($databases);
$list=trim(`mysql -e 'show databases' -u root --password=$pw`);
preg_match_all("/^$domain$/m", $list,$databases);
foreach($databases[0] as $database){
echo "Dumping database $database.. ";
`mysqldump -u root --password=$pw --add-drop-table --add-locks --quote-names --extended-insert $database > $path/$database.sql`;
echo "done\n";
}
}
else{
echo "Dumping database $db.. ";
`mysqldump -u root --password=$pw --add-drop-table --add-locks --quote-names --extended-insert $db > $path/$db.sql`;
echo "done\n";
}
$addmysql="*.sql mysql";
}
function delmysqlbackup($path,$db){
if($path!="")
`rm -f $path/*.sql`;
if(!@unlink("$path/mysql"))
echo "Couldn't delete mysql";
}
function tarsite($path,$domain,$site){
global $addmysql,$tmppath,$excludelogs,$excludeext;
echo "Tarring $path.. ";
$exts=explode(" ",$excludeext);
$extline="";
foreach($exts as $ext){
$extline.="--exclude=*.$ext ";
}
$excludelog="";
if($excludelogs){
$excludelog="--exclude=var/log/*";
}
`cd $path; tar -zcf $tmppath/$domain.tgz $excludelog $extline etc/httpd etc/mail etc/shadow etc/aliases etc/aliases.db home var $addmysql`;
`cd $tmppath; tar -cf $domain.tar $domain.tgz $domain.xml.gz`;
unlink("$tmppath/$domain.tgz");
unlink("$tmppath/$domain.xml.gz");
echo "done\n";
}
function upload($domain){
global $ftphost,$ftpuser,$ftppw,$ftpfolder;
global $date,$tmppath;
echo "Uploading file.. ";
`ncftpput -m $ftphost -u$ftpuser -p$ftppw $ftpfolder/$date $tmppath/$domain.tar > /dev/null 2>&1`;
echo "done\n";
}
function copyfile($domain){
global $date,$tmppath,$savepath;
if(!file_exists("$savepath/$date")) {
mkdir("$savepath/$date");
}
echo "copying file $domain.tar.. ";
`cp $tmppath/$domain.tar $savepath/$date/$domain.tar`;
echo "done\n";
}
function backup($domain){
global $tmppath,$ftp,$local;
#get variabels
$path=trim(`/usr/local/bin/sitelookup -d $domain ¦ awk -F\, {'print $4'}`);
if($path==""){
exit;
}
$site=trim(`/usr/local/bin/sitelookup -d $domain ¦ awk -F\, {'print $3'}`);
$db=str_replace(".","_",$domain);
#dump mysql database
$mysqlcheck=checkmysql($site);
if($mysqlcheck)
mysqlbackup($path,$db,$site,$domain);
else
echo "$domain has no database\n";
#xml dump
xmldump($domain,$site,$path);
#backup dirs
tarsite($path,$domain,$site);
#remove database back-ups
if($mysqlcheck)
delmysqlbackup($path,$db);
#upload the file
if($ftp){
upload($domain);
}
if($local){
copyfile($domain);
}
unlink("$tmppath/$domain.tar");
echo "\n";
}
function backupall(){
$sites=trim(`/usr/local/bin/sitelookup -a ¦ awk -F\, {'print $1'}`);
$sites=explode("\n", $sites);
foreach($sites as $site){
backup($site);
}
}
function backuplist($listfile){
$sites = file ("$listfile");
foreach($sites as $site){
$site=trim($site);
backup($site);
}
}
?>
This would really make it a lot easier to regularly backup that machine.
If you don't have webmin loaded on your system you may wish to grab it and install it. Webmin has a menu item for backing up your system to a variety of media and using a variety of options. It can shield you from the command line a bit, if necessary.
I have had good results using this to backup my system.