📜 ⬆️ ⬇️

Backing up files and databases

Script features:
- backup directories and databases
- download archives to a local folder or FTP
- email notification of successful / unsuccessful completion


Backup directories

Creating a backup directory is easy. It is enough just to specify let (path) to the folder that we want to archive and give the name a backup (name). Optionally, you can specify folders and files that we want to exclude.
The number of directories for which you need to create a backup may be several

Backup DB

Just as easy to create a backup database. It is enough to specify the database name (db_name). Optionally, you can exclude some tables (for example, test ones) or import only the structure of tables (for example, a log table). As with directories, the number of databases may be several
')
Backup download

The created bykap can be saved either to a local folder or upload via FTP. Here you can also specify the number of copies you want to keep, that is, after downloading a new archive, the old ones will be deleted.

Backup Alert

After the backup is created and saved in the specified location, you can set up an email alert. Upon successful completion (on_success), the template of the letter that you specify (template) will be taken, all statistics will be parsed into it and sent to your email address. You can also set up sending a letter if any errors occur (on_failed)

Using

Config example:
array( // common options 'common' => array( 'tar_cmd' => '/bin/tar', 'gzip_cmd' => '/bin/gzip', 'backup_filename_prefix' => $prefix, 'backup_filename' => 'backupname', ), // backup options 'backup' => array( // directory backup 'directory' => array( 'tar_cmd' => '/bin/tar', 'items' => array( array( 'name' => 'home_user1', 'path' => '/home/user1', 'exclude' => 'tmp,logs,cache', ), array( 'name' => 'home_user2', 'path' => '/home/user2', 'exclude' => 'tmp', ) ) ), // database backup 'mysql' => array( 'mysqldump_cmd' => '/usr/bin/mysqldump', 'user' => 'root', 'password' => 'xxx', 'host' => 'localhost', 'items' => array( array( 'db_name' => 'xxx', 'ignore_tables' => 'test', 'tables_structure' => 'logs,sessions', ), array( 'db_name' => 'xxx2', ), ), ), ), // upload backup options 'upload' => array( // upload to local directoey 'directory' => array( 'max_count' => 3, 'path' => '/backups', ), // upload to ftp 'ftp' => array( 'max_count' => 3, 'path' => '/backups', 'host' => 'xxx', 'user' => 'xxx', 'password' => 'xxx' ), ), // notification options 'nofification' => array( // email notification 'email' => array( 'on_success' => array( 'to' => 'xxx@xxx.xxx', 'subject' => 'Success backup', 'template' => realpath(__DIR__ . '/../Command/Notification/email_templates/success.php') ), 'on_failed' => array( 'to' => 'xxx@xxx.xxx', 'subject' => 'Failed backup', 'template' => realpath(__DIR__ . '/../Command/Notification/email_templates/failed.php') ), ), ), 


Example of use:
 $backupTask = new BackupTask\BackupTask($config); $backupTask->run(); 


Example crown:
 @daily /usr/bin/php /path/to/backup.php daily @weekly /usr/bin/php /path/to/backup.php weekly @monthly /usr/bin/php /path/to/backup.php monthly 


Sources here

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


All Articles