The Personal Blog of a Geek in Arizona

Menu

Search

Backing up, Setting up, and Migrating Linux Webservers

Linux webservers like Ubuntu (and CentOS, etc) can sometimes be confusing for new admins who aren’t sure how to do proper backups or migrate websites and databases between servers. It takes awhile, but you eventually get a toolkit of scripts, commands, or techniques that work for you. If you’re lucky, you actually write them down!

Here are mine.

This is my install command, for prepping an Ubuntu server to be an Apache webserver with MySQL and PHP. Git is in there for version control which you should be using if you’re a coder at all. Add vsftpd to this list if for some horrible reason you need FTP (protip: use SCP instead.)

For the root user, create a ~/.my.cnf file (as root) with the following content:[client]
password=PASSWORD

Note that USER, PASSWORD, and DBNAME should be replaced appropriately. Formerly the crontab script included a -pPASSWORD option (without a space) but this is less secure and reliable than the .my.cnf method.

Make sure you create /backups and that it is writeable by root (or whoever the cron job runs as.) Test the command before putting it in your crontab.

You’ll see that I’m doing a mysqldump of the database named ‘mysql’ — this is intentional as it is the database that holds configuration information including database users which you’ll probably want during a restoration.

Also, I’m doing a backup of /etc which is where most configuration files are stored. Half of a website is how it’s configured, so you’ll definitely want that during a restoration.

This should take care of most Apache/MySQL websites as long as all websites are stored in /var/www — if not, modify the script as needed.

Finally, it would be a good idea to encrypt these files or at least make sure they’re chmod 700 — readable only by the folder owner and nobody else — because they will invariably contain passwords in the config files and mysql tables. Your mileage may vary and it’s your responsibility to make sure you’re adequately secured.

To copy the backups to another server and extract the www backup, use this (from the new server):