Skip to content

Instantly share code, notes, and snippets.

@CyberPunkCodes
Last active February 14, 2022 19:06
Show Gist options
  • Save CyberPunkCodes/56be65f64e20a8d04b702e1aa75eb5da to your computer and use it in GitHub Desktop.
Save CyberPunkCodes/56be65f64e20a8d04b702e1aa75eb5da to your computer and use it in GitHub Desktop.
XAMPP Backer Upper
#!/bin/bash
echo "Running XAMPP Backer Upper"
# The full path to the XAMPP root installation. No trailing slash.
XAMPP_ROOT_LOCATION="" # ie: /Applications/XAMPP
BACKUP_ROOT_LOCATION="" # ie: /Users/username/Desktop
BACKUP_PREFIX="backup_"
BACKUP_TIME=`date +"%m-%d-%Y_%H-%M-%S"`
BACKUP_FOLDER="${BACKUP_PREFIX}${BACKUP_TIME}"
BACKUP_LOCATION="${BACKUP_ROOT_LOCATION}/${BACKUP_FOLDER}"
if [ -z "${XAMPP_ROOT_LOCATION// }" ]
then
echo "XAMPP_ROOT_LOCATION Not configured!"
exit;
fi
if [ -z "${BACKUP_ROOT_LOCATION// }" ]
then
echo "BACKUP_ROOT_LOCATION Not configured!"
exit;
fi
if [ ! -d "$XAMPP_ROOT_LOCATION" ]
then
echo "XAMPP_ROOT_LOCATION Does not exist!"
exit;
fi
if [ ! -d "$BACKUP_ROOT_LOCATION" ]
then
echo "BACKUP_ROOT_LOCATION Does not exist!"
exit;
fi
echo "XAMPP Location: ${XAMPP_ROOT_LOCATION}"
echo "Backup Location: ${BACKUP_LOCATION}"
echo "All checks passed!"
# Create the backup folder
mkdir -p $BACKUP_LOCATION
echo "Copying config files..."
# Copy config files we don't want to lose
mkdir -p ${BACKUP_LOCATION}/xamppfiles/etc
cp ${XAMPP_ROOT_LOCATION}/xamppfiles/etc/php.ini ${BACKUP_LOCATION}/xamppfiles/etc
cp ${XAMPP_ROOT_LOCATION}/xamppfiles/etc/httpd.conf ${BACKUP_LOCATION}/xamppfiles/etc
cp ${XAMPP_ROOT_LOCATION}/xamppfiles/etc/my.cnf ${BACKUP_LOCATION}/xamppfiles/etc
mkdir -p ${BACKUP_LOCATION}/xamppfiles/etc/extra
cp ${XAMPP_ROOT_LOCATION}/xamppfiles/etc/extra/httpd-vhosts.conf ${BACKUP_LOCATION}/xamppfiles/etc/extra
mkdir -p ${BACKUP_LOCATION}/xamppfiles/phpmyadmin
cp ${XAMPP_ROOT_LOCATION}/xamppfiles/phpmyadmin/config.inc.php ${BACKUP_LOCATION}/xamppfiles/phpmyadmin
echo "Config files backed up!"
echo "Compressing htdocs..."
cd ${XAMPP_ROOT_LOCATION}/xamppfiles/
tar -pczf ${BACKUP_TIME}.tar.gz htdocs
mv ${BACKUP_TIME}.tar.gz ${BACKUP_LOCATION}/xamppfiles/htdocs.tar.gz
echo "Backup complete!"
@CyberPunkCodes
Copy link
Author

XAMPP Backer Upper

This is a script to help backup important XAMPP files. I got tired of manually backing up my XAMPP files every time I wanted to upgrade my XAMPP. This last time, I forgot my extra vhosts config. Not the end of the world, but that kind of sucks..

This is mostly for myself in the future, but other XAMPP users might find it helpful.

This is not for Winblows users. Get a real OS.

Warning

This does not back up your mysql databases! If you want to save some of your databases, create a directory somewhere (ie: desktop) named databases. Go into phpmyadmin and export what you need, saving them to your "databases" folder. Then after you have ran this script, copy that "databases" directory into the backup directory that was created. Just so you have them all in the same place. Most of my projects use migrations, so this isn't a big deal for me. I also haven't had much luck of my databases actually working after an upgrade anyways (mysql/inno db). You could also write or use a 3rd party script to back up all of your databases.

My process

Every time I upgrade XAMPP, I nuke it. I grab what I need to keep (sites/projects in htdocs and some config files). Once backed up, I uninstall XAMPP with their uninstaller. I then physically delete what is left of XAMPP (ie: from my "Applications" dir (mac)). I really like a whole new fresh start. Nothing lingering around to bite my rear later. I thought "man, this sucks nuking everything", so I tried to do an upgrade leaving the dangling XAMPP files (what the uninstaller left behind) and I don't feel like the installer installed everything into htdocs because I already had stuff in there. It's missing applications.html somehow. I want the latest version of the dashboard if it's changed. Not that I use them lol, but I want everything it's supposed to have. However, it did change user/group for EVERYTHING back to root/admin! Thanks xampp! So, I nuke it all and move my projects back in to be 100% sure it's a clean slate.

After a new XAMPP install, I take ownership of htdocs and everything in it (the few files/folder that XAMPP puts in there). I change the user from "root" to my username (via chown). I also change the group (chgrp) to "admin", which is my user's group.

How to know what user or group to use? In terminal, create a file and a directory. Run mkdir test and touch test.txt and run ls -l or something similar that shows the owner and group. That is the user and group that your htdocs and all files in it should belong to.

Lets assume your username is bob and group is admin:

cd /Applications/XAMPP/xamppfiles
chown -R bob htdocs
chgrp -R admin htdocs

If you notice the files you backed up (your websites/projects in htdocs) are out of wack, run it again on htdocs or each directory individually to fix them. Just keep your backup tar file until you are totally done. Maybe even a few days or weeks just in case you notice something later.

Usage

Save this file somewhere on your computer. Open the file in your editor and configure XAMPP_ROOT_LOCATION and BACKUP_ROOT_LOCATION. These must be full exact paths. There are examples next to them.

Then run it:

sh xbak.sh

You could even create an alias, like backup.xampp or xampp.backup.

It will copy specific config files so you can re-configure your new xampp install with the same values. I don't recommend replacing the files because new versions may have differently structured config files. Yes, it probably would work. But after a few versions, things change. If you have kept the same php.ini file since version 7.2, you would be replacing the config file for 8.1 with yours from 7.2 lol.

It will also tar the htdocs (keeping file permissions) so you don't lose any of your sites/files. You can extract it where it is, and it will extract in the same directory. It will not overwrite your htdocs unless you moved the tar back into your xamppfiles directory. Normally I don't need ALL of my old sites back. So I just run mv commands and grab just what I need. Maybe you just need to look at an old file for reference and don't need to move it back into your htdocs.

Config Files

Below are the config files that are backed up, and the common changes I make each time (if any).

etc/httpd.conf

Search for <IfModule unixd_module> and you will see it sets the user and group for the web server to run under. Change it to the same user/group as you set the files to above. Using bob as the username and admin as the group, example:

User bob
Group admin

Also, near the bottom, uncomment Include etc/extra/httpd-vhosts.conf. You put all of your test sites in the httpd-vhosts.conf file so you can access them via my-site.test. I use port 443 (for https access) and my certmagic script to generate my certs. Even for my localhost. Though I am on a mac, so I don't think certmagic would work for linux users. You might be able to copy it and remove the part where it adds it to the mac keychain..

etc/extra/httpd-vhosts.conf

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/Applications/XAMPP/xamppfiles/certmagic/certs/server.crt"
    SSLCertificateKeyFile "/Applications/XAMPP/xamppfiles/certmagic/certs/server.key"
    ServerName localhost
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
</VirtualHost>

########################
#  some-site.test
########################

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/Applications/XAMPP/xamppfiles/certmagic/certs/server.crt"
    SSLCertificateKeyFile "/Applications/XAMPP/xamppfiles/certmagic/certs/server.key"
    ServerName some-site.test
    ServerAlias some-site.test
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/some-site"
    ErrorLog "logs/some-site.test-error_log"
    CustomLog "logs/some-site.test-access_log" common
</VirtualHost>

etc/my.cnf

Near the bottom, set it to collation-server=utf8mb4_unicode_ci

etc/php.ini

Search for date.timezone and set your timezone.

phpmyadmin/config.inc.php

I don't change anything in here, but some people might configure a password or something, so worth grabbing just in case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment