Hesk Automatic backup

Everything related to Hesk - helpdesk software

Moderator: mkoch227

Post Reply
steve
Posts: 147
Joined: Tue Mar 27, 2012 9:32 pm

Hesk Automatic backup

Post by steve »

I run Hesk on my hosting providers servers, so automatic backups were a little challenging. I found two solutions, both of which I have implemented.

For both of these methods you will need to be able to schedule a cron job. My provider uses CPanel which allows me to do this.

Method one - Backup to Server:
  • Open the your CPanel
    Find the Advanced Tab
    Click Cron Jobs
You can set the time and frequency to backup the database. Under the command field, copy and paste the following command

Code: Select all

mysqldump --opt -Q -u dbusername --password=dbpassword dbname | gzip > /path-to-store-the-backup-file/db_backup.sql.gz
  • Replace dbusername with the database user
    Replace dbpassword with the database user password
    Replace dbname with the database that you are backing up
    Replace the path-to-store-the-backup-file to the file path in your server where you want to save the backup
Note: In some server, you might need to put a pair of single quote around the dbpassword for it to work.

Method two - Email yourself the database.
  • Download this PHP Script
    Extract the zip file and open the backup.php with a text editor
    Change the database detail to match your configuration

    Code: Select all

    $dbhost = "localhost"; // usually localhost
    $dbuser = "dbuser"; //enter your database username here
    $dbpass = "dbpass"; //enter your database password here
    $dbname = "dbname"; // enter your database name here
    $sendto = "Send To <mail@mail.com>";
    $sendfrom = "Send From <mail@mail.com>";
    $sendsubject = "Daily Database Backup";
    $bodyofemail = "Here is the daily backup of my database.";
    In the cron job section of the cPanel, enter the following command

Code: Select all

php -q /path-to-the-php-script-folder/backup.php
.htaccess file to prevent any access from the browser. If you want to access from the browser, remove the .htaccess file

Thanks to Damien Oh at maketecheasier.com for these steps.
-Steve
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Re: Hesk Automatic backup

Post by Klemen »

Thanks for sharing. It's indeed a smart thing to backup database (and files) at least once per day.

However, backing up to the same disk doesn't help because in case of disk failure you also loose the backups. Emailing you the backup is much smarter or backing up to a secondary drive/remote server.
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
steve
Posts: 147
Joined: Tue Mar 27, 2012 9:32 pm

Re: Hesk Automatic backup

Post by steve »

For those that are interested, what I have done is used the PHP script & Cron job to upload the backup file to my FTP server. This is great if you have a separate FTP server. If you are like me and you only have the one server you can use the code below to create a *.bat file that will download the file from your FTP server every night.

1) Get the backup working using the above method, have the PHP script dump the file to a directory on your FTP Server. I use a separate directory called backup, you will need to have this file in its own directory for this to work.

2) Open note pad and paste in the following:

Code: Select all

c:\windows\system32\ftp.exe -i -s:c:\backup\ftp-settings.txt

FORFILES /p c:\backup\files\ /m *.gz* /d -10 /c "CMD /C del @file"
Change the directories to suit your needs

The first command calls the ftp-settings.txt file to get the commands to run in the FTP client.

The second command will delete all files in the specified folder that were created 10 days before the date the script is ran, change the "-10" to however many days you want to archive, or just remove it entirely if you want to manage the archive manually.

Save the file as "ftpbackup.bat"

3) Then open a new notepad document and paste the following:

Code: Select all

lcd c:\backup\files
open ftp.yourserver.com
username
password
binary on
mget *
mdelete *
quit
Save this as a txt file and name it ftp-settings-txt, the batch file we created earlier will call on this file for ftp commands.

The above commands to the following.

lcd c:\backup\files will set the directory you want to download to.
open will connect the the server address immediately following, "open ftp.yourserver.com"
Username replace this with the username to your ftp server. Omit the word "username" enter just the username it self "backupuser".
Password same as username, omit the work "Password" just enter the password.
Binary On i have to set the FTP mode to binary to make the download work, you may not have to on your server.
mget * will download all files in the FTP directory.
mdelete * will delete all the files in the FTP directory.
quit will end your FTP session.

4) Create a scheduled task in windows to run the ftpbackup.bat file every night.

To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks.

To schedule a new task:

Double-click Add Scheduled Task to start the Scheduled Task Wizard, and then click Next in the first dialog box.

Click Browse, click the folder and file that you want to schedule, and then click Open.
Type a name for the task, and then choose one of the following options:
Daily
Weekly
Monthly
One time only
When my computer starts (before a user logs on)
When I log on (only after the current user logs on)

Click Next, specify the information about the day and time to run the task, and then click Next.

Type the name and password of the user who is associated with this task. Make sure that you choose a user with sufficient permissions to run the program. By default, the wizard selects the name of the user who is currently logged on.

Click Next, and then click Finish after you verify the choices that you have made.

Your done! If you have completed all steps you should now be automatically downloading cPanel backups at your chosen frequency.
-Steve
Post Reply