> For the complete documentation index, see [llms.txt](https://prosbcdocs.telcobridges.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prosbcdocs.telcobridges.com/tmedia-documentation/server-and-os-administration/scheduled-database-backup.md).

# Scheduled Database Backup

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

## Overview

The *tbdbbackup* script supports the following features:

* Backup the configuration database into a backup 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 in */lib/tb/toolpack/pkg/database\_backups*. The default is 7 days.
* Backup files are listed on the Web portal in Backups->Manual Database Backup
* Optional copy to a remote server supporting [SSH Key-Based Authentication](https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server)
  * Copy the backup file on a remote server (using scp) - optional
  * Keep M days of backup files on the remote server (using ssh) - optional

On the 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 \~]# which tbdbbackup /usr/bin/tbdbbackup

## Configuration

The script is configured with **user variables**.

* *CONFIG\_FILE*: Configuration file name and location. By default, it is set to /root/tbdbbackup.cfg. CONFIG\_FILE=/root/tbdbbackup.cfg

*tbdbbackup.cfg* file example: LOCAL\_BACKUP\_MAX\_DAYS=1 REMOTE\_BACKUP\_MAX\_DAYS=1 REMOTE\_HOST=192.168.101.170 REMOTE\_PORT=22 REMOTE\_DIR=/root/backup\_test REMOTE\_USER=root

The configuration file can redefine any of the following user variables. To see the default variables: head -n 50 /usr/bin/tbdbbackup

* *LOCAL\_DIR*: Set this variable to change the local directory. By default the script uses /lib/tb/toolpack/pkg/database\_backups. For example, to change the local backup dir to /home/tbdbbackup: LOCAL\_DIR=/home/tbdbbackup
* *LOCAL\_BACKUP\_MAX\_DAYS*: Maximum number of days to keep local backup files. Set to 7 days by default.

1. Assign no value to disable the local cleanup LOCAL\_BACKUP\_MAX\_DAYS=7

* *REMOTE\_BACKUP\_MAX\_DAYS*: Maximum number of days to keep remote backup files (if REMOTE\_HOST is set). Set to 7 days by default.

1. Assign no value to disable the remote cleanup REMOTE\_BACKUP\_MAX\_DAYS=7

* *REMOTE\_HOST*: Remote host name or IP address. Remote host copy is disabled by default.

1. Remote host IP or hostname for the remote copy REMOTE\_HOST=10.10.10.10 By default, no value is assigned. This disables the remote copy. REMOTE\_HOST=

* *REMOTE\_PORT*: Remote host ssh port. REMOTE\_PORT=22
* *REMOTE\_DIR*: Remote directory to which the backup files are copied. (/root/backup by default) REMOTE\_DIR=/root/backup
* *REMOTE\_USER*: Remote host login user. REMOTE\_USER=root
* *CONNECT\_TIMEOUT*: *scp* connection timeout CONNECT\_TIMEOUT=10
* *NAME\_PREFIX*: Change this variable to customize the backup file's prefix file name

1. By default take the Tmedia hostname for the backup 'hostname'. NAME\_PREFIX=tbdbbackup\_$(hostname)

## Remote Server Copy

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

* Connect to 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 generated.
* 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](https://github.com/telcobridges-main/tmedia-wiki/tree/main/tmedia/operations/password-less-ssh/README.md)

## Troubleshooting

1.Manually run the script /usr/bin/tbdbbackup

2.Check if the backup is created locally: ls -alt /lib/tb/toolpack/pkg/database\_backups/

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

4.To test the "LOCAL\_BACKUP\_MAX\_DAYS" (if configured), you can perform the following:

* 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/*

5. Look at the script log file in /lib/tb/toolpack/pkg/database\_backups/tbdbbackup.log
6. cat /lib/tb/toolpack/pkg/database\_backups/tbdbbackup.log Start a scheduled backup: Thu Jan 12 11:53:12 EST 2017
   * mysqldump -u tbdb -ptbdbpw --opt toolpack\_2\_10 > tbdbbackup\_TB014369\_toolpack\_2\_10\_2017\_01\_12\_11h53m.sql => OK
   * gzip -f tbdbbackup\_TB014369\_toolpack\_2\_10\_2017\_01\_12\_11h53m.sql => OK
   * Delete local files older then 7 days: Nothing to delete Stop scheduled backup: Thu Jan 12 11:53:21 EST 2017

## Schedule the Script

Modify the system crontab to execute the backup on a regular basis.

* Create or modify the crontab on the Tmedia crontab -e
* Add new line with a [cron configuration](http://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/#)

| Frequency                | Cron Configuration                               |
| ------------------------ | ------------------------------------------------ |
| hourly (top of the hour) | `0 * * * * /usr/bin/tbdbbackup > /dev/null 2>&1` |
| daily (midnight)         | `0 0 * * * /usr/bin/tbdbbackup > /dev/null 2>&1` |

Example: 0 0 \* \* \* /usr/bin/tbdbbackup > /dev/null 2>&1

## Sending Backup Folder to Google Drive

You can upload the backup folder to Google Drive.

1. log in to the CLI using SSH and download the gdrive with the following command: sudo wget -O drive <https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg>
2. Move GDrive to /usr/sbin/drive folder by entering the following command: sudo mv drive /usr/sbin/drive
3. Set permissions: sudo chmod 755 /usr/sbin/drive
4. run the drive command to start the authentication process. You will receive a link like the following; <https://accounts.google.com/o/oauth2/auth?client\\_id=12345678901203-7n0vf5serdar7on>...
5. Copy and paste the link in the browser. Accept the permission and you will get a verification code. Copy the verification and paste it back into your shell

   Enter verification code: 4/9gKYAFAJ326XIP6JJHAEhs342t35LPiA5QGW0935... You have installed Google Drive and Connected to your Google Drive account.
6. Create a bash script to upload files to Google Drive. Go to your Google Drive and create a folder. Go to the folder. Now, look at the URL. At the end of the URL, you can see the folder’s path, as shown in the following screenshot:

![Google Drive 1](https://docs.telcobridges.com/w/images/9/9c/Google_Drive_1.png)

0B02roDDQi5I8SUpLZ29RZ1A2YWc is the Mix folder’s path. Copy this path.

7. Download the following file and put any folder on your server. The following file compresses the /home/tbdbbackup folder and uploads it to Google Drive. You can find other examples on the internet.

| Google Drive                                                                       |
| ---------------------------------------------------------------------------------- |
| \*[google\_drive.sh](https://docs.telcobridges.com/w/images/3/3b/Google_drive.zip) |

8. After uploading the google\_drive.sh file you need to add cron job to run after tbdbbackup and upload the generated backup to google drive.

## Other Cloud Options

* You can upload your backups to **AWS S3**. Please check the link below; [Uploading Backupts to AWS S3 Instruction](https://telcobridges.com/sbc/products/freesbc-prosbclab/)
* You can upload your backups to **Google Cloud Storage**. Please check the following link below;

[Uploading Backups to Google Cloud Bucket Instruction](https://telcobridges.com/sbc/products/freesbc-prosbclab/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://prosbcdocs.telcobridges.com/tmedia-documentation/server-and-os-administration/scheduled-database-backup.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
