Scheduled Database Backup

From TBwiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
This procedure shows how to configure a Tmedia to send its database backup files to a remote server automatically.
+
This procedure shows how to configure a periodic database backup using a bash script (''tbdbbackup'') executed from a Tmedia host unix cron jobs. Cron allows Linux and Unix users to run commands or scripts at a given date and time.<br><br>
  
Warning: If the destination is a Windows server, you require to install software that supports scp. Not tested by TelcoBridges personnel.
+
= "tbdbbackup" Script =
 +
The ''tbdbbackup'' script support the following features:
 +
*Backup the TMG-CONTROL configuration database into a file
 +
*Backup file name formatted as  ''tbdbbackup_<hostname>_toolpack_<version>_<date>_<time>.sql.gz''
 +
  tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz
 +
*Keep N days of local backup files. Default is 7 days.
 +
*Backup files are accessible from the Web portal in Backups->Manual Database Backup
 +
*Optional copy to a remote server supporting [https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server SSH Key-Based Authentication]
 +
**Copy the backup file on a remote server (using scp) - optional
 +
**Keep M days of backup files on the remote server (using ssh) - optional
  
The shell script used by this procedure is included in the [[Tmedia]]'s distribution. For other distribution the tbcopycdr script can be decompressed from the [http://repo.telcobridges.com/centos/5.5/updates/SRPMS/tb_tools-1.1-3.src.rpm tb_tools.src.rpm] SRPM.
+
= Get the Script =
 +
On latest product version, the script should be available from the ''/usr/bin/''. To validate if you have the script on your Tmedia:
 +
*Connect to the [[Tmedia]] using SSH as root
 +
[root@TB014369 ~]# ls /usr/bin/tbdbbackup
 +
ls: cannot access /usr/bin/tbdbbackup: No such file or directory
 +
 
 +
If the script is not available on your Tmedia:
 +
* get the script from  
 +
 
 +
= Configure the Script =
 +
1. Copy the script to /root
 +
'''Note''': You must copy the script to another location to avoid loosing your configurations when upgrading your host (yum update).
 +
  cp /usr/bin/tbdbbackup /root/tbdbbackup
 +
  chmod +x /root/tbdbbackup
 +
2. Edit the script file:
 +
vim /root/tbdbbackup
  
= Setup SSH Public and Private Key =
+
3. Configure the following variables:
 +
*''LOCAL_BACKUP_MAX_DAYS'' : Keep the local backup files for that number of days. Set to 7 days by default.
 +
  # Add '#' in front of the line to disable the local backup file cleanup
 +
  #LOCAL_BACKUP_MAX_DAYS="7"
 +
*''REMOTE_BACKUP_MAX_DAYS'' :  Keep the local backup files for that number of days. Set to 7 days by default.
 +
  # Add '#' in front of the line to disable the remote backup file cleanup
 +
  #LOCAL_BACKUP_MAX_DAYS="7"
 +
*''REMOTE_HOST'' : Remote host name or Ip address.
 +
  # Add '#' in front of the line to disable the remote copy
 +
  #REMOTE_HOST = 10.10.10.10
 +
*''REMOTE_PORT'' : Remote host name or Ip address
 +
*''REMOTE_DIR'' : Remote host directory where the backup files will be copied
 +
*''REMOTE_USER'' : Remote host user
  
This will enable the [[Tmedia]] to send files to the remote host without asking for a password:
+
= Create an SSH Key =
 +
You must create an SSH key pair and copy it to the remote server, if the remote server copy is configured. This will enable the [[Tmedia]] to copy the backup files to the remote host without asking for a password:
  
 
*Connect to the [[Tmedia]] using SSH as root
 
*Connect to the [[Tmedia]] using SSH as root
Line 24: Line 61:
 
More details here: [[Password_less_ssh|Password-less SSH]]
 
More details here: [[Password_less_ssh|Password-less SSH]]
  
= Configure the Tmedia to Send files =
+
= Test your Script =
 +
1.Manually run the script/;
 +
/root/tbdbbackup
  
*Open the file configuration
+
2.Check if the backup is locally created:
  vim /usr/bin/tbcopybackup
+
  ls -alt /lib/tb/toolpack/pkg/database_backups/
  
*Set the following variables in the file
+
3.If the remote copy is configured, make sure the file was copied on the remote server
**TMG_CDR_DIR (Tmedia directory where the [[Text_Call_Detail_Records_(CDR)|CDR]] are located, default directory in file is incorrect )
+
**REMOTE_DIR  (Remote host directory where the [[Text_Call_Detail_Records_(CDR)|CDR]] will be send, the directory MUST exist and directory MUST end with a '/')
+
**REMOTE_USER (Remote host user)
+
**REMOTE_HOST (Remote host name or Ip address)
+
**ERASE_AFTER_SEND (TMedia will erase the [[Text_Call_Detail_Records_(CDR)|CDR]] files locally or not after a successful send, default true)
+
  
Note: if you set ERASE_AFTER_SEND to false the CDR text file will be available in the /home/telcobridges/cdr/ directory and rename *.gz.already_send.
+
4.Test "LOCAL_BACKUP_MAX_DAYS" (if configured):
 +
*run the script a few times to create multiple files
 +
*use "touch" to change the date of a file to a date older then "LOCAL_BACKUP_MAX_DAYS" days ago. For example, if "LOCAL_BACKUP_MAX_DAYS" is set to 15 days, set the date of the file as "16 days ago".
 +
[root@TB014369]# ls -alt /lib/tb/toolpack/pkg/database_backups/
 +
total 820
 +
-rw-r--r--  1 root  root  206638 Jan 11 16:40 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
 +
-rw-r--r--  1 root  root  206612 Jan 11 15:52 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz
  
Example: Send the files to the home directory of the root user of host 10.10.10.10
+
[root@TB014369 database_backups]# touch -d "16 days ago" /lib/tb/toolpack/pkg/database_backups/tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
TMG_CDR_DIR=/lib/tb/toolpack/setup/12358/cdr_logs/
+
REMOTE_DIR=~/
+
REMOTE_USER=root
+
REMOTE_HOST=10.10.10.10
+
ERASE_AFTER_SEND=true
+
  
=== Validate Setup Configuration ===
+
[root@TB014369]# ls -alt /lib/tb/toolpack/pkg/database_backups/
 +
total 820
 +
-rw-r--r--  1 root  root  206612 Jan 11 15:52 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz.
 +
-rw-r--r--  1 root  root  206638 Dec 26 16:43 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
  
*Run the tbcopycdr script with the -v option
+
*run the script again; the file ''tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz'' should have been removed from ''/lib/tb/toolpack/pkg/database_backups/''
tbcopycdr -v
+
 
+
If no error were seen on the shell, your configuration is correct. Also, you should have a hello_world.log file on the remote host.
+
 
+
= Configure Tmedia to send the files automatically =
+
  
 +
= Schedule the Script in a Cron Job =
 
*Create a new crontab on the [[Tmedia]]
 
*Create a new crontab on the [[Tmedia]]
 
  crontab -e
 
  crontab -e
Line 63: Line 97:
 
! width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | Frequency
 
! width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | Frequency
 
! width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | Cron Configuration
 
! width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | Cron Configuration
|-
 
| width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); text-align:center;" | Every 10 minutes
 
| width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | <code>*/10 * * * * /usr/bin/tbcopycdr</code>
 
 
|-
 
|-
 
| width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); text-align:center;" | hourly (top of the hour)
 
| width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); text-align:center;" | hourly (top of the hour)
| width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | <code>0 * * * * /usr/bin/tbcopycdr</code>
+
| width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | <code>0 * * * * /root/tbdbbackup</code>
 
|-
 
|-
 
| width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); text-align:center;" | daily (midnight)
 
| width="210" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); text-align:center;" | daily (midnight)
| width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | <code>0 0 * * * /usr/bin/tbcopycdr</code>
+
| width="350" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" | <code>0 0 * * * /root/tbdbbackup</code>
 
|}
 
|}

Revision as of 16:59, 11 January 2017

This procedure shows how to configure a periodic database backup using a bash script (tbdbbackup) executed from a Tmedia host unix cron jobs. Cron allows Linux and Unix users to run commands or scripts at a given date and time.

Contents

"tbdbbackup" Script

The tbdbbackup script support the following features:

  • Backup the TMG-CONTROL configuration database into a file
  • Backup file name formatted as tbdbbackup_<hostname>_toolpack_<version>_<date>_<time>.sql.gz
 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz
  • Keep N days of local backup files. Default is 7 days.
  • Backup files are accessible from the Web portal in Backups->Manual Database Backup
  • Optional copy to a remote server supporting SSH Key-Based Authentication
    • Copy the backup file on a remote server (using scp) - optional
    • Keep M days of backup files on the remote server (using ssh) - optional

Get the Script

On latest product version, the script should be available from the /usr/bin/. To validate if you have the script on your Tmedia:

  • Connect to the Tmedia using SSH as root
[root@TB014369 ~]# ls /usr/bin/tbdbbackup
ls: cannot access /usr/bin/tbdbbackup: No such file or directory

If the script is not available on your Tmedia:

  • get the script from

Configure the Script

1. Copy the script to /root Note: You must copy the script to another location to avoid loosing your configurations when upgrading your host (yum update).

 cp /usr/bin/tbdbbackup /root/tbdbbackup
 chmod +x /root/tbdbbackup

2. Edit the script file:

vim /root/tbdbbackup

3. Configure the following variables:

  • LOCAL_BACKUP_MAX_DAYS : Keep the local backup files for that number of days. Set to 7 days by default.
 # Add '#' in front of the line to disable the local backup file cleanup
 #LOCAL_BACKUP_MAX_DAYS="7"
  • REMOTE_BACKUP_MAX_DAYS : Keep the local backup files for that number of days. Set to 7 days by default.
 # Add '#' in front of the line to disable the remote backup file cleanup
 #LOCAL_BACKUP_MAX_DAYS="7"
  • REMOTE_HOST : Remote host name or Ip address.
 # Add '#' in front of the line to disable the remote copy
 #REMOTE_HOST = 10.10.10.10
  • REMOTE_PORT : Remote host name or Ip address
  • REMOTE_DIR : Remote host directory where the backup files will be copied
  • REMOTE_USER : Remote host user

Create an SSH Key

You must create an SSH key pair and copy it to the remote server, if the remote server copy is configured. This will enable the Tmedia to copy the backup files to the remote host without asking for a password:

  • Connect to the Tmedia using SSH as root
  • Create Tmedia public/private dsa key pair
ssh-keygen -t rsa

Note: Do not add a pass phrase, simply press enter until the key pair is generate.

  • Append the content of the public key to the remote host authorized keys

Tmedia public key location

/root/.ssh/id_rsa.pub

Remote Host authorized keys location

~/.ssh/authorized_keys

More details here: Password-less SSH

Test your Script

1.Manually run the script/;

/root/tbdbbackup

2.Check if the backup is locally created:

ls -alt /lib/tb/toolpack/pkg/database_backups/

3.If the remote copy is configured, make sure the file was copied on the remote server

4.Test "LOCAL_BACKUP_MAX_DAYS" (if configured):

  • run the script a few times to create multiple files
  • use "touch" to change the date of a file to a date older then "LOCAL_BACKUP_MAX_DAYS" days ago. For example, if "LOCAL_BACKUP_MAX_DAYS" is set to 15 days, set the date of the file as "16 days ago".
[root@TB014369]# ls -alt /lib/tb/toolpack/pkg/database_backups/
total 820
-rw-r--r--  1 root  root  206638 Jan 11 16:40 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
-rw-r--r--  1 root  root  206612 Jan 11 15:52 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz
[root@TB014369 database_backups]# touch -d "16 days ago" /lib/tb/toolpack/pkg/database_backups/tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
[root@TB014369]# ls -alt /lib/tb/toolpack/pkg/database_backups/
total 820
-rw-r--r--  1 root  root  206612 Jan 11 15:52 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_15h52m.sql.gz.
-rw-r--r--  1 root  root  206638 Dec 26 16:43 tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz
  • run the script again; the file tbdbbackup_TB014369_toolpack_2_8_2017_01_11_16h40m.sql.gz should have been removed from /lib/tb/toolpack/pkg/database_backups/

Schedule the Script in a Cron Job

  • Create a new crontab on the Tmedia
crontab -e
  • Add new line with the cron configuration

Example:

Frequency Cron Configuration
hourly (top of the hour) 0 * * * * /root/tbdbbackup
daily (midnight) 0 0 * * * /root/tbdbbackup
Personal tools