Skip to content

Instantly share code, notes, and snippets.

@maerlyn
Created August 30, 2012 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maerlyn/3530787 to your computer and use it in GitHub Desktop.
Save maerlyn/3530787 to your computer and use it in GitHub Desktop.
ftp-only backup megoldas
Egyszerű backup megoldás olyan tárhelyekhez, ahol csak ftp elérés van. Részletek a blogomon: http://maerlyn.eu/2012/08/30/ftp-only-backup.html
<?php
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$return = "";
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
file_put_contents(dirname(__FILE__) . "/dbbackup.sql", $return);
}
backup_tables("localhost", "user", "pass", "dbname");
#!/bin/bash
ssh-add
cd /home/high/backup
find ftp.example.com -mindepth 1 -maxdepth 1 -not -name .git -exec rm -r {} \;
wget --http-user=httpuser --http-password=httppassword -O /dev/null http://example.com/dbbackup/backup.php
wget --ftp-user=ftpuser --ftp-password=ftppassword --recursive --level=inf ftp://ftp.example.com
if [[ $? -ne 0 ]]; then
echo "Halozati hiba miatt meghiusult a letoltes." | mail -s "[backup] halozati hiba" "email@example.com"
exit 0
fi
cd ftp.example.com
[[ `git status --porcelain | wc -l` -ne 0 ]] || exit 0
STATUS=`git status`
git add -A
git commit -m "auto backup `date "+%Y-%m-%d %H:%M"`"
git push origin master
echo "${STATUS}" | mail -s "[backup] `date "+%Y-%m-%d %H:%M"`" "email@example.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment