Introduction
How to restore missing emails from a backup to an email account within the command line.
Procedure
1. Navigate to your backup directory and extract backup for that account:
[root@cptechs ~]# ls -lah /backup/2021-09-03/accounts/cptechs.tar.gz
-rw------- 1 root root 8.6M Sep 3 02:01 /backup/2021-09-03/accounts/cptechs.tar.gz
[root@cptechs ~]#
To extract the tar.gz file, you can use the following command:
tar -xf /backup/2021-09-03/accounts/cptechs.tar.gz
Alternatively, you can add the option -C to specify the location of extraction:
tar -xf /backup/2021-09-03/accounts/cptechs.tar.gz -C ./testing/
2. (OPTIONAL) Create your email account either within the cPanel interface or via the API if the email account is not already created.
3. Once extracted the backup, locate the emails you wish to restore and copy them to the email account you desire to import these emails to:
cp testing/cptechs/homedir/mail/cptechs.com/cptechs/cur/* /home/cptechs/mail/cptechs.com/cptechs/cur/
In this case, I'm copying all the emails from the cur directory from the extraction performed in step 1 to the cur directory in the email account. If you wish to restore all emails you could copy all the mail directories (cur, new, .spam, .Trash, etc...).
4. When you have finally copied all the missing emails, you will have to change the ownership of the emails you have copied as they are currently as root:root
:
[root@cptechs ~]# ls -lah /home/cptechs/mail/cptechs.com/cptechs/cur/
total 656K
drwxr-x--x 2 cptechs cptechs 4.0K Sep 4 00:13 .
drwxr-x--x 11 cptechs cptechs 4.0K Sep 4 00:14 ..
-rw-r----- 1 root root 69K Sep 4 00:13 1619257310.M639539P31366.184-94-197-2.cprapid.com,S=70233,W=71445:2,ST
-rw-r----- 1 root root 198K Sep 4 00:13 1626428363.M591959P2848.184-94-197-2.cprapid.com,S=202716,W=205552:2,ST
-rw-r----- 1 root root 2.3K Sep 4 00:13 1628241325.M137822P2360.184-94-197-2.cprapid.com,S=2270,W=2338:2,T
-rw-r----- 1 root root 1.6K Sep 4 00:13 1628241403.M664399P2360.184-94-197-2.cprapid.com,S=1635,W=1682:2,T
-rw-r----- 1 root root 69K Sep 4 00:13 1628580668.M654617P27013.184-94-197-2.cprapid.com,S=70235,W=71449:2,T
-rw-r----- 1 root root 198K Sep 4 00:13 1628580668.M661101P27013.184-94-197-2.cprapid.com,S=202718,W=205556:2,T
-rw-r----- 1 root root 2.3K Sep 4 00:13 1628580668.M671948P27013.184-94-197-2.cprapid.com,S=2272,W=2342:2,ST
-rw-r----- 1 root root 1.6K Sep 4 00:13 1628580668.M674398P27013.184-94-197-2.cprapid.com,S=1637,W=1686:2,ST
-rw-r----- 1 root root 69K Sep 4 00:13 1628581055.M467747P28015.184-94-197-2.cprapid.com,S=70235,W=71449:2,S
-rw-r----- 1 root root 2.3K Sep 4 00:13 1628660940.M646208P1221.184-94-197-2.cprapid.com,S=2320,W=2388:2,S
-rw-r----- 1 root root 2.5K Sep 4 00:13 1628661081.M444426P1525.184-94-197-2.cprapid.com,S=2543,W=2616:2,S
-rw-r----- 1 root root 1.6K Sep 4 00:13 1628849801.M613155P32539.184-94-197-2.cprapid.com,S=1635,W=1682:2,S
-rw-r----- 1 root root 2.8K Sep 4 00:13 1628849827.M174081P32539.184-94-197-2.cprapid.com,S=2819,W=2895:2,S
[root@cptechs ~]#
You can change the ownership utilizing chown as such (replace user and group with the cPanel user account):
chown user:group file.txt
In my case, I have run the following as my cPanel user is cptechs:
chown cptechs:cptechs /home/cptechs/mail/cptechs.com/cptechs/cur/1619257310.M639539P31366.184-94-197-2.cprapid.com,S=70233,W=71445:2,ST
It is not recommended that you run chown or chmod recursively.
5. Done.