0

Can anyone tell me how to create a backup script for Ubuntu VPS which is used as a webserver.

<p>I also want to add it as a cron job so that it runs automatically every day midnight at <strong>12:00AM</strong></p>

Programs which i use in my VPS :

  • Apache2
  • php5
  • MySQL
  • phpMyAdmin
  • sites-enabled
  • SSL

Thanks in advance!

1 Answers1

1

Look into using rsync to perform the actual backup to your other server:

man rsync

You will need to know exactly what data you would like to backup, though. Just from the list of programs (if you are using the vanilla directories), you will want to at least backup:

  • /var/lib/apache2
  • /var/log/apache2
  • /var/www
  • /etc/apache2
  • /var/lib/mysql

For scheduling the backup process, you can look into crontab:

man crontab

Tips:

  • Try to isolate the backup process to a specific user, then give it read access to the backup directories. I would highly recommend not using the root user to backup your data.
  • Make sure that your backup script is executable (chmod +x /my/backupscript)
  • rsync runs over SSH, so whatever is initiating the backup will need SSH access (man ssh-keygen).
  • Depending on what kind of data you are hosting, you will want to make sure and stop all processes that are using the backup directories. I would definitely recommend stopping MySQL at a minimum (service mysqld stop).
  • It's more efficient to make a local, compressed (man tar) backup of the data (for example, to /backups/mydata_$(date +%s).tgz), then rsync the local backup to your other server. Actively rsyncing the actual directories could lead to extended downtime on your db as your dataset grows.
Ross
  • 1,150
  • 7
  • 7
  • can i email the backups as an attachment after every backup? – Midhun Varghese Mar 05 '14 at 14:54
  • 1
    @MidhunVarghese do you mean email the actual backup? If it's small enough in size, then you probably could. If you want your server to send email, you can find a simple overview here. – Ross Mar 06 '14 at 15:32