Poll

Useful For You?

Yes
2 (100%)
No
0 (0%)
I'll stay with CWP provided scripts
0 (0%)

Total Members Voted: 1

Voting closed: May 17, 2020, 11:57:14 AM

Author Topic: Custom Backup  (Read 17286 times)

0 Members and 1 Guest are viewing this topic.

Re: Custom Backup
« Reply #15 on: November 18, 2020, 12:27:08 AM »
That's a small screenshot!

Edit: Phew, that's better. :p

30 minutes to do 5GB ain't too bad but that's with restricting I/O & CPU throughput. Remember to exclude superfluous directories (not folders, please) such as cache and the other listed ones - limit it to the necessary only. Some Wordpress (and others) plugins allow lusers to backup from within their applications. It's pointless doing a backup of a backup: not the same as taking copies of a backup remotely.
(You may have gathered this subject area was one of my corporate consultancy roles.) 
« Last Edit: November 18, 2020, 12:51:34 AM by cynique »

Re: Custom Backup
« Reply #16 on: November 18, 2020, 12:30:54 AM »
Major revamp, with provision for extending to system files.
Use in a cron task as normal, or add a split parameter if you prefer to keep individual parts.

examples:
Quote
12 1 * * * /root/custom-backup.sh > /dev/null 2>&1
Quote
16 3 * * * "/root/custom-backup.sh split" > /dev/null 2>&1


Code: [Select]
#!/usr/bin/bash
# CWP Custom backup
# Version 3.1 ejsolutions/Teo/cynique
# Use split as a parameter to retain split backups
# Set the following 3 items to suit
tmp_dir=/home/tmp_bak/
backup_dir=/backup/custom/
retention=1
# Optional though advisable
# Create a backup_exclude.conf file in each user home directory,
# listing directories to exclude, for example
# backupcwp/
# tmp/
# cache/
# -------------------
if [ -d ${tmp_dir} ]; then
 mkdir -p ${tmp_dir}
fi
if [ -d ${backup_dir} ]; then
 mkdir -p ${backup_dir}
fi
now_dir=${backup_dir}$(date -d "today" +"%Y%m%d%H%M")
echo "Timestamped location: " ${now_dir}
mkdir -p ${now_dir}/accounts
# -------------------
mysql root_cwp -B -N -s -e "SELECT username,domain FROM user WHERE backup='on'" | while read -r username domain
do

echo Custom backup task starting for $username at $domain
mkdir -p ${tmp_dir}${username}/home_dir
echo Copying home directory
if [ -f /home/${username}/backup_exclude.conf ]; then
ionice -c 3 nice rsync -a --exclude-from=/home/${username}/backup_exclude.conf /home/${username}/ ${tmp_dir}${username}/home_dir
else
ionice -c 3 nice rsync -a /home/${username}/ ${tmp_dir}${username}/home_dir
fi
echo Backing up databases
mkdir -p ${tmp_dir}${username}/mysql/
mysql --defaults-extra-file=/root/.my.cnf -e "show databases LIKE '${username}%';" | grep -v Database | while read databasename
 do
     echo Dumping $databasename
     nice -n 5 mysqldump --defaults-extra-file=/root/.my.cnf "$databasename" > ${tmp_dir}${username}/mysql/"$databasename.sql" \
               2> ${tmp_dir}${username}/mysql/errors.txt && sync && \
nice gzip ${tmp_dir}${username}/mysql/"$databasename.sql"
 done
if [ -d /var/vmail/${domain} ]; then
 mkdir -p ${tmp_dir}${username}/vmail/
 echo Copying email
 ionice -c 3 nice cp -fR /var/vmail/${domain} ${tmp_dir}${username}/vmail/
fi
if [ "split" = "$1" ]; then
mkdir -p ${now_dir}/accounts/${username}/
for i in home_dir mysql vmail
do
  echo "Compressing " $i
  ionice -c 3 nice -n 15 tar -cjf ${now_dir}/accounts/${username}/$i.tar.bz2 ${tmp_dir}${username}/$i 2>/dev/null
done
else
echo Consolidating files
ionice -c 3 nice -n 15 tar -cjf ${now_dir}/accounts/${username}.tar.bz2 ${tmp_dir}${username}
fi
echo Cleaning up
# /usr/bin/find ${backup_dir} -name "*.bz2" -mtime +${retention} -delete > /dev/null 2>&1
/usr/bin/find ${backup_dir} -mtime +${retention} -delete > /dev/null 2>&1
rm -Rf ${tmp_dir}${username}

done
echo Custom Backup Job Finished
« Last Edit: November 18, 2020, 12:32:59 AM by cynique »

Re: Custom Backup
« Reply #17 on: November 18, 2020, 01:07:30 AM »
BTW, for large backup sets, zstd is probably the way to go, rather than bz2. It's fairly new on the scene however, so application support is limited. Pigz (as used by WHM/cPanel) might also help and is more established. This opens a real can of worms though, as can be seen if you search for a performance comparison of these. ;)
I like to stick to the basics where possible and well established methods. KISS philosophy, again.

Offline
*
Re: Custom Backup
« Reply #18 on: November 18, 2020, 01:17:45 AM »
That's a small screenshot!
Eek, yea without manual constraints it took my desktop resolution and ran with it  ::)

30 minutes to do 5GB ain't too bad but that's with restricting I/O & CPU throughput.
Agreed, I'm just a WordPress Developer (SCSS/PHP) hosting a bunch of clients sites. If this scales up in the near future I would strongly consider upgrading the current EC2 rig, it's currently 2x vCPU's and 4GB RAM.

Remember to exclude superfluous directories (not folders, please) such as cache and the other listed ones - limit it to the necessary only.
I meant to ask you about this! Could we add those as a wildcard within the variables above this script, with additional ones in the users config file (that you've already set)? For example:
#exclude these folders in all user directories:
/backupcwp/
/tmp/
/cache/
/cwp_stats/

Would be great, especially for anyone with a whole bunch of user accounts :)

Some Wordpress (and others) plugins allow lusers to backup from within their applications. It's pointless doing a backup of a backup: not the same as taking copies of a backup remotely.
Already on it, strongly advised all users that we didn't get a 500GB external drive for nothing but some still ignore it..  ;D

(You may have gathered this subject area was one of my corporate consultancy roles.)
Could have guessed, yea ;)

Offline
*
Re: Custom Backup
« Reply #19 on: November 18, 2020, 01:27:50 AM »
BTW, for large backup sets, zstd is probably the way to go, rather than bz2. It's fairly new on the scene however, so application support is limited.
Looks really good, but yea maybe wait a while until its more established ;)

Pigz (as used by WHM/cPanel) might also help and is more established. This opens a real can of worms though, as can be seen if you search for a performance comparison of these. ;)
I like to stick to the basics where possible and well established methods. KISS philosophy, again.
Having seen the stats of Pigz it looks great, was there a reason you opted for bz2 aside from old habits? :)

Offline
*
Re: Custom Backup
« Reply #20 on: November 18, 2020, 01:29:31 AM »
Major revamp, with provision for extending to system files.

Super excited, nice one!
Saving this for later today, have the original CWP backups running atm. I have to test a solution before moving over to it permanently, live clients with real websites.. etc :)

Re: Custom Backup
« Reply #21 on: November 18, 2020, 11:22:45 AM »
Could we add those as a wildcard within the variables above this script..
simple script for general deployment:
Code: [Select]
#!/usr/bin/bash
for i in /home/*
 do
  cp /root/backup_exclude.conf /home/$i
 done
Quote from: FreshLondon
..was there a reason you opted for bz2 aside from old habits?
Got it in a oner. ;)
Got a multitude of other things to do, rather than explore a particular area and my burnt-out brain can only cope with so much at once.

*** I'll reiterate, from the opening post. This custom backup is used to supplement, not replace the CWP provided backup routines. It doesn't (yet?) cater for IP allocation, subdomains, email forwarders, packages and other functions. It is intended as a "failsafe" backup that can be used to restore most functionality of a user website onto any control panel, not just CWP. Additionally, it correctly uses retention terminology, (currently) unlike the CWP one. ;) ***
« Last Edit: November 18, 2020, 12:10:13 PM by cynique »

Offline
*
Re: Custom Backup
« Reply #22 on: November 18, 2020, 01:22:26 PM »
  cp /root/backup_exclude.conf /home/$i
This looks amazing, thank you! I will do this once I get my backup_exclude.conf correct :)

Speaking of which, here it is:
Code: [Select]
#listing directories to exclude from custom backup:
backupcwp/
tmp/
cache/
cwp_stats/
ai1wm-backups/
ai1wm-backups/
wpvividbackups/
The last three entries are in the public_html/wp-content/ folder. This is relevant for all /wp-content/ folders, not just in public_html (imagine a subdomain or addon domain, for example).
Can we wildcard these, or is it already done?

For example, does mentioning:
Code: [Select]
wpvividbackups/also include:
Code: [Select]
public_html/wp-content/ai1wm-backups/
public_html/wp-content/domains/domainone.com/ai1wm-backups/
public_html/wp-content/domains/domaintwo.com/ai1wm-backups/

or do these have to be set individually?
Would be great to wildcard the folder name, let me know if its possible!



*** I'll reiterate, from the opening post. This custom backup is used to supplement, not replace the CWP provided backup routines. It doesn't (yet?) cater for IP allocation, subdomains, email forwarders, packages and other functions. It is intended as a "failsafe" backup that can be used to restore most functionality of a user website onto any control panel, not just CWP. Additionally, it correctly uses retention terminology, (currently) unlike the CWP one. ;) ***

Agreed, and in time I will uncheck the options this backup covers but leave on the CWP backup for options it doesnt (like the ones you listed) but it is a VERY good start.

On another note, I hope you don't mind my suggestions. Just trying to give useful input/feedback, what's been achieved already is immense  :D

Re: Custom Backup
« Reply #23 on: November 18, 2020, 01:46:56 PM »
IIRC it's an implicit pattern search, for exclusion, however I may be proven wrong - rsync is relatively new to me, in the grand scheme of things.
https://www.howtogeek.com/168009/how-to-exclude-files-from-rsync/
Which implies *backups/  could/should be added in your use case. Generally, backup exclusions/inclusions is an iterative process until a compromise is made between protection and speed/space.

I've been running with this for well over a year, so not really a "start". It saved my bacon once, when a VPS went down and I temporarily commissioned the (small spec.) backup VPS. I shared the script, to give others the benefit, especially as the CWP one has various (continuing/changing) issues and the developers haven't taken my advice onboard. It gets frustrating!
As you'll notice, the original script I used has somewhat morphed but it'll never have all the gizmos in it - there's commercial backup software for that (though even biggies from, for example R1Soft are bug-ridden/ill-conceived).

That being said, thanks for stirring me into the revamp - much easier to manage my multiple encrypted nextcloud remote backups now. :) 8)

Now, back to looking at a PHP e-commerce migration script, plus a newly created VPN. :'(
« Last Edit: November 18, 2020, 01:51:10 PM by cynique »

Offline
*
Re: Custom Backup
« Reply #24 on: November 18, 2020, 02:18:23 PM »
Which implies *backups/  could/should be added in your use case. Generally, backup exclusions/inclusions is an iterative process until a compromise is made between protection and speed/space.
I'll test and let you know, thanks! :)

That being said, thanks for stirring me into the revamp - much easier to manage my multiple encrypted nextcloud remote backups now. :) 8)
I know the feeling, this is like a theme I built a year ago. People come back and ask for edits i'd never imagined, pushing it to be better each time.

Now, back to looking at a PHP e-commerce migration script, plus a newly created VPN. :'(
Epic, now that is something I can do! All this linux stuff is a little bit beyond me, but i'm learning. It's a steep road ;D

Re: Custom Backup
« Reply #25 on: November 18, 2020, 02:26:02 PM »
Lemme know how you get on with V3.1 - just curiosity. 

Offline
*
Re: Custom Backup
« Reply #26 on: November 18, 2020, 02:39:50 PM »
Major revamp, with provision for extending to system files.
Use in a cron task as normal, or add a split parameter if you prefer to keep individual parts.

Just started a test, OMG ITS SPLITTING IT!!!  ;D

One thing I've noticed though:


Any way we can remove the HH:MM from the date string for efficiency? :)

Offline
*
Re: Custom Backup
« Reply #27 on: November 18, 2020, 03:32:48 PM »
Lemme know how you get on with V3.1 - just curiosity.

It split, that worked really well. :) I don't know what. was expecting, maybe a bit of fear there whenever running as new script, but it worked great!

I assumed (after seeing a folder named yyyymmddhhmm) that it would create loads of folders but no, thats just the time that the backup was initiated. There is just one folder with several /home/ accounts inside!

Things to note:
YES, the backup exclusions work no matter what directory (really impressed)  ;D

inside the backup created folder:
202011181436/accounts/yogacaveinitiati/
was a file for the home directory.

Unzipping this created a series of folders (inside the above mentioned path):
home/tmp_bak/yogacaveinitiati/home_dir/
not an issue, reeeeally not an issue.. but if you were wanting this to be super perfect, maybe just trim it down to /home_dir/ or something similar?

Epic job my man, epic job!

Offline
*
Re: Custom Backup
« Reply #28 on: November 18, 2020, 03:35:19 PM »
side note but not important, a client started sending 30k emails while i was testing the backup.
QUICKLY KILL PROCESS, KILL PROCESS, KILL PROCESS.

Jeez my pants need changing ;D

Re: Custom Backup
« Reply #29 on: November 18, 2020, 04:24:16 PM »
PMSL at the emails - spammer! ban, ban, ban  >:(

HH:MM remains so that folks can run more than once a day, particularly during testing. Of course, doesn't affect directory sorts etc. ;)

Yeah, during a test (and later needed) restore I spotted the subdir creation. As you said, ain't major but a bit unpolished.  :-[
Maybe a v3.2 will address this - who knows?

(Side note: my replacement VPN setup went peachy.. time to firewall it.)
« Last Edit: November 18, 2020, 04:26:09 PM by cynique »