Ever struggled to restore a file from a cpbackup backup tarball?
Did it take 10 commands and 20 lines of typing and fiddling?
How would it be if you could restore any client file in only one short command?
NOTE: July 2013: cprevert does not work with the new-style cpanel backup format, will post here when I fix it!
Cpbackup is a simple and effective way of backing up an entire account, but really sucks badly if you need to restore only one file!
There’s a solution – but it requires shell access!
Ok hyper mode off, switching to a more serious explanation:
We’d like to share a shell script for restoring versions of directories or folders. To run it, simply change your directory to your client directory, and run the command “cprevert filename”.
Here’s an example that should make it all a lot clearer:
[host1:/home/fredshop/public_html/plugins]# revert error_log
user = fredshop
looking for ./public_html/plugins
–> 20100811_daily … hfm
–> 20100810_daily … hf(no change)
–> 20100809_daily … hf(no change)
–> 20100808_daily … hf(no change)
–> 20100807_daily … hf(no change)
–> 20100806_daily … hf(no change)
–> 20100805_daily … hf(no change)
–> 20100805_weekly … hf(no change)
–> 20100729_weekly … hfm
–> 20100722_weekly … hfm
–> 201007_monthly … hf(not found)
–> 201006_monthly … hf(not found)
–> 201005_monthly … hf(not found)
Restored 3 distinct versions
[host1:/home/fredshop/public_html/plugins]# ll error*
-rw-r–r– 1 fredshop fredshop 23565 Aug 11 13:12 error_log
-rw-r–r– 1 fredshop fredshop 4713 Jul 21 15:10 error_log.bak_20100722_weekly
-rw-r–r– 1 fredshop fredshop 22305 Jul 27 11:32 error_log.bak_20100729_weekly
-rw-r–r– 1 fredshop fredshop 23145 Aug 3 20:28 error_log.bak_20100811_daily
You’ll note that we did only two things to restore the file – cd to the client directory, and run the restore command. Also note that the original file is NOT overwritten, you have to do a manual cp to actually overwrite the file.
You can see above that although there were a lot of backups available, only 3 different versions of the file existed (the lines marked with “(no change)” were no different from the previous version), and the restored versions would have .bak and the backup name appended; you can see the versions that were restored and the versions that were skipped, either because the file was not in the backup tarball or had not changed (no point in restoring multiple copies of the same file from different backups, just gets confusing).
By default, as above, the command only restores file versions that are unique, although it can be asked to restore all versions with the –a option. Without the –a option, it will also not restore anything at all unless it is different to the file presently on disk. By default, the file on disk is not affected, as the versions are restored into files with extensions of .bak_backupversion. These defaults make it very easy to restore a file to it’s last known good state – simply run cprevert then run the cp command, then rm to delete old versions. [We’ll probably add an option to do all this in one line one day, but not yet]
You’ll note the multiple daily, weekly and monthly backups. The system that this was run on was also running the whmscripts version of postcpbackup, which rotates the backups, keeping a requested number of copies of each of the dailies, weeklies and monthlies – allowing you to build up a good history over time.Download cprevert.sh [1023 downloads]
This script presently requires the findbackup script: Download findbackup.sh [909 downloads]
Although the script is free, please post in the blog and/or email in your comments and thoughts – that’s what motivates us to keep sharing!
We’re considering adding options that grep, list and reverse grep files, for instance, to restore the first uninfected version of a file, and to allow single command rollback of a file.
-p rollback file to previous version; run again to rollback further. Original is saved.
-G string restore first version of file NOT match string (eg: first uninfected version)
-g string restore first version of the file that does match string
-l list only, don’t restore