Author Topic: Backup to S3 or remote server daily job  (Read 11221 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 »

Offline
*
Re: Backup to S3 or remote server daily job
« Reply #1 on: January 30, 2020, 06:03:00 AM »
I know this is an older post but im wondering if it still applies with the new AWS CLI v2?

I followed the guide but got the errors:

Quote
/scripts/user_backup_s3: line 27: aws: command not found
/scripts/user_backup_s3: line 31: aws: command not found

also in order to securely connect to S3 what needs to be done to install keys etc? is there a good guide on all of this as im a bit of a s3 newb...

i changed the code to look for aws2 instead of aws but i get Unknown Options messages:

Quote
Unknown options: /backups/daily/20200130_stevepl.tar.gz,s3://BUCKETNAME.s3-ap-southeast-2.amazonaws.com/BUCKETPATH/backups/SERVER

BUCKETNAME = the name of my bucket
BUCKETPATH  = the path to my buckets backup folder
SERVER = my folders name
« Last Edit: January 30, 2020, 06:10:18 AM by theozsnowman »