Get our free book (in Spanish or English) on rainwater now - To Catch the Rain.

Appropedia:Admin Tasks/Automatic backups

From Appropedia
Jump to: navigation, search

Domain Backup[edit]

From the home directory,

cd ~/

make these two directories:

mkdir backups
mkdir backups/archives

Create the following bash program

vi ~/backups/

Here are the contents:

suffix=$(date +%Y%m%d)
cd /home/lonny1/.snapshot/hourly.0/
tar -cf /home/lonny1/backups/archives/appropedia.$suffix.tar
gzip /home/lonny1/backups/archives/appropedia.$suffix.tar

Change the rights.

chmod 755 ~/backups/

Line by line explanation[edit]

  1. Defines a Bash script.
  2. Sets up the date structure to append to the file name. Use the command "man date" to learn different time stamps.
  3. Change to Dreamhost's backup directory. See below for more info.
  4. Creates the tar file. the flags "-cf" tell the program tar to Create Forcefully an archive. The first path /home/...$suffix.tar tells it where to create the archive, and the second path tells it what to archive.
  5. Zip it up for exporting.


As of the January 2007, a domain backup took about ten minutes.

Mysql Database Backup[edit]

This script creates a backup of our MySQL database, and places them in the same directory as the domain backups.

Create the following bash program:

vi ~/lonny1/backups/

Here are the contents:

cd /home/lonny1/backups/
mkdir mysql
suffix=$(date +%Y%m%d%Z)
mysqldump --opt -u##### -p##### -h appropedia > mysql/appropedia.$suffix.sql
tar -cf archives/mysql_backup.$suffix.tar mysql/*
gzip archives/mysql_backup.$suffix.tar
# If you would like to email the tar file backup to yourself uncomment this line:
# mutt -a /home/lonny1/backups/archives/mysql_backup.$suffix.tar -s "MySQL Backup"
rm -r mysql/

To add in more databases just copy/paste the mysqldump line underneath itself and fill in the proper information for each database.

Change the rights.

chmod 755 ~/backups/

Line by line explanation[edit]

  1. Defines a Bash script (again)
  2. Changes the current working directory. tar likes relative paths.
  3. Creates a temporary mysql directory for the MySQL backup(s).
  4. Sets up the date structure to append to the file name.
  5. Performs the MySQL dump. More of these can be added for other databases.
  6. Creates the tar file of the database(s) dumped.
  7. Compresses the tar file.
  8. Removes the temporary mysql directory.


As of January 2007, the database backup took around a minute


Edit the cron file to automate the backups at the desired frequency.

Method 1[edit]

crontab -e

Add or edit the following lines, adjusting the times to your liking:

28 1 * * * /home/lonny1/backups/
14 1 * * * /home/lonny1/backups/

Each command needs to be on it's own line.

Method 2[edit]

Create a text file dedicated to importing your commands into crontab. I have noticed that, when using very long commands, the the default editor (I think it was pico or nano at the time) seemed to have problems maintaining the single line integrity required by crontab. I use vi, and saved the file here,


to load it, use crontab as follows.

crontab cron_commands.txt

check for success with

crontab -l

Automatic FTP downloading of archive[edit]

Automate importing the backups to a local Windows machine.

Create the following batch file, named as downloads.bat

ftp  -i -s:ftp.txt

This tells Windows to open up the command line FTP program in non-interactive mode and to access the file, ftp.txt. Pause tells it to leave the window open to view transfer speed and check download success.

Create the following text file, named as ftp.txt

cd backups/
mget archives/
mdelete archives/

Place the two files in the same directory in the local Windows machine and create a Windows Scheduled Task to run download.bat a little while after the shell scripts have been executed.

Manual Execution[edit]

To run the scripts manually, either to test them or to get a more current backup before making significant changes to the sight do the following.

cd ~/backups

See also[edit]

Appropedia's backup plan was mostly devised from here: Dreamhost Automatic Backup

Dreamhost provides a backup plan with their service. It's worth knowing about. Dreamhost:Automated_domain_snapshots (And our domain backup bash script uses it.)