03-15-16 Update: The backup script now supports encryption. Make sure to download the latest version here.

So you think your web host makes backup copies of your website regularly? Think again. While most web hosting companies do backup their servers on a daily or weekly basis, you don’t have access to these backups should you need to restore some or all the files from your website. These backups are only meant to restore an entire server should it encounter some type of failure.

For those static websites that don’t change often, the manual backup utility provided with the hosting control panel should do the job fine. However, if you host a dynamic website with a database back end, you want to have daily backups of your website taken automatically.

So how can you backup your website automatically? Well if you’re not on a budget, some web hosting companies offer an optional backup service. This is usually the best option as it uses an agent which performs incremental backups of your data. This allows you to restore any file from your website at any given point in time.

Another option is to use third-party backup services, such as CodeGuard andBackupMachine, to perform backups of your website remotely. Pricing usually starts around $5 per month but this can increase quickly if your website uses a lot of disk space.

Finally, if you don’t have the necessary budget to spend on a website backup service, here’s a simple script that allows you to perform an FTP backup of your website automatically.

Free FTP Backup Script

I wrote a small Perl script which requires the Net::FTP and Net::SFTP libraries in order to work properly. The script creates an archive of your home directory and an SQL dump file of your database(s). The two files are then archived into a single, compressed file and sent to a remote server over FTP or SFTP.

Here’s how to use it:

  1. Download the backup script to your computer and rename it to “ftp-backup-script.pl”.
  2. Open the backup script using your favourite text editor. I suggest you have a look at Notepad++ if you don’t have one.
  3. Here are the parameters you need to configure:
    • $password: Specify the password you want to use to encrypt your backup. Leave empty do disable encryption (not recommended).
    • $delete_backup: Specifies if the local backup copy should be deleted after being uploaded to the remote server (0 = no, 1 = yes).
    • $homedir: The full path to the root folder of your website (or any other folder you wish to backup).
    • $backup_dest_dir: The full path to the local backup directory.
    • $ftp_backup: Set to “0” to disable, “1” to enable FTP/SFTP backup.
    • $use_sftp: Set to 1 to enable SFTP transfer (recommended). Requires Net::SFTP to be installed.
    • $ftp_host: Hostname of the FTP server to upload the backup to.
    • $ftp_port: Port for the FTP or SFTP connexion. The default FTP port is 21 and the default SFTP port is 22.
    • $ftp_user: FTP/SFTP username.
    • $ftp_pwd: FTP/SFTP password.
    • $ftp_dir: The path to upload the backup file to on the remote server.
    • $ftp_debug: Set to 1 to enable FTP/SFTP debug messages.
    • $dbhost: The MySQL database host. This is usually set to “localhost” but contact your web hosting provider if you’re unsure about this.
    • $dbuser: The MySQL user account required to access the database server. I recommend you create a MySQL user account with read-only access. If you don’t know how to do this, read this tutorial.
    • $dbpwd: The password for the MySQL user account.
    • $backup_all_databases: If set to “1”, the script will backup every database it can read. Set this option to “0” if you want to backup only some databases or no databases at all.
    • $database_names: Specify the name of the database(s) to backup. Leave empty to skip MySQL backup. This parameter is effective only when $backup_all_databases is set to “0”.
  4. Save the backup script and upload it to your hosting account. I recommend you upload the script to a folder that is not publicly accessible since it contains sensitive information.
  5. Change the script’s permissions to “0755” to allow its execution. You can do this using an FTP client:
    ftp file permissions
    file attributes
  6. Alternatively, you can use this command to change the permissions if you have a shell access:
    # chmod 0755 /path/to/ftp-backup-script.pl
  7. In order for the script to be executed automatically, you will need to create a “cron” task (or a scheduled task). If you are using cPanel, click the “Cron jobs” icon from the control panel’s main page.
  8. Entering the cron job details as follow will run the backup script every day at midnight. Feel free to adjust this to your needs.

    cron job ftp backup

  9. That’s it!

In some cases, depending on the size of your website, this process can take a lot of time and resources. Be careful not to have your hosting account suspended for overloading the web server.

 

source: https://www.webhostinghero.com/ftp-backup-script-for-websites-and-mysql/