Views: 8920
Last Modified: 30.12.2022

Scheduled backups

Often, when deploying project, based on BitrixVA/BitrixEnv, a backup copy for the project is required.

Automatic backup feature for the site and the database is available in BitrixVA/BitrixEnv. Backup will be created as per schedule in the .tar.gz archive format and recorded in the directory /home/bitrix/backup/archive/.

This method has both advantages and disadvantages, compared to Bitrix24 products built-in backup copy mechanism:

  • Advantages: higher creation speed for backup copy and independence from project performance.
  • Disadvantages: backup copy cannot be created for files, stored in Cloud-storage drives.

Creating a schedule

To create scheduled automatic backup copy via BitrixVA/BitrixEnv tools:

  • Select item 6. Manage sites in the pool > 6. Change backup settings on site in the Virtual Appliance settings.
  • Select host name from the list and confirm the option to change of automatic backup schedule:

  • Select period and hour for the start of automatic backup:

    If more detailed configuration for backup is required, command line utility can be used:

    /opt/webdir/bin/bx-sites -a backup -d dbcp --enable --minute=10 --hour=18 --day=any --month=any --weekday=any
    

    Note: How to configure the correct time in BitrixVA/BitrixEnv, see here.

  • After this step, the work of configuration wizard is finished. The task to create backup copy for project is added in Cron (/etc/crontab).

    Backup is created for kernel (site type kernel and ext_kernel) and its all links, if such exist. To perform backup, the task is created in crontab-file. For example:

    10 22 * * * bitrix /opt/webdir/bin/bx_backup.sh sitemanager0 /home/bitrix/backup/archive
    

    As the first option, DB name is added; second option indicates catalogue, where the archive will be created.

    As a result, the following type of archive will be created by the script: www_backup__DD.MM.YYYY_<random_string>.tar.gz (for example - www_backup_dbcp_21.10.2014_1RJKXbMv.tar.gz).

    The following files could exist inside the archive:

    1. DB dump /home/bitrix/mysql_dump__DD.MM.YYYY_.sql
    2. site kernel data
    3. sites Links-type data with full path

Managing backups via bx-sites

  • -a|--action - action to manage the site; in this case it executes backup
  • -d|--database - DB name (data for all sites that use this DB will be contained in the backup)
  • --enable|--disable - enabling and disabling backup for sites
  • --minute - parameters for crontab file record (minutes)
  • --hour - parameters for crontab file record (hours)
  • --day - parameters for crontab file record (day)
  • --month - parameters for crontab file record (month)
  • --weekday - parameters for crontab file record (week day)

In case of successful execution, the utility will return new options for site:

/opt/webdir/bin/bx-sites -a backup -d sitemanager0 --enable --minute=10 --hour=23 --day=1 --month=any --weekday=any -o json | python -mjson.tool  
...
            "BackupCronFile": "/etc/crontab",
            "BackupDay": "1",
            "BackupFolder": "/home/bitrix/backup/archive",
            "BackupHour": "23",
            "BackupMinute": "10",
            "BackupMonth": "*",
            "BackupTask": "enable",
            "BackupVersion": "v5",
            "BackupWeekDay": "*", 
...

Exclusion lists

Several files/catalogues are queried to be excluded from the backup copy. The list of such exclusions can be found in the file /opt/webdir/bin/ex.txt.

By default, it contains the following subcatalogues:

bitrix/cache
bitrix/managed_cache
bitrix/stack_cache
bitrix/local_cache
bitrix/backup
bitrix/tmp
upload/tmp
upload/resize_cache

Backup contents/recovery

As noted above, backup includes:

  • site kernel catalogue itself (kernel or ext_kernel);
  • DB dump file (/home/bitrix/mysql_dump_<db>.sql);
  • sites catalogues (link), which use the kernel.

For example, the command:

/opt/webdir/bin/bx_backup.sh sitemanager /home/bitrix/backup/archive 

creates file www_backup_sitemanager_30.01.2015_bnnW1NPm.tar.gz in the directory /home/bitrix/backup/archive/

To perform backup, it is necessary to go to DocumentRoot of the kernel. Execute:

tar xzvvf www_backup_dbvm04_22.08.2018_YBC0dLJe.tar.gz
-rw-rw-r-- bitrix/bitrix      490 2017-11-22 17:46 ./telephony/lines.php
-rw-rw-r-- bitrix/bitrix      543 2017-11-22 17:46 ./telephony/editgroup.php
-rw-rw-r-- bitrix/bitrix       42 2017-11-22 17:46 ./telephony/.access.php
-rw-rw-r-- bitrix/bitrix      496 2017-11-22 17:46 ./telephony/groups.php
drwxrwxr-x bitrix/bitrix        0 2017-11-22 17:26 ./auth/
-rw-rw-r-- bitrix/bitrix      593 2017-11-22 17:26 ./auth/.left.menu.php
-rw-rw-r-- bitrix/bitrix      534 2017-11-22 17:26 ./auth/index.php
-rw-rw-r-- bitrix/bitrix      248 2017-11-22 17:26 ./auth/.section.php
-rw-rw-r-- bitrix/bitrix  5794692 2018-08-22 16:29 home/bitrix/mysql_dump_dbvm04_22.08.2018_YBC0dLJe.sql
drwxr-xr-x                     Creating directory: home
drwxr-xr-x                     Creating directory: home/bitrix
-rw-rw-r-- bitrix/bitrix       44 2018-08-22 16:29 home/bitrix/mysql_dump_dbvm04_22.08.2018_YBC0dLJe_after_connect.sql

After that, DB is to be restored:

mysql sitemanager0 < home/bitrix/mysql_dump_sitemanager_30.01.2015_bnnW1NPm_after_connect.sql

You can use similar command to restore the file mysql_dump_sitemanager_xxxx.sql.

In case the copy is being restored on another server, the archived database password won't match to the new database, because passwords are generated at random after new virtual appliance is installed. It means that after restoring from backup, you need to change the bitrix user password, taken from /bitrix/.settings.php after unpacking the archive.

You can change the password via SQL query:

SET PASSWORD FOR 'bitrix'@'localhost' = PASSWORD;

Next, restore the data of additional sites, if available:

rsync -av home/bitrix/ext_www/ /home/bitrix/ext_www/

Proceed to deleting the database dump file.


Attention!: Do not forget to monitor the space on disk and periodically delete old backup copies.




Courses developed by Bitrix24