I’ve written a backup script in order to get my WordPress blog backup automatically. The script is only usable on a Linux/Unix box, since it uses default GNU tools.
The script connects to the server via ssh, copies a folder to a location, dumps a database to the same place with the copied folder, creates a tar.gz out of it, then gets the new file via ftp to a prefered location.
The important thing here is that, you should add your ssh public key to the server so that ssh will connect automatically. I also use .my.cnf files to login mysql without specifying password, so you’d better do that. I’ve talked about it in an earlier post here.
Keep in mind that you need an ftp client to connect. If you don’t have it, install it using yum, apt or whatever.
#!/bin/bash ### START OF EDIT THESE ### ############################ HOST='192.168.1.1' # ip address of your server SSHUser='root' # user to connect as ssh FTPUser='myfunkyftpusername' # user to connect as ftp FTPPass='mysupersecretFTPpassword!' # ftp connection password MYSQLUser='root' # # user to connect as MySQL SSHPort=22 # change if different FTPPort=21 # change if different DB=wordpress # which database to backup? DIRECTORY='/home/eaydin/public_html/wp-content' # directory to back up - server side DIRWRITE='/home/eaydin/' # move the backup here on the server. DROPBOX='/home/eaydin/Dropbox' # local file path to backup - host side. use your Dropbox folder? FILENAME='wp-backup' # Filename to use for backups ### END OF EDIT THESE ### ######################### DIRWRITE=${DIRWRITE%/} # remove trailing / from dir name. FILENAME=${FILENAME%/} # remove trailing / from filename in case the user types it. DATE=`eval date +%d%m%Y"-"%H%M` # create date format. (created on the host side, not server. depends on the host time setings.) FILETAR=$FILENAME-$DATE.tar.gz # name of the tar.gz file (not path!) ssh -t $SSHUser@$HOST -p $SSHPort "\ cp -R $DIRECTORY $DIRWRITE/$FILENAME-$DATE ;\ mysqldump --add-drop-table -u $MYSQLUser $DB > $DIRWRITE/$FILENAME-$DATE/wordpress.sql ;\ tar -cvzf $DIRWRITE/$FILETAR $DIRWRITE/$FILENAME-$DATE ;\ chown $FTPUser:$FTPser $DIRWRITE/$FILETAR ;\ rm -rf $DIRWRITE/$FILENAME-$DATE " ftp -n $HOST $FTPPort <<END_SCRIPT quote USER $FTPUser quote PASS $FTPPass lcd $DROPBOX cd $DIRWRITE binary get $FILETAR quit END_SCRIPT exit 0
The lines between 5 and 17 are the ones you should edit, they’re all self explained in the comments.
It’s a good idea to add the script to your crontab.
In order to do it, especially on Ubuntu systems, just add your current PATH value right below the /bin/sh line. Like this,
eaydin@eaVT:~$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So you should add this line at the top of the script,
#!/bin/env bash PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Or, just run the script whenever you want. I usually set the download path (defined as the $DROPBOX variable on line 16) to my Dropbox folder, this way my backups get automatically synced on the Dropbox server.