[HocVPS Script Plugin] – Automatically backup the entire VPS

Yesterday I had to share a script to help automatically backup the entire WordPress website activity on the server settings HocVPS Script . However, today I will introduce a better bash scripts, the entire VPS backup with instructions to create autorun daily cronjob always.

This script automatically back up all data on the server, including:

  1. The entire MySQL database, each database .sql file
  2. The entire code in the folder public_html of the domain, in the form /home/domain.com/public_html/
  3. Complete Nginx configuration folder /etc/nginx/conf.d/

All data will be compressed into a .zip file to the directory and / root / backup / and automatically delete old backup files, only keeping the file within 14 days of return.

Thus, you absolutely can use this script to move the VPS, or restore any particular piece of data. Her script writing is compatible with the directory structure by HocVPS Script management, but you can also modify to fit any other server also.

Currently, I am using this script to daily data backup and upload to Google Drive with Rclone.

Guidelines automatically backup the entire VPS

1. Create a backup script bash

– Creates backup.sh file in the directory / root /

 nano /root/backup.sh

Consult guidelines use nano if unknown user.

– Copy the entire contents of the script below and paste into:

 # HocVPS Plugin Script - Backup Server

#! / Bin / bash

. /etc/hocvps/scripts.conf

SERVER_NAME = VPS

TIMESTAMP = $ (date + "% Y-% m-% H% M D_%")
BACKUP_DIR = "/ root / backup / $ TIMESTAMP"
MYSQL_USER = "root"
MYSQL = / usr / bin / mysql
MYSQL_PASSWORD = $ mariadbpass
Mysqldump = / usr / bin / mysqldump
SECONDS = 0

mkdir -p "$ BACKUP_DIR / mysql"

echo "Starting Backup Database";
= `$ MYSQL databases --user = $ MYSQL_USER -p -e $ MYSQL_PASSWORD" SHOW databases; " | -Ev grep "(Database | information_schema | performance_schema | mysql)" `

print for $ db databases; dirty
$ Mysqldump --user = $ MYSQL_USER --opt --force -p $ MYSQL_PASSWORD --databases $ db | gzip> "$ BACKUP_DIR / mysql / $ db.gz"
done
echo "Finished";
echo '';

echo "Starting Backup Website";
#Loop Through the / home directory
for D in / home / *; dirty
if [ -d "$D" ]; then #if a directory
domain = $ D ## * / # Domain name
echo "-" $ domain;
zip -r $ BACKUP_DIR / $ domain.zip / home / $ domain / public_html / q -x / home / $ domain / public_html / wp-content / cache / **  * #Exclude cache
fi
done
echo "Finished";
echo '';

echo "Starting Backup Nginx Configuration";
cp -r $ /etc/nginx/conf.d/ BACKUP_DIR / nginx /
echo "Finished";
echo '';

echo "Starting Compress Files";
zip -r -q /root/backup/$SERVER_NAME-$TIMESTAMP.zip $ BACKUP_DIR
rm -rf $ BACKUP_DIR
size = $ (ls -lah /root/backup/$SERVER_NAME-$TIMESTAMP.zip | awk 'print $ 5')
echo "Finished";
echo '';

#remove Older backups (14 days)
find / root / backup / -mindepth 1 -mtime +14 -delete

duration = $ SECONDS
echo "Total $ size, $ (($ duration / 60)) minutes and $ (($ duration% 60)) seconds elapsed."

Additional explanation:

  • Name the backup file is automatically set to the current date and time.
  • MYSQL_USER default is root, password is taken in the configuration file /etc/hocvps/scripts.conf.

– Press Ctrl + O, Enter to save and Ctrl + X to exit.

– Distribution rights to the script

 chmod + x /root/backup.sh

– So that’s it then, now you can test it by running the command /root/backup.sh

Done, check the folder / root / backup / has yet lightweight .ZIP file backup.

2. Create automated daily backup cronjob

Now I would for scripts automatically run at 2:00 am.

 crontab -e EDITOR = nano

Paste the following into the Terminal window

 0 2 * * * /root/backup.sh> / dev / null 2> & 1

Press Ctrl + O, Enter to save and Ctrl + X to exit

That’s it, keep daily 2am script automatically runs, the entire data backup of VPS.

In the next article, I will guide you how to automatically upload files to Google Drive backups to save space for VPS. Me welcome to read offline.

[HocVPS Script Plugin] – Automatically backup the entire VPS
5 (100%) 10 vote