This is a bash script for backing up multiple web sites, MySQL databases and /etc/ into a specified backups directory.
Once configured (variables set within the script), it does this:
- Creates a directory for your site (file) backups (if it doesn't exist) with option for exclude;
- Creates a directory for your MySQL dumps (if it doesn't exist);
- Loops through all of your MySQL databases and dumps each one of them to a gzipped file;
- Creates a directory for your configurations (/etc/) (if it doesn't exist);
- Tars and gzips each folder within your sites directory (I keep my websites in /var/www/html/).
___This script works fine for me (using Ubuntu 16.04.3 LTS on Vultr and selfhosted), but servers vary greatly. USE THIS SCRIPT AT YOUR OWN RISK! There is always risk involved with running a script. I AM NOT RESPONSIBLE FOR DAMAGE CAUSED BY THIS SCRIPT. For this reason I removed the delete functionality from the project I forked. ___
You may very well know more about bash scripting and archiving than I do. If you find any flaws with this script or have any recommendations as to how this script can be improved, please fork it and send me a pull request.
- MOST IMPORTANTLY: Open the backup.sh file in a text editor and set the configuration variables at the top (see below).
- Optionally, edit the tar command on line 97 to add some more --exclude options (e.g. --exclude="cache/*")
- Place the backup.sh file somewhere on your server (something like /opt/full-web-server-backup).
- Make sure the backup.sh script is executable by root:
sudo chmod 744 /opt/full-web-server-backup/backup.sh
- Preferably set up cron to run it every night (see below).
There are a bunch of variables that you can set to customize the way the script works. Some of them must be set before running the script!
NOTE: The BACKUP_DIR setting is preset to /backups/site-backups. If you want to use something like /var/site-backups, you'll need to create the directory first and set it to be writable by you.
- BACKUP_DIR: The parent directory in which the backups will be placed. It's preset to:
"/backups/site-backups"
- KEEP_MYSQL: How many days worth of mysql dumps to keep. It's preset to:
"14"
- KEEP_SITES: How many days worth of site tarballs to keep. It's preset to:
"2"
- MYSQL_HOST: The MySQL hostname. It's preset to the standard:
"localhost"
- MYSQL_USER: The MySQL username. It's preset to the standard:
"root"
- MYSQL_PASS: The MySQL password. You'll need to set this yourself!
- MYSQL_BACKUP_DIR: The directory in which the dumps will be placed. It's preset to:
"$BACKUP\_DIR/mysql/"
- SITES_DIR: This is the directory where you keep all of your web sites. It's preset to:
"/var/www/html/"
- SITES_BACKUP_DIR: The directory in which the archived site files will be placed. It's preset to:
"$BACKUP_DIR/sites/"
- THE_DATE: The date that will be appended to filenames. It's preset to:
"$(date '+%Y-%m-%d')"
(2017-11-07)
- MYSQL_PATH: Path to mysql. It's preset to:
"$(which mysql)"
- MYSQLDUMP_PATH: Path to mysqldump. It's preset to:
"$(which mysqldump)"
- FIND_PATH: Path to find. It's preset to:
"$(which find)"
- TAR_PATH: Path to tar. It's preset to:
"$(which tar)"
- RSYNC_PATH: Path to rsync. It's preset to:
"$(which rsync)"
- Cron;
- External password file usage (in ~/.my.cnf);
- Define compression level for each task (mySQL, DB, configuration);
- Include/exclude specific directories (for example archive only /etc/nginx and /etc/mySQL directories);
- Keep n number of files or add verification for disk space + email to admin if disk is low on space.