2024-07-29 17:15:47 +02:00
|
|
|
# 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!!!
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2023-10-18 16:54:51 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
#---------------------------
|
2023-10-18 16:54:51 +00:00
|
|
|
|
|
|
|
echo -e "Stoping webserver and starting backup procedure...\n"
|
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
systemctl stop $SERVICE # Stop service
|
2023-10-18 16:54:51 +00:00
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
rm -rf $DEST/* & # Clean destination folder first
|
|
|
|
remove=$! # get the pid number of the cleaning command
|
|
|
|
wait $remove # and wait for it to finish
|
2023-10-18 16:54:51 +00:00
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
mysqldump --user $DBUSER --password=$DBPASS $DB > $DEST/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
|
2023-10-18 16:54:51 +00:00
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
cp -r $SOURCE $DEST # Copy the website folder to destination
|
|
|
|
copy=$! # get the pid number of the copy command
|
|
|
|
wait $copy # and wait for it to finish
|
2023-10-18 16:54:51 +00:00
|
|
|
|
2024-07-29 17:15:47 +02:00
|
|
|
systemctl start $SERVICE # Start service
|
2023-10-18 16:54:51 +00:00
|
|
|
|
|
|
|
echo -e "Website backed up\nWebserver back online.\n"
|
2024-07-29 17:15:47 +02:00
|
|
|
logger "Website backed up into $DEST - [WEB-BACKUP]" # Log backup time in journalctl
|