View Source

h2. Question


----
How can I backup and restore individual cPanel accounts with R1Soft CDP?

h2. Solution

cPanel account backups are made with the cpbackup or pkgacct utilities in cPanel.  In addition to the account's or account's home directory files there are dumps of configuration and database queries run by pkgacct.  The full details [are available here|http://docs.cpanel.net/twiki/bin/view/AllDocumentation/WHMDocs/BackupTarballContents].  Some of the contents of a cPanel account backup can only be obtained from your WHM server while it is running. 

You can always restore the entire state of your WHM server and all cPanel accounts from a CDP recovery point by restoring all files or performing a bare-metal restore.  However, you may at times need to restore only individual cPanel accounts, for example when a cancelled customer decides they want to activate or for accidental changes made to any of the account's configuration in cPanel.

Using the example script below you can periodically create cPanel account backups that do not include the files in the account's home directory since those can easily be obtained by restoring files from a CDP recovery point.

h6. Creating Periodic cPanel Account Packages that Get Protected by CDP

Upload or copy the script below to a file in the directory /etc/cron.daily on your WHM server.  This script will create a package of all your cPanel accounts excluding the /home directory files.

Note:
* If you have a very large number of accounts on your WHM server it may only be feasible to run this weekly.  This can be accomplished by placing the script in /etc/cron.weekly.  
* The script uses the ionice utility which requires the CFQ io scheduler and kernel 2.6.13+.  If you do not have the ionice utility available remove "ionice \-c3" from the script.
* Ensure you have enough free space in /home to accommodate the size of the account packages created.

{code}
#!/usr/bin/perl
 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
BEGIN { unshift @INC, '/usr/local/cpanel'; }
 
use Cpanel::Config::Users  ();
 
my @cpusers = Cpanel::Config::Users::getcpusers();
foreach my $user (@cpusers) {
 
  my $cmd = "nice -n 19 ionice -c3 /scripts/pkgacct --skiphomedir " . $user . " 2>&1";
  print "############## ";
  print $cmd;
  print "\n";
  open my $OUTPUT, '-|', $cmd or next;
  while (<$OUTPUT>) {
    print $_;
  }
  close $OUTPUT or next;
}
{code}
The resulting configuration backup will be placed in: /home/cpmove-USERNAME.tar.gz and will be automatically backed up by CDP as long as you have selected the /home file system or disk in your Disk Safe for protection.

h6. Restoring an Individual cPanel Account

# Use CDP (as the server administrator) to restore the file /home/cpmove-USERNAME.tar.gz to an alternate location (e.g. /tmp)
# Login to the cPanel server using SSH and run:&nbsp; # /scripts/restorepkg \--force /tmp/cpmove-USERNAME.tar.gz
# Restore the contents of the /home/USERNAME using CDP file restore

{kb-related-articles}