Author Topic: Backup to S3 or remote server daily job  (Read 34 times)

0 Members and 1 Guest are viewing this topic.

Offline
*
Backup to S3 or remote server daily job
« on: April 15, 2018, 05:11:38 PM »
1. Install & Configure AWS CLI https://docs.aws.amazon.com/cli/latest/userguide/installing.html
2.You can comment at /etc/cron.daily/cwp
 
Code: [Select]
#/usr/local/cwp/php71/bin/php -d max_execution_time=1000000 -q /usr/local/cwpsrv/htdocs/resources/admin/include/cron_backup.php 
3. New bash file like as /scripts/full_backup with content
     
Code: [Select]
#!/bin/bash
#
# BACKUP SCRIPT FOR USER FILES AND MYSQL DATABASES
# email: trongkhoa1103@gmail.com
#
#
MYSQLPWD=`grep password= /root/.my.cnf|awk -F= {'print $2'}`
QUERYCODE="select username from root_cwp.user where backup='on'"
# MySQL Backup
for i in `mysql --skip-column-names -uroot -p"$MYSQLPWD" -e "$QUERYCODE" -B`; do  echo "$i"|sh /scripts/user_backup_s3 $i;done
4.create /scripts/user_backup_s3

Code: [Select]
#!/bin/bash
#
# BACKUP SCRIPT FOR USER FILES AND MYSQL DATABASES
# trongkhoa1103@gmail.com
#
#

USERNAME=$1
BKPDATE=`date +%Y%m%d`
GETHOMEDIR="/backups"
GETUSERHOMEDIR=`grep $USERNAME /etc/passwd|awk -F: {'print $6'}`
QUERYCODE="show databases like \""$USERNAME"_%\""
MYSQLPWD=`grep password= /root/.my.cnf|awk -F= {'print $2'}`

if [ ! -e "$GETHOMEDIR/daily"  ];then
mkdir -p "$GETHOMEDIR"/daily
fi

if [ ! -e "$GETHOMEDIR/mysql"  ];then
        mkdir -p "$GETHOMEDIR"/mysql
fi


# MySQL Backup
for i in `mysql --skip-column-names -uroot -p"$MYSQLPWD" -e "$QUERYCODE" -B`; do mysqldump -uroot -p"$MYSQLPWD" $i | gzip > "$GETHOMEDIR/mysql/$BKPDATE_$i".sql.gz;done
# move to S3
aws s3 mv $GETHOMEDIR/mysql/*.* s3://[bucketname]/[path]
# Files Backup
su  -c "cd $GETUSERHOMEDIR;tar -pczf $GETHOMEDIR/daily/"$BKPDATE"_"$USERNAME".tar.gz * --exclude 'backups'" -s "/bin/bash"
# move to S3
aws s3 mv $GETHOMEDIR/daily/*.* s3://[bucketname]/[path]
4. Create Cron job at 00:30 or anytime you want
 
Code: [Select]
30 0 * * * /usr/bin/sh /scripts/full_backup >/dev/null 2>&1My enviroment:
  OS Centos 7
  PHP version 7.1
 
if any problem you can contact me:
 skype: trongkhoa1505
 email: trongkhoa1103@gmail.com or khoabidihost@gmail.com
« Last Edit: April 15, 2018, 05:15:10 PM by lytrongkhoa »