Per eseguire un rapido backup di tutto il sito:

   1 #!/bin/bash
   2 # Licence: GPL2 or greater
   3 # Copyright: risca (http://risca.eu)
   4 # Version: 0.0.1
   5 
   6 # Welcome to the upgrade script for mediawiki.
   7 # Do it fast, do it better ;-)
   8 
   9 # This script performes a backup or a set-up according to a backup. The file
  10 # that are touched are the following:
  11 #  *  mysql database;
  12 #  *  files into the mediawiki'sdirectory;
  13 # And you get back:
  14 #  *  a tar file containing both the files and the database;
  15 
  16 ################################
  17 # Main function                #
  18 ################################
  19 
  20 function select_mode {
  21   case "$@" in
  22     b)   
  23         backup fast
  24     ;;
  25     ba)   
  26         backup all
  27     ;;
  28     r)
  29         restore fast
  30     ;;
  31     ra)
  32         restore all
  33     ;;
  34     *) echo "Usuage: mediawiki-update.sh b=BACKUP|ba=BACKUP-ALL|r=RESTORE|ra=RESTORE-ALL"
  35     ;;
  36   esac
  37 }
  38 
  39 function backup {
  40   test_env
  41   ask_info backup
  42   set_env
  43   get_wiki_config
  44   do_mysql backup
  45   files backup
  46   archive create
  47   cleanall
  48 }
  49 
  50 function restore {
  51   test_env
  52   ask_info restore
  53   set_env
  54   archive extract
  55   files export
  56   get_wiki_config
  57   do_mysql restore
  58   cleanall
  59 }
  60 
  61 ################################
  62 # Utility and basic functions  #
  63 ################################
  64 
  65 function ask_info {
  66   case $1 in
  67     backup)
  68       echo -e "What is your wiki path? (eg. /var/www/mysite.eu/mediawiki/w)"
  69       read -p " > "  WPATH
  70       echo -e "On which file do you want to save everything? (eg. /var/www/backup)"
  71       read -p " > " BPATH
  72       echo 
  73       echo -e "Set the archive name: (eg. mysite)"
  74       read -p " > " ARCH
  75     ;;
  76     restore)
  77       echo -e "Which file do you want to unzip? (eg. /var/www/mybackup.tar.gz)"
  78       read -p " > " ARCH
  79       echo -e "Where will be your web root directory? (eg. /var/www/mysite.com/wiki)"
  80       read -p " > " WPATH
  81       BPATH=$(dirname ${ARCH})
  82       ARCH=$(basename ${ARCH})
  83     ;;
  84   esac
  85 }
  86 
  87 function confirm ()
  88 {
  89   for II in "$@"
  90   do
  91     VALUE=${!II}
  92     echo "$II = $VALUE"
  93   done
  94   read -p "Is that correct? (y/n) ->  " yn
  95   if [ $yn == n ]; then
  96     exit
  97   fi
  98 }
  99 
 100 function test_env {
 101   if [ $(dpkg -l | grep mysql-client | grep -c ^ii) -eq 1 ]
 102     then
 103       echo 'The package mysql-client is missing, please install it!'
 104   fi
 105 }
 106 
 107 function get_wiki_config {
 108   DB_NAME=$(grep ^\$wgDBname ${WPATH}/LocalSettings.php | grep -o -P '(?<=").*(?=")')
 109   DB_USER=$(grep ^\$wgDBuser ${WPATH}/LocalSettings.php | grep -o -P '(?<=").*(?=")')
 110   DB_PW=$(grep ^\$wgDBpassword ${WPATH}/LocalSettings.php | grep -o -P '(?<=").*(?=")')
 111   confirm "DB_NAME" "DB_USER" "DB_PW"
 112 }
 113 
 114 function set_env () {
 115   mkdir -p ${BPATH}/tmp
 116   if [ "$?" -ne 0 ]; then echo "command failedi, cannot create ${BPATH}/tmp"; exit 1; fi
 117   TMP=${BPATH}/tmp
 118   timestamp=$(date +%Y%m%d-%H%M%S)
 119 }
 120 
 121 function do_mysql {
 122   if [ $1 == "backup" ]; then
 123     mysqldump --user=$DB_USER --password=$DB_PW --databases $DB_NAME > $TMP/${DB_NAME}${timestamp}.sql
 124   fi
 125   if [ $1 == "restore" ]; then
 126     testMysql
 127     mysql --user=$DB_USER --password=$DB_PW --databases $DB_NAME < $TMP/${DB_NAME}
 128   fi
 129 }
 130 
 131 function testMysql {
 132   UE=$(echo "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'pwiki')" | mysql -u root -p -s -N)
 133   if [ $UE -eq "1" ]; then
 134     echo "User already setted, going on..."
 135   fi
 136 }
 137 
 138 function files {
 139   case $1 in
 140     backup)
 141       # Create backup of config and other datas
 142       #FILE2B=${WPATH}'/LocalSettings.php '${WPATH}'/extensions '${WPATH}'/images'
 143       #echo $FILE2B
 144       tar -pcvf $TMP/${ARCH}-${timestamp}.tar -C $WPATH .
 145     ;;
 146     export)
 147       tar -pxvf $TMP/*.tar -C $WPATH
 148     ;;
 149   esac
 150 }
 151 
 152 function archive {
 153   # Create the tar archive with all of the previous data
 154   if [ $1 == "create" ]; then
 155     tar -pcavf $BPATH/$ARCH.tar.gz -C $TMP .
 156   elif [ $1 == "extract"]; then
 157     tar -apxf $BPATH/$ARCH -C $TMP
 158   fi
 159   echo 'archive done: $BPATH/$ARCH'
 160 }
 161 
 162 function cleanall {
 163 # delete unusefull TMP files
 164   echo "Can I delete $TMP? BE CAREFUL!!! (y/n)"
 165   read -p " > " REMOVE
 166   if [ $REMOVE == "y" ]; then
 167     rm -r $TMP
 168   fi
 169 }
 170 
 171 echo -e "\n - - - - - - - - - - - - - -\n"
 172 select_mode $@
 173 echo " "

MyWiki: Mediawiki/BackupUpgrade (last edited 2015-02-15 10:51:45 by risca)