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 drawback of the above method in the topic above is the uselessness. Well, honestly, who needs to backup files and the database at the same time? Those. backing up files and the database every day is stupid, but doing it once a month is stupid. Therefore, I broke my example into 2 parts. Separately, we backup the database daily and separately, we backup files once a week.
Accordingly, we need php on the server. That in our time is no longer luxury, but banality.
So, for myself, I decided that locally backups are stored in / usr / backup /
Create a php script / usr / backup / backup_db:
As you can see, everything is simple and primitive. I note that $ exс is an array with exceptions. Add bases here that we don’t want to backup every day. The rest of the databases that the user $ db_user sees are backed up, each separately and put them in the /usr/backup/mysql/date/base.gz directory.
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, etc. appeared. It is enough for me to copy one file and backups of all projects are collected automatically. It's comfortable.
Accordingly, we add the line to the crowns (for FreeBSD. For Linux, as far as I remember, the other. Please correct, linuxoids):
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 ...
Someone like that, all my projects are in / usr / local / www / ... As you can see, the script will backup everything except the log files and svn files. As for svn, maybe I'm wrong, but so far there have been no problems with this. Also, the $ exc array is an exception. For this script in cron we write a slightly different rule: in this way we backup scripts and statics at 5 a.m. on the weekend.
I use these scripts not only on my servers, but also on client ones. True, on the client I also supplement them by copying archives to third servers to increase fear. On some servers, separate tables in the necessary databases will be backed up every hour, etc. PHP provides the flexibility that is so lacking in bash.
ps after adding data to the crowns 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.
The main drawback of the above method in the topic above is the uselessness. Well, honestly, who needs to backup files and the database at the same time? Those. backing up files and the database every day is stupid, but doing it once a month is stupid. Therefore, I broke my example into 2 parts. Separately, we backup the database daily and separately, we backup files once a week.
Accordingly, we need php on the server. That in our time is no longer luxury, but banality.
So, for myself, I decided that locally 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('d.m.Y').'/');
$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('d.m.Y').'/'.$base.'.gz');
}
}
As you can see, everything is simple and primitive. I note that $ exс is an array with exceptions. Add bases here that we don’t want to backup every day. The rest of the databases that the user $ db_user sees are backed up, each separately and put them in the /usr/backup/mysql/date/base.gz directory.
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, etc. appeared. It is enough for me to copy one file and backups of all projects are collected automatically. It's comfortable.
Accordingly, we add the line to the crowns (for FreeBSD. For Linux, as far as I remember, the other. Please correct, linuxoids):
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 ...
Someone like that, all my projects are in / usr / local / www / ... As you can see, the script will backup everything except the log files and svn files. As for svn, maybe I'm wrong, but so far there have been no problems with this. Also, the $ exc array is an exception. For this script in cron we write a slightly different rule: in this way we backup scripts and statics at 5 a.m. on the weekend.
#!/usr/local/bin/php
$exс=array('test.ru');
$dir='/usr/local/www/';
system('mkdir -p /usr/backup/www/'.date('d.m.Y').'/');
$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('d.m.Y').'/'.$file.'.tar.gz --exclude=*.log* --exclude=*.svn* '.$dir.$file);
}
}
0 5 * * 6 root /usr/backup/backup_www
I use these scripts not only on my servers, but also on client ones. True, on the client I also supplement them by copying archives to third servers to increase fear. On some servers, separate tables in the necessary databases will be backed up every hour, etc. PHP provides the flexibility that is so lacking in bash.
ps after adding data to the crowns 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.