Backups: Difference between revisions

From IHRIS Wiki
(Created page with "Once you've installed iHRIS, you will need to run backups to make sure you don't lose any data if something happens to your hardware. Backups should be moved to a separate machi...")
 
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 36: Line 36:
## Uncomment the following line to remove backup files older than 7 days except
## Uncomment the following line to remove backup files older than 7 days except
## from the first day of the month.
## from the first day of the month.
#find $BACKUP_DIR -name "backup_${DATABASE}*.sql.bz2" -mtime +7 -not -name "back
#find $BACKUP_DIR -name "backup_${DATABASE}*.sql.bz2" -mtime +7 -not -name "backup_${DATABASE}*-01.sql.bz2" -exec rm {} \;
up_${DATABASE}*-01.sql.bz2" -exec rm {} \;
</source>
</source>


Line 98: Line 97:


This script doesn't do any automatic deletion of backup files so you may need to set up something separate to do this.
This script doesn't do any automatic deletion of backup files so you may need to set up something separate to do this.
You can copy this script to your SITE_DIR/cron/local directory as well and make your customizations.  To add this to the cron you would need to include the PHP executable to run the script:
<source lang="text">
0 1 * * * php SITE_DIR/cron/local/backup_exclude_caches.php
</source>
[[Category:Developer Resources]]

Latest revision as of 06:49, 21 August 2019

Once you've installed iHRIS, you will need to run backups to make sure you don't lose any data if something happens to your hardware. Backups should be moved to a separate machine ideally at another location in case a disaster occurs where the server is located.

Backup User

You can use the same database user that you use to connect to the database for iHRIS, but you may also just want to create a read only user to use for backups. You can do this in PHPMyAdmin by creating a new user (we'll call it backup for this, but you can name it anything you'd like). You can generate a password in PHPMyAdmin or set one you'd like to use. You should restrict access to localhost in most cases. The backup user will need SELECT and LOCK TABLES privileges. You can do this per database or on every database on your server depending on your security needs. You can also create the user using mysql from the terminal. Just run the following command in the terminal:

<source lang="bash"> mysql -u root -p </source>

You'll need to enter the root password for the MySQL server and then you can run this command:

<source lang="sql"> GRANT LOCK TABLES,SELECT ON *.* TO 'backup'@'localhost' IDENTIFIED BY 'PASSWORD'; </source>

Just replace PASSWORD with the password you want to use for the backup user.

Full Backup Script

You can now create a shell script to run the database backup and alternatively clean up any old backups so you don't run out of disk space. This will backup every table in the given database so it will be larger since it will back up the form and report caches. This will make restoration faster though as you won't need to recreate these caches. Here is an example script:

<source lang="bash">

  1. !/bin/bash

DATABASE=YOUR_DB BACKUP_DIR=/var/lib/iHRIS/sites/backups DB_USER=backup DB_PASS=PASSWORD


SUFFIX=`date +%F`

cd $BACKUP_DIR mysqldump -u $DB_USER -p$DB_PASS $DATABASE > backup_${DATABASE}_${SUFFIX}.sql bzip2 -f backup_${DATABASE}_${SUFFIX}.sql

    1. Uncomment the following line to remove backup files older than 7 days except
    2. from the first day of the month.
  1. find $BACKUP_DIR -name "backup_${DATABASE}*.sql.bz2" -mtime +7 -not -name "backup_${DATABASE}*-01.sql.bz2" -exec rm {} \;

</source>

You will need to replace YOUR_DB with the database you'd like to backup. You will need to set the DB_USER and DB_PASS based on how you created your backup user. You will also need to create the BACKUP_DIR and set it to where you'd like your backups to go. From a terminal you can create the directory with this command (replacing the directory with the one you've chosen):

<source lang="bash"> mkdir -p /var/lib/iHRIS/sites/backups </source>

You can create this script in your site in a cron directory. To avoid having this file be added to launchpad, you can place it in a local directory, for example: SITE_DIR/cron/local/do_backups.sh

You will also need to be sure your script is executable by running this from a terminal: <source lang="bash"> chmod a+x SITE_DIR/cron/local/do_backups.sh </source>

Setting Up the Cron

For more information on using the cron system, see the Ubuntu Cron HowTo.

Now you'll need to set up the cron to run your backup script nightly. From the previous example the script will be at SITE_DIR/cron/local/do_backups.sh. To edit your cron you can type the following in a terminal: <source lang="bash"> crontab -e </source>

You will see a file to edit similar to this.

<source lang="text">

  1. Edit this file to introduce tasks to be run by cron.
  2. Each task to run has to be defined through a single line
  3. indicating with different fields when the task will be run
  4. and what command to run for the task
  5. To define the time you can provide concrete values for
  6. minute (m), hour (h), day of month (dom), month (mon),
  7. and day of week (dow) or use '*' in these fields (for 'any').#
  8. Notice that tasks will be started based on the cron's system
  9. daemon's notion of time and timezones.
  10. Output of the crontab jobs (including errors) is sent through
  11. email to the user the crontab file belongs to (unless redirected).
  12. For example, you can run a backup of all your user accounts
  13. at 5 a.m every week with:
  14. 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
  15. For more information see the manual pages of crontab(5) and cron(8)
  16. m h dom mon dow command

</source>

You will need to add a new line to the bottom of this file. For example to run your script at 1AM every morning:

<source lang="text"> 0 1 * * * SITE_DIR/cron/local/do_backups.sh </source>

Backup Without Caches

If you have limited space, you may want to backup on the main data and not any cached database tables. This will reduce the size of your backup file. There is a sample script in I2CE/tools/backup_exclude_caches.php. You will just need to configure your $db_user and $db_pass variables as well as the $backupdir. By default, this script will backup all databases on your server so you can set a list of databases to backup using the $include_only array.

This script doesn't do any automatic deletion of backup files so you may need to set up something separate to do this.

You can copy this script to your SITE_DIR/cron/local directory as well and make your customizations. To add this to the cron you would need to include the PHP executable to run the script:

<source lang="text"> 0 1 * * * php SITE_DIR/cron/local/backup_exclude_caches.php </source>