📜 ⬆️ ⬇️

Elementary PHP scripts for data backup

In response to a recent topic about backups on Bash. The topic is useful, no doubt, but I want to demonstrate a more flexible way ...
The main disadvantage of the above method in the topic above is the uselessness. Well, honestly, who needs to back up files and database at the same time? Those. backing up every day both the files and the base is stupid, but doing it once a month is stupid. Therefore, I broke my example into 2 parts. Separately, we back up the database daily and separately, back up files once a week.
Accordingly, we need php on the server. What in our time is no longer a luxury, but a trivial matter.
So, for myself, I decided that local backups are stored in / usr / backup /
Create a php script / usr / backup / backup_db:

#!/usr/local/bin/php
<?
$ex=array('information_schema', 'mysql');
$db_pass='password';
$db_user='root';

system('mkdir -p /usr/backup/mysql/'.date('dmY').'/');
$db=mysql_connect("localhost", $db_user, $db_pass);
$sql=mysql_query("SHOW DATABASES");
while($a=mysql_fetch_assoc($sql)){
$base=$a['Database'];
if(!in_array($base, $ex)){
exec('/usr/local/bin/mysqldump -u'.$db_user.' -p'.$db_pass.' --opt '.$base.' | /usr/bin/gzip > /usr/backup/mysql/'.date('dmY').'/'.$base.'.gz');
}
}

As you can see, everything is simple and primitive. Note that $ exc is an array with exceptions. Here we add databases that we don’t want to back up every day. The rest of the database that the user sees $ db_user, we back up, each separately and put them in the directory /usr/backup/mysql/data/baz.gz
This is much more convenient than creating a separate backup file for each project. For example, I have 2 servers and on each 10+ projects. Not so much, but the number is constantly updated. Someone left, someone came, a new server appeared and so forth. It is enough for me to copy one file and backups of all projects are collected automatically. It's comfortable.
Accordingly, we add to the cron line (for FreeBSD. For Linux, as far as I remember, the other. Please correct, Linux):
0 5 * * * root /usr/backup/backup_db
Which says that every day at 05:00 we collect a new backup of the base and gzipem it.

Well, do not forget about the code and pictures ... / usr / backup / backup_www ...
Who else, I have all the projects in / usr / local / www / ...
')
#!/usr/local/bin/php
<?
$ex=array('test.ru');
$dir='/usr/local/www/';

system('mkdir -p /usr/backup/www/'.date('dmY').'/');
$d=opendir($dir);
while(($file=readdir($d))!==false){
if($file!='.' && $file!='..' && is_dir($dir.'/'.$file) && !in_array($file, $ex)){
exec('tar cpzvf /usr/backup/www/'.date('dmY').'/'.$file.'.tar.gz --exclude=*.log* --exclude=*.svn* '.$dir.$file);
}
}

As you can see, the backup script is all but log files and svn files. I may be wrong about svn, but so far there have been no problems with it. The $ exc array is also an exception.
For this script in cron we write a slightly different rule:
0 5 * * 6 root /usr/backup/backup_www
thus backing up scripts and statics at 5 am on weekends.
I use these scripts not only on my servers, but also on client ones. True, on the client I supplement them with copying archives to third servers to heighten fear. On some servers, separate tables in the necessary databases back up every hour and so on. PHP gives you the flexibility that bash lacks.

ps after adding the data to the cron, do not forget to reboot it. And do not forget to set chmod + x for backup_db and backup_www
pps any additions and criticism are welcome.

Source: https://habr.com/ru/post/52004/


All Articles