You are here

A Perl script to auto Update (and backup) Drupal files

Last modified: 
Sunday, December 14, 2014 - 16:46


This script is prompt driven and will do the following:
- Check the Drupal site for its current release version
- Display your Drupal version
 
Give you the following options from the command prompt:
- Download the latest Drupal files
- Back up your current Drupal files
- Back up your current Drupal SQL database
- Extract the latest downloaded files to your site
 
** Once the script has completed, you need to run Drupals update.php
This script only backs up your files and dbase along with downloading and installing Drupal files BUT it does not run any SQL-database updates, you NEED to do that from your Drupal update.php file

This script is intended to run on your server via SSH.
Upload the script file to a directory on your server where it can not be accessed from a web browser.

Download the following file (right click, Save link As..) drupal_file_update.pl.txt and rename it to drupal_file_update.pl
Make 'drupal_file_update.pl' executable
Terminal
name@machine:/$ sudo chmod +x /path/to/drupal_file_update.pl

At the top of the script are a handfull of settings which you need to edit before you save the script to your server.

# FULL Drupal ROOT Path with-out ending slash
# You should only need to change the end part of this string, /drupal/site, to reflect your site structure
my $drupalRootPath = "/home/$username/public_html/drupal/site";

# FULL Drupal ROOT URL with-out ending slash
my $drupalRootURL = 'http://localhost/drupal/site';

# Full DIR path to save TAR backups
# This path will also be used to temperaraly save the newly downloaded Drupal Update .tar file
# Dir needs to be writable
my $backupSavePath= "$drupalRootPath/backups";

# File(s) you want/need to preserve after your update is complete
# File(s) here will be copied to a temp dir *before* the update starts
#	and after the update is done, they will be copied back.
# Ive added some code to my .htaccess and want to preserve it, hence the option.
my @preserveFile = ('.htaccess',
				'robots.txt',
				);
			
# Files and Dirs that you do not want to backup
# Relative path to Drupal root for files and dirs that will NOT be backed up
# Being this script is only for Updates, i do not see the need to waist space and back up everything,
# so i exclude images and since i placed my 'backup' folder in-side my Drupal install, i opmit the backup dir also
my @nobackup = ('backups',
				'sites/default/files/*.jpg', 
				'sites/default/files/*.JPG', 
				'sites/default/files/images',
				'sites/default/files/myuploads/', 
				'sites/default/files/pictures',
				'sites/default/files/brilliant_gallery_temp', 
				);

 

Step by step with screen shots:
When the script is first run

 

Selecting 'yes' to start version check:
Here the script will download the Drupal Project page, phase through it and grab the latest version number.
After that it will scan your Drupal CHANGE.txt file for the last version change and display the following questions:

Your Drupal Ver = xxx
Latest Drupal Ver = xxx
-------------------------------------
1) Start Script
2) Display Backup Dir
3) Exit

Selecting 'start update' will run a couple checks first.
- Drupal Root path definded in the script is correct
- Backup dir exists, if not it will create it
- If the script can write to the backup dir, if not it will display an Error and exit the script.
Finaly you will be given the option to donwload the latest Drupal file.


If you select yes, 'wget' will download and save the latest Drupal .tar file to your 'backup' dir and then ask you if you want to backup your current Drupal files.


By selecting yes to backing up your current files the script will run 'tar' with the definded --excluded files you set in the script.
Being this file backup is only intented to protect your Drupal files (and not a Full backup) there is an array which you can add file(s) and dir(s) so you do not create a huge backup file waisting space, time and system resources.
Once complete you will be asked if you want to back up your MySQL (this script will only back up MySQL).

 

The script will read your /sites/default/settings.php file to get your MySQL info and then run a mysqldump.
After its done you will be asked if you want to extract the Drupal download into your current Drupal Install.

 

First this the script will do is make a copy of any files you have in the '@preserveFile' array.
Ive made some changes to my .htaccess which i do not want the new Drupal files to over write, so i added .htaccess to the array.
After the script makes a copy (it will place the copied files in your backup dir), it will then un-tar the Drupal download and extract it to your Drupal install.
Once the extraction is complete, the copied files will be copied back to your main Drupal dir, and the old copied in the backup dir will be delete.


Now, you must go to your Drupal update.php file and run that so it can run the necessary SQL updates.

 

Now after all that, i discovered you can do most of this with the Drupal Drush Module (i have not installed it, so can not offer any insight (but thought i would mention it)).

In time i plan on making a Drupal Module that will be a simple click and go that will to all of this from your Drupal's Admin CP

Disclaimer:
The information found here has been created to help people.
The steps given have worked for me with out a problem, but i will not be held responsible for how you use the information or what the out come of the use does for you.
If there is something that could cause problems or there is a risk in doing it, i have noted that - and done so for good reason. Google and Yahoo are great resources, so please use them also.
Mobile QR Barcodes: 
Save Article as PDF
Current URL Address


Mobile: File Download for - drupal_file_update.pl.txt

Tags: 
File Downloads: 

Add new comment