88 lines
2.2 KiB
Bash
88 lines
2.2 KiB
Bash
# backing.sh
|
|
# ---------------------
|
|
#
|
|
# Backup a website folder and its database
|
|
#
|
|
# SYNOPSIS
|
|
# backing.sh [DB] [DBUSER] [DBPASS] [DEST] [SOURCE]
|
|
#
|
|
# This file can be used in two ways,
|
|
# -- Input the variables in the command line with the above synopsis
|
|
# -- Or run the file without any arguments and edit the variables below
|
|
#
|
|
# !!!Make sure the destination folder exists!!!
|
|
#
|
|
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
# Variables ----------------
|
|
DB="database" # Database name
|
|
DBUSER="databaseUser" # Database user
|
|
DBPASS="databasePass" # Database user password
|
|
DEST="/home/user/backups/daily/latest" # Backup folder
|
|
SOURCE="/usr/share/nginx/web_folder" # Source of the website folder
|
|
SERVICE="systemd.service" # Systemd web service
|
|
#---------------------------
|
|
|
|
|
|
# Check for command line arguments -----------------
|
|
if [ -z "$1" ] # First is database name
|
|
then
|
|
DB_S=$DB
|
|
else
|
|
DB_S=$1
|
|
fi
|
|
|
|
if [ -z "$2" ] # Second is database user
|
|
then
|
|
DBUSER_S=$DBUSER
|
|
else
|
|
DBUSER_S=$2
|
|
fi
|
|
|
|
if [ -z "$3" ] # Third is database user password
|
|
then
|
|
DBPASS_S=$DBPASS
|
|
else
|
|
DBPASS_S=$3
|
|
fi
|
|
|
|
if [ -z "$4" ] # Fourth is destination backup folder
|
|
then
|
|
DEST_S=$DEST
|
|
else
|
|
DEST_S=$4
|
|
fi
|
|
|
|
if [ -z "$5" ] # Fifth is source website folder
|
|
then
|
|
SOURCE_S=$SOURCE
|
|
else
|
|
SOURCE_S=$5
|
|
fi
|
|
#---------------------------------------------------
|
|
|
|
|
|
echo -e "Stoping webserver and starting backup procedure...\n"
|
|
|
|
systemctl stop $SERVICE # Stop service
|
|
|
|
rm -rf $DEST_S/* & # Clean destination folder first
|
|
remove=$! # get the pid number of the cleaning command
|
|
wait $remove # and wait for it to finish
|
|
|
|
mysqldump --user $DBUSER_S --password=$DBPASS_S $DB_S > $DEST_S/data_latest.sql & # Get the database to destination
|
|
dump=$! # get the pid number of the database command
|
|
wait $dump # and wait for it to finish
|
|
|
|
cp -r $SOURCE_S $DEST_S # Copy the website folder to destination
|
|
copy=$! # get the pid number of the copy command
|
|
wait $copy # and wait for it to finish
|
|
|
|
systemctl start $SERVICE # Start service
|
|
|
|
echo -e "Website backed up\nWebserver back online.\n"
|
|
logger "Website backed up into $DEST_S - [WEB-BACKUP]" # Log backup time in journalctl
|