Automating the Restore of GLPI Backups

This script will automatically Restore the latest backups of GLPI onto the server that it is run on. This can be the production server if there is an issue or used on another server to automatically test GLPI backups and essentially create a GLPI Cluster or automatic backups server instance if you like.

The process is the same for both it just depends where the script is run.

There will be a number of places where paths and passwords need to be changed which are marked with # Instruction in the script below:

I called the script but it can be called anything you like:

#! /bin/bash
# Automates GLPI backup Restore

# extract mysql dump to /backups/mysql/uncompressed
# Delete any old extracted DB files

echo "deleting any old mysql dumps." # Delete old glpi.sql

rm /backups/mysql/glpi.sql #CHANGE PATH AS NEEDED

echo "DONE!"

new=$(ls -t1 /backups/mysql/ | head -1) # find newest file and assign variable(new):

echo "the newest mysqlsump file is: $new"

# gunzip it to /backups/mysql/glpi.sql

echo "extracting $new now!"

gunzip -c /backups/mysql/$new > /backups/mysql/glpi.sql #CHANGE PATH AS NEEDED

echo "file extracted successfully"

sleep 5

# Print start status message.
echo "restoring glpi database"

# import dump into db

mysql -u root -pPASSWORD glpi < /backups/mysql/glpi.sql #CHANGE PASSWORD to actual Password

# Print end status message.

echo "database restore finished!"

sleep 5

# Remove old folder and add datestamp to it

echo "deleting any old glpi folders."

rm -R /backups/www/var #CHANGE AS NEEDED

# find newest tar.gz and assign variable untar to it

untar=$(ls -t1 /backups/www/ | head -1) #CHANGE AS NEEDED

echo "the most recent backup is $untar"

# untar www folder

tar -xf /backups/www/$untar -C /backups/www/ #CHANGE AS NEEDED

# move old glpi folder to glpi_old

mv /var/www/html/glpi /var/www/html/glpi_old 

# copy folder to /var/www/html/

echo "copying the glpi folder to the web folder"

cp -r /backups/www/var/www/html/glpi /var/www/html/ #CHANGE AS NEEDED

echo "glpi folder copied successfully"

# change permissions on folder

echo "changing web folder permissions"

sudo chown -R www-data:www-data /var/www/html/glpi

echo "DONE!"

sudo service apache2 restart

echo "GLPI has now been restored to its latest backup."

The script can be run by using the following command:

bash /path/to/

This will restore the backups to the state of the newest backup automatically.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top