Ylläpidätkö WordPress-sivustoja? Oletko kyllästynyt toistamaan komentoja wget wordpress.org/latest.tar.gz -> tar -zxf latest.org -> cp/mv wordpress/* /var/www/ ??? Nyt ongelmiisi on ratkaisu, autopress!
Halusin tutustua hieman bash-skriptauksen maailmaan ja olin kyllästynyt toistelemaan noita komentoja, joten tein pienen skriptinpätkän tekemään asian puolestani. Julkaisen sen tässä alla, tee sillä mitä lystäät! Koodi saattaa olla hassusti rakennettua, typerää ja tehotonta – mutta se ajaa jotenkuten asiansa.
Pointtina koko hommassa on tietysti se että skriptin voi ajastaa ajettavaksi vaikkapa parin tunnin välein eikä päivityksistä tarvitse enää huolehtia, joskin asiassa on kaksi aika isoa muttaa:
- WordPressin päivittäminen uusimpaan versioonsa saattaa rikkoa sivustosi, varsinkin jos käytössä on erikoisempia teemoja tai lisäosia, joten WordPressin päivittäminen täysin automaattisesti on oikeastaan aika huono idea. Tosin jos ylläpitämäsi sivusto on yhtään henkilökohtaista webbisivua tärkeämpi, on sinulla varmastikin testiympäristö erikseen ja tätä voisi käyttää vaikkapa vain siellä.
- WordPressin päivittäminen saattaa vaatia lähtöversiosta ja uudesta versiosta riippuen myös tietokannan päivityksen (joka tapahtuu kirjautumalla WordPressin Dashboardiin admin-tunnuksin), eikä tämä skripi tee sitä. Eli sinun täytyy joka tapauksessa kirjautua WordPressiin päivityksen jälkeen mahdollisimman nopeasti.
Jos haluat kuitenkin ajastaa skriptin suoritettavaksi vaikka tunnin välein, muokkaa crontabiasi komennolla crontab -e ja lisää sinne rivi: “*/60 * * * * /hakemisto/jossa/skripit/on/autopress-0.1″. Lisää ajastamisesta cronilla esimerkiksi täältä.
Copypasteta tuosta alempaa tai lataa tästä: autopress-0.1
#!/bin/bash
############################################
######### AUTOPRESS 0.1 ####################
############################################
#
# Autopress is a bash script to automatically update WordPress.
# For more information check http://jannewaren.fi
#
# Author: Janne Warén (janne.waren@iki.fi)
# License: Public domain
#
# How to use it?
# Just configure the script starting from line X and run it.
# Note: Make sure you have write permission to $wordpress_location
# You still need to do the database update manually by logging in to WP
############################################
######### VERSION HISTORY AND TODO #########
############################################
#
# Version 0.1 Initial release
# - basic functionality
# - check for updates, update wordpress files
# - logging
# - silent mode
#
# TODO:
# - Backup before updating
# - Check only -mode (with e-mail notifications)
# - WordPress database update
# - Plugin updates
############################################
######### CONFIGURE HERE ###################
############################################
# Where is the script located?
script_location="/home/user/bin/"
# Where is your WordPress installation located?
wordpress_location="/var/www/"
# Where to check for a new update?
wordpress_url="http://wordpress.org/latest.tar.gz"
# Where to write a log?
# Note: if running with cron, specify full path!
logfile="/home/user/bin/autopress.log"
# Are we running in silent mode? (0=no, 1=yes)
silent="0"
# Only check for a new version? (and notify via e-mail)
# checkmode="1"
# email="username@example.com"
# Should we do a file backup? (0=no, 1=yes)
# backup_files="1"
# Should we do a database backup? (0=no, 1=yes)
# backup_database="1"
# MySQL username and password (only needed if backup_database=1)
# mysql_username=""
# mysql_password=""
############################################
######### THE ACTUAL SCRIPT ################
############################################
# Get the latest WordPress versions filename from WordPress.org
function latest_file_name {
local latest=$(
curl -s --head $wordpress_url |
tr -d '\r' |
awk -F '=' '/^Content-Disposition/ {print $2}'
)
echo $latest
}
# Compare the latest version from wordpress.org and the latest version in your folder and decide if we need to update
function version_comparison {
output "Checking for a new WordPress version.."
local remote_file=$(latest_file_name)
local local_file=$(current_version)
if [ $remote_file = $local_file ]; then
output "You already have the latest version $remote_file, no need to update, quitting!"
exit
fi
local newer=$(
echo -e "$remote_file\\n$local_file" |
sort -V |
tail -1
)
if [ $newer = $local_file ]; then
output "Either you got beta versions or something went terribly wrong.. Quitting!"
elif [ $newer = $remote_file ]; then
output "Found an update!"
updating_version="$newer"
update_wordpress
else
exitcode="Something went wrong with version comparison"
quit
fi
}
# Determine which of the WordPress tar.gz files is newest version in current folder
function current_version {
# First check if any matching file exist
local count_files=$(
ls $script_location |
grep -c wordpress-
)
if [ $count_files = 0 ]; then
# If files do not exist, skip the rest of version_comparison and just update
output "No previous version detected, will update/install WordPress!"
updating_version="$newer"
update_wordpress
else
# If files exists, return the newest version filename
local file_name=$(
ls $script_location |
grep wordpress- |
sort -V |
tail -1
)
echo $file_name
fi
}
# So we have decided to update, here we go!
function update_wordpress {
if [[ -e "$script_location""latest.tar.gz" ]]; then
output "Removing old latest.tar.gz file"
rm "$script_location"latest.tar.gz
fi
output "Downloading $wordpress_url - this may take a while"
wget -q -P "$script_location" $wordpress_url
tar -zxf "$script_location"latest.tar.gz -C "$script_location"
cp -R "$script_location"wordpress/* $wordpress_location
mv "$script_location"latest.tar.gz "$script_location""$updating_version"
rm -fr "$script_location"wordpress/
output "Done! Your WordPress has been updated, please log in to update your database!!"
}
# Needed just in case we need to quit, pretty much pointless at the moment though
function quit {
output "Self destruction in 1...2...3...DONE!"
output "Quit reason: $exitcode"
exit
}
# Write a log and echo (if not in silent mode)
function output() {
if [ $silent = 0 ]; then
echo "$1"
fi
local time=$(date +'%Y-%m-%d %H:%M:%S')
echo "$time $1" >> $logfile
}
function initialize {
if [[ ! -e $logfile ]]; then
touch $logfile
output "Created log file: $logfile"
fi
}
function main {
initialize
version_comparison
}
main















