sh webserver-backup.sh

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh webserver-backup.sh相关的知识,希望对你有一定的参考价值。

#!/bin/bash
# Execute it as root, due to archiving permision conflicts
#CRONTAB entry for every day at 04:00 AM
#0 4 * * * /usr/local/bin/webserver-backup.sh >/dev/null 2>&1

# DATABSE DATA
DB_USER="XXX"
DB_PASSWORD="XXX"

# PATH'S
OUTPUT_PATH="/srv/backup"
WWW_PATH="/srv/webserver"
TEMP_PATH="/srv/backup/temp" #where to store data temporary before archiving and encrypting

# FTP DATA (where to save backup)
FTP_LOGIN="XXX"
FTP_PASS="XXX"
REMOTE_PATH="BACKUPS/WEBSERVER/"
REMOTE_HOST="77.23.XXZ.XX"
PORT="21"

# OTHER CONSTS
DATE=`date +%Y-%m-%d`
LOG_DATE=`date +[%Y-%m-%d:%H:%M:%S]`

# create directory if not exist
mkdir -p "$TEMP_PATH"

# remove old backup data at webserver in case error occurred during upload.
rm -f $TEMP_PATH/*.sql > /dev/null 2>&1
rm -f $OUTPUT_PATH/*.tar.gz > /dev/null 2>&1
rm -f $OUTPUT_PATH/*.tar.gz.asc > /dev/null 2>&1

# get database table names
databases=`mysql --user=$DB_USER --password=$DB_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

echo "$LOG_DATE DUMPING DATABASE TABLES"
for db in $databases; do
	# Ignore default phpmyadmin tables
    if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] && [[ "$db" != mysql* ]] && [[ "$db" != "performance_schema" ]] ; then
        name=$TEMP_PATH/$DATE-$db
        echo "$LOG_DATE Dumped: $name"
		mysqldump --force --opt --user=$DB_USER --password=$DB_PASSWORD --databases $db > $name.sql
    fi
done
echo "$LOG_DATE DONE!"


# arhive dumped sql's and webserver directories
echo "$LOG_DATE Archieving data"
tar -czf $OUTPUT_PATH/$DATE-webserver.tar.gz -C $TEMP_PATH . -C $WWW_PATH .
echo "$LOG_DATE DONE!"

# ENCRYPT archive
# gpg -e -u "Sender User Name" -r "Receiver User Name" somefile
# --yes auto overwrite
echo "$LOG_DATE ENCRYPTING WEBSERVER"
/usr/bin/gpg --yes -e -a -r "mindaugas" $OUTPUT_PATH/$DATE-webserver.tar.gz
# remove archived after encryption
rm -f $OUTPUT_PATH/*.tar.gz > /dev/null 2>&1
echo "$LOG_DATE DONE!"


echo "$LOG_DATE UPLOADING TO REMOTE FTP"
sudo curl -T $OUTPUT_PATH/$DATE-webserver.tar.gz.asc ftp://$REMOTE_HOST:$PORT/$REMOTE_PATH/ --user $FTP_LOGIN:$FTP_PASS
echo "$LOG_DATE DONE!"

 
# CHECK IF BACKUP EXIST IN REMOTE FTP
if curl $OUTPUT_PATH/$DATE-webserver.tar.gz.asc ftp://$REMOTE_HOST:$PORT/$REMOTE_PATH/ -s | grep $DATE-webserver.tar.gz.asc; then
	echo "$LOG_DATE ENCRYPTED ARCHIVE SUCCESFULLY UPLOADED!"
	echo "$LOG_DATE DELETING TEMP DATA"
	rm -f $TEMP_PATH/*.sql > /dev/null 2>&1
	rm -f $OUTPUT_PATH/*.tar.gz > /dev/null 2>&1
	rm -f $OUTPUT_PATH/*.tar.gz.asc > /dev/null 2>&1
	echo "$LOG_DATE DONE!"cronmt
	echo "$LOG_DATE DO WEBSERVER BACKUP SUCCESS!" | /usr/local/bin/slackpost.sh
else
  echo "$LOG_DATE [ERROR] NO BACKUP WAS FOUND AFTER UPLOAD!" | /usr/local/bin/slackpost.sh
fi

以上是关于sh webserver-backup.sh的主要内容,如果未能解决你的问题,请参考以下文章

Linux下执行.sh文件

如何使我的命令行在具有扩展名(.sh)和名称如“weird.sh.sh.sh”的文件上工作

sh sh_template.sh

sh sh.sh

Linux下面如何运行 SH文件

配置告警系统主脚本main.sh mon.sh load.sh 502.sh disk.sh