138 lines
4.3 KiB
Bash
138 lines
4.3 KiB
Bash
###############################################################################
|
|
#
|
|
#Make sure that install-backup-tools.sh has ran for this site configureation
|
|
#
|
|
#
|
|
###############################################################################
|
|
## Automation
|
|
##
|
|
## Edit contab (nano /etc/crontab)
|
|
## Restart crontab (/etc/init.d/cron restart)
|
|
##
|
|
#################################################################################
|
|
## SFTP
|
|
## if sftp is used comment line "FTPPW" and
|
|
## make sure ssh connection can be made with root's ssh key
|
|
##
|
|
#################################################################################
|
|
|
|
|
|
##Configuration
|
|
|
|
RMDAYS=14 #Days until first backup gets removed
|
|
DOMC=01 #Day of month for monthly backups
|
|
BACKUPSERVICE=ftp #transfer prtocal use ftp or sftp
|
|
REMOTESERVER=<Remote-Server> #Addres of FTP Server
|
|
REMOTEUSER=<user> #User for FTP Server
|
|
FTPPW=<Password> #Password for FTP Server ##Comment out if SFTP is used##
|
|
REMOTEPATH=/Rftp/backup/vps #Path were backup wil be stored on FTP and a site sub-folder will be made
|
|
|
|
DOMAIN=<domain.com> #DomainName
|
|
SITENAME=<name> #Name of site that will be backed up
|
|
BACKUPFROM="/etc/nginx/ /etc/apache2/ /etc/php/ /etc/mysql/ /etc/crontab /opt/ /var/www/"$DOMAIN"/" #Folders and files that will be backed
|
|
|
|
DBNAME=<DBName> #Database that will be backed up
|
|
DBPASS='<MSQLRootPW>' #MySQL Root password (syntax '<MSQLRootPW>')
|
|
|
|
ENABLEUPDATE=1 #set to 1 to enable or to 0 to disable wordpress updates
|
|
|
|
|
|
#############################################################################
|
|
## #
|
|
## PLEASE DO NOT MAKE CHANGES UNDER HERE #
|
|
## #
|
|
#############################################################################
|
|
|
|
|
|
##Setting Static Variables
|
|
LFTP=/usr/bin/lftp #Path to binary
|
|
TMPDIR=$(mktemp -d) #Your archives are here
|
|
SENDDIR=$(mktemp -d) #Sends everything in this folder to ftp
|
|
TODAY=$(date --iso) #Today's date like YYYY-MM-DD
|
|
RMDATE=$(date --iso -d "$RMDAYS days ago") #TODAY minus X days - too old files
|
|
DOM=`date '+%d'` #Day of the month
|
|
|
|
|
|
|
|
##Run
|
|
|
|
#Creating AI1M file
|
|
sudo -u www-data wp --path=/var/www/"$DOMAIN"/html plugin update all-in-one-wp-migration-unlimited-extension
|
|
sudo -u www-data wp --path=/var/www/"$DOMAIN"/html plugin update all-in-one-wp-migration
|
|
cd /var/www/"$DOMAIN"/html
|
|
sudo -u www-data wp ai1wm backup
|
|
mv /var/www/"$DOMAIN"/html/wp-content/ai1wm-backups/*.wpress "$TMPDIR"
|
|
cd $TMPDIR
|
|
tar -zcf /"$SENDDIR"/"$SITENAME"-ai1wm.tar.gz -C $TMPDIR *.wpress #Create ai1wm tar File
|
|
rm -f /"$TMPDIR"/*.wpress
|
|
|
|
#Dumping DataBase
|
|
mysqldump -u root "$DBNAME" --password="$DBPASS" --default-character-set=utf8 > "$TMPDIR"/"$SITENAME".sql #Dumping DB
|
|
tar -zcf /"$SENDDIR"/"$SITENAME"-DB.tar.gz -C "$TMPDIR" *.sql #Create tar File
|
|
|
|
#Taring systemFiles files
|
|
tar -zcf /"$SENDDIR"/"$SITENAME"-Files.tar.gz $BACKUPFROM #Create tar File
|
|
|
|
if [ $BACKUPSERVICE = ftp ]; then
|
|
#Uploading and remove first backup on FTP
|
|
$LFTP << EOF
|
|
open ${REMOTEUSER}:${FTPPW}@${REMOTESERVER}
|
|
cd "${REMOTEPATH}"
|
|
mkdir -p ${SITENAME}/${TODAY}
|
|
cd ${SITENAME}/${TODAY}
|
|
mput -E ${SENDDIR}/*
|
|
cd ..
|
|
rm -rf ${RMDATE}
|
|
bye
|
|
EOF
|
|
|
|
#Checking and running monthly backup to FTP
|
|
if [ $DOM == $DOMC ]; then
|
|
$LFTP << EOF
|
|
open ${REMOTEUSER}:${FTPPW}@${REMOTESERVER}
|
|
cd "${REMOTEPATH}"
|
|
rm -rf ${SITENAME}/Monthly/
|
|
mkdir -p ${SITENAME}/Monthly/
|
|
cd ${SITENAME}/Monthly/
|
|
mput -E ${SENDDIR}/*
|
|
bye
|
|
EOF
|
|
fi
|
|
elif [ $BACKUPSERVICE = sftp ]; then
|
|
#Uploading and remove first backup on SFTP
|
|
$LFTP << EOF
|
|
open -u ${REMOTEUSER}, sftp://${REMOTESERVER}
|
|
cd "${REMOTEPATH}"
|
|
mkdir -p ${SITENAME}/${TODAY}
|
|
cd ${SITENAME}/${TODAY}
|
|
mput -E ${SENDDIR}/*
|
|
cd ..
|
|
rm -rf ${RMDATE}
|
|
bye
|
|
EOF
|
|
|
|
#Checking and running monthly backup to SFTP
|
|
if [ $DOM == $DOMC ]; then
|
|
$LFTP << EOF
|
|
open -u ${REMOTEUSER}, sftp://${REMOTESERVER}
|
|
cd "${REMOTEPATH}"
|
|
rm -rf ${SITENAME}/Monthly/
|
|
mkdir -p ${SITENAME}/Monthly/
|
|
cd ${SITENAME}/Monthly/
|
|
mput -E ${SENDDIR}/*
|
|
bye
|
|
EOF
|
|
fi
|
|
fi
|
|
|
|
#Deletes Temp Folder
|
|
rm -r "$TMPDIR" && rm -r "$SENDDIR"
|
|
|
|
#Wordpress updates
|
|
if [ $ENABLEUPDATE = 1 ]; then
|
|
wp cli update
|
|
sudo -u www-data wp --path=/var/www/"$DOMAIN"/html core update
|
|
sudo -u www-data wp --path=/var/www/"$DOMAIN"/html plugin update --all
|
|
sudo -u www-data wp --path=/var/www/"$DOMAIN"/html theme update --all
|
|
fi
|