Useful Back-up Scripts
You find yourself maintaining a server, or servers, running MariaDB/MySQL and/or OpenLDAP, and you think it would be a good idea to create clean back-ups on a daily basis so you can rebuild things should something go horribly wrong. Here are some useful script ideas to help you. As with my other guides, all of my scripts live in /root/scripts.
RPM
It is usually a good idea to create a list of packages installed on your RHEL/CentOS/Rocky Linux server to facilitate a speedy rebuild or relocation. This script helps with that:
#!/bin/bash # # Script to export short package names for rebuilding # a new server via Yum. # Use "yum -t `cat rpm-package-list`" to build with the output # A path, a path # BACKUP=/var/backup/rpm # Check directory exist, if not, create them. # if [ ! -d $BACKUP ]; then mkdir -p $BACKUP fi # Output RPMs # rpm -qa --qf %{NAME}\ > $BACKUP/rpm-yum-list rpm -qa | sort > $BACKUP/rpm-package-list # Clean up # unset FILE exit 0 |
This script is called rpm-list and can be called from /etc/cron.daily via a symlink. The files created in /var/backup will be picked-up by your rsync back-up script, if you have followed my other guide: How to back-up Linux with Rsync.
If you build a basic install server from PXE, you can use the output of this script to effectively re-install/clone all of the packages in use on an existing system. With the required repositories added, simply copy the rpm-yum-list file to your new system and enter (as root): yum -t `cat rpm-package-list` on the command-line. Yum will helpfully install all of the packages listed in the file that are not already present.
MariaDB / MySQL
Database servers can be very scary, so it is a good idea to back-up as much as you can should anything go horribly wrong. This script can be used on any system running MySQL or MariaDB.
#!/bin/bash # # Script to back-up MariaDB # A path, a path. # BACKUP=/var/backup/mariadb # Check directory exist, if not, create them. # if [ ! -d $BACKUP ]; then mkdir -p $BACKUP fi # Dump full database # mysqldump --lock-tables --all-databases --events > $BACKUP/mariadb-full-backup.sql # Dump MySQL database # mysqldump --lock-tables mysql > $BACKUP/mysql-database.sql # Dump furniture database # mysqldump --lock-tables furniture > $BACKUP/furniture-database.sql # Dump tracking database # mysqldump --lock-tables tracking > $BACKUP/tracking-database.sql # Clean up # unset BACKUP exit 0 |
The above example script, called mariadb-backup initially creates one large database dump file of everything. This is aimed at total system failure and a need to start again from scratch. The mysql-database output contains the database names, users, and permissions. The next two are examples of backing up a "furniture" database and a "tracking" database to separate files to facilitate the restoration of a single database.
The script can only work correctly if you create a .my.cnf file in /root that contains the username and password used to access the top-level of the database. Here is an example:
[mysql] user=root password=mysqlpassphrase [mysqladmin] user=root password=mysqlpassphrase [mysqlimport] user=root password=mysqlpassphrase [mysqldump] user=root password=mysqlpassphrase [mysqlcheck] user=root password=mysqlpassphrase [mysql_upgrade] user=root password=mysqlpassphrase |
The script is best called from an rsync job control script to ensure the back-up is fresh. Here is an example of calling the script remotely via SSH (assuming key-based connections not prompting for passwords):
#--------------------------------------------------------------- # Run MySQL back-up script ssh -l root $SERVER '/root/scripts/mariadb-backup' #--------------------------------------------------------------- |
In the above example $SERVER would be defined in the script as the FQDN of the server being backed-up. The script is being called on the remote database server ready for the remote back-up server to rsync that day's changed data.
OpenLDAP
If your organisation is running one or more OpenLDAP servers for authentication, email address look-ups, et al, it is a good idea to perform regular back-ups in case something goes horribly wrong. The following script ldap-backup helpfully dumps the entire LDAP directory to an LDIF format file:
#!/bin/bash # # Script to back-up LDAP # A path, a path. # DIR=/var/backup/ldap # Check and create back-up dir if not present # if [ ! -d $DIR ]; then mkdir $DIR fi # Back-up LDAP database # slapcat -l $DIR/ldap-backup.ldif # Done # exit 0 |
The script is best called from an rsync job control script to ensure the back-up is fresh. Here is an example of calling the script remotely via SSH (assuming key-based connections not prompting for passwords):
#--------------------------------------------------------------- # Run MySQL back-up script ssh -l root $SERVER '/root/scripts/ldap-backup' #--------------------------------------------------------------- |
In the above example $SERVER would be defined in the script as the FQDN of the server being backed-up. The script is being called on the remote database server ready for the remote back-up server to rsync that day's changed data.
Page updated: 28th August 2021