The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can you restore only a particular file?

Discussion in 'Data Protection' started by judosensei, Apr 27, 2009.

  1. judosensei

    judosensei Member

    Joined:
    May 31, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Arlington VA
    I have one table in mySQL that is corrupt and I'd like to restore it from the daily backup. Can it be done in WHM or cPanel, or should I restore the entire database? Can anyone give me easy instructions? It looks like in WHM I have to restore an entire account. Thanks.

    Also, is there a way to restore from other than the last daily backup. My last backup has the same problem as my present database, so I'd like to go one day further back. I guess I don't know how to work with backups and the WHM/cPanel choices seem very limited.
     
    #1 judosensei, Apr 27, 2009
    Last edited: Apr 27, 2009
  2. eth00

    eth00 Well-Known Member
    PartnerNOC

    Joined:
    Mar 30, 2003
    Messages:
    723
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    NC
    cPanel Access Level:
    Root Administrator
    If you want to restore only specific tables you would have to do so manually, cPanel only lets you do an entire DB. You would have to go to the backup on the hard drive, restore it as a different DB, then dump the data you want.

    cPanel has daily, weekly, and monthly options. If you have one of the other two enabled you can restore those backups. cPanel will not let you roll back to a backup 2 days only, without custom scripting you have only those options for backups.
     
  3. judosensei

    judosensei Member

    Joined:
    May 31, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Arlington VA
    Thanks, but that is too bad. I tried to restore from the daily backup but it was created after the problem was created so it is no good. I then restored from the weekly backup, but the mysql database was not restored properly and all the messages in my forum were still missing. I then tried to restore from the weekly backup and the server screeched to a halt. All services failed and it has been unresponsive for the past 6 hours.

    This is the first time I have needed a backup and WHM seems inadequate as a protection for loss of data in a database.
     
  4. eth00

    eth00 Well-Known Member
    PartnerNOC

    Joined:
    Mar 30, 2003
    Messages:
    723
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    NC
    cPanel Access Level:
    Root Administrator
    Sounds like something else is going on, a restore should never crash a server.

    MySQL backups are done by a normal dump so as long as the dump works ok you should be fine. Since it crashed I wonder if you have somethnig going on with the hardware/OS.
     
  5. judosensei

    judosensei Member

    Joined:
    May 31, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Arlington VA
    Thanks. There was a firewall issue and God knows what else. I've got everything working again though. I'm going to try this for backups of the database (found in another thread).

    ___________________________
    The guide is written for people who have Linux Webhosts with a CPanel interface, including access to CRON.

    I assume that you know how to create folders, upload files, and CHMOD them on your webserver.

    My host has set up my hosting account so my webspace is in the /home/<accountname>/public_html folder.
    So I have created a /home/<accountname>/backup folder. This is where I have my backup scripts and where the backup file is created. The folder is CHMOD 755.

    In the /home/<accountname>/backup folder I have a file called wbackup.crn It is an ASCII text file, CHMOD 755 to allow it to be executed. It contains Linux 'command line' instructions that may not be familiar to people who only access their webspace through browsers. These are the contents of the file:

    CODE
    rename .php .bak /home/<accountname>/public_html/forums/index.php
    rename down index /home/<accountname>/public_html/forums/down.php
    #
    rename .php .bak /home/<accountname>/public_html/index.php
    rename down index /home/<accountname>/public_html/down.php
    #
    mysqldump --user=<accountname> --password=<password> --opt <dbasename> | gzip > /home/<accountname>/backup/dbsitebackupw_`date +%Y_%m_%d`.gz
    #
    tar -cf /home/<accountname>/backup/tarbackupw_`date +%Y_%m_%d`.tar `find /home/<accountname>/public_html/forums/uploads/ -type f -mtime -7`
    #
    gzip /home/<accountname>/backup/tarbackupw_`date +%Y_%m_%d`.tar
    #
    rename index down /home/<accountname>/public_html/index.php
    rename .bak .php /home/<accountname>/public_html/index.bak
    #
    rename index down /home/<accountname>/public_html/forums/index.php
    rename .bak .php /home/<accountname>/public_html/forums/index.bak
    #
    exit


    The # lines are comments to make the file more readable. Let's look at what these lines do.

    CODE
    rename .php .bak /home/<accountname>/public_html/forums/index.php
    rename down index /home/<accountname>/public_html/forums/down.php


    Here I rename the FORUMS home page index.php to index.bak, and a file of my own (down.php) is renamed to index.php. down.php is a small HTML file that tells the user that the boards are temporarily down while the database is backed up. I like to do this to prevent any users from being able to read or write the database. Even taking the board offline allows database reads, so I prefer this technique.

    CODE
    rename .php .bak /home/<accountname>/public_html/index.php
    rename down index /home/<accountname>/public_html/down.php


    Here I do the exact same thing in the public_html folder to the SITE home page. This is because the site home page has various database driven features that I want disabling while I run the backup.

    CODE
    mysqldump --user=<accountname> --password=<password> --opt <dbasename> | gzip > /home/<accountname>/backup/dbsitebackupw_`date +%Y_%m_%d`.gz


    This line is the command to backup the database using mysqldump, and then 'pipe' the backup into a file through gzip. This produces a compressed zip file called /home/<accountname>/backup/dbsitebackupw_`date +%Y_%m_%d`.gz .

    There are 2 important things here. The backup file is put in the backup folder and so is unreachable by typing a URL in your browser. This makes things a bit more secure. The other thing is the filename. The `date +%Y_%m_%d` command means that the file has a datestamp built into its name thereby making the filename unique, providing that backups are not run more than once per day!

    CODE
    tar -cf /home/<accountname>/backup/tarbackupw_`date +%Y_%m_%d`.tar `find /home/<accountname>/public_html/forums/uploads/ -type f -mtime -7`
    #
    gzip /home/<accountname>/backup/tarbackupw_`date +%Y_%m_%d`.tar


    Here a 'tarball' is created of all files in the upload folder that have changed in the last 7 days. A 'tarball' is essentially one file that contains several files all scrunched together, so the gzip line then zips this file to compress it. Again a datestamp is included in the file name to make it unique.

    CODE
    rename index down /home/<accountname>/public_html/index.php
    rename .bak .php /home/<accountname>/public_html/index.bak
    #
    rename index down /home/<accountname>/public_html/forums/index.php
    rename .bak .php /home/<accountname>/public_html/forums/index.bak
    #
    exit


    These lines rename everything back the other way, reversing what was done at the start of the script. When these commands have completed the site and forums will be back online.

    The exit command tells CRON that the script is finished.

    So now I have my backup script file I need to find a way to make sure it gets run every week. For this, I use the CPanel CRON feature. CRON is a scheduling tool. It runs all the time on the server and looks to see whether there are any scripts to be run. It runs them at specified times that it finds in the CRONtab. The CPanel interface allows you to add and delete scripts to the CRONtab. On my CPanel it is in the Advanced section.

    Clicking on the CPanel Cron jobs link, I am asked whether I want to use Standard Mode or Advanced Mode. I choose Standard Mode.

    In the text box labelled 'command to run:' I type '/home/<accountname>/backup/wbackup.crn' This is the full path and filename to the script above. Then, using the lists that are there I choose a day of the week and time for the script to run.

    It's important to choose a time when your site is quiet. It's also important to understand that the time you choose is SERVER time, which may or may not be the same as the time zone that you live in!

    In Cpanel , clicking 'Save Crontab' adds your script to the CRON schedules.

    That's all there is to it. You now have a backup script scheduled to run on your server that creates a couple of zipped files for you to download later. Even better, it runs regularly without you needing to do anything at all. At your leisure, you can download the backups from the server to your PC.

    I have made this script deliberately simple. There's all sorts of extras you could implement like emailing the zip files to you, FTPing them to another server, emailing you if the script succeeds (you normally get an email if it fails!)

    Notes:
    1. To make this script work for you, you need to change anything in <> brackets to the correct names for your server. You should also check that the file paths that I use in these examples are adjusted to suit your own.
    2. To produce your own .crn files, you can use any editor that allows you to save in text format. The .crn file extension is not mandatory, but recommended.
     
  6. clark40

    clark40 Registered

    Joined:
    Apr 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    hello every body

    Here I do the exact same thing in the public_html folder to the SITE home page. This is because the site home page has various database driven features that I want disabling while I run the backup.
     
  7. AzJohnny

    AzJohnny Registered

    Joined:
    Apr 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I am trying to find a backup prior to the date of my last monthly backup...is that possible?
     
Loading...

Share This Page