How to backup a remote site from your linux server/computer
This is a little script I’ve wrote to backup some of the remote sites I have on other servers.
This script will ask you for input but if you want to use this as a cron job, just define the variables with your server information.
All you need is the YAFC ftp client to download the files. You can, of course replace the part where I used that specific client with code that uses your own preferred client. YAFC is great for the job because of it’s ability to download folders recursively.
You can get YAFC at http://yafc.sourceforge.net/ or if you use an Ubuntu or Debian distribution just
sudo apt-get install yafc
.
#!/bin/bash
# ======= BACKUP PROCEDURE ======
echo -n "Server address: "
read SERVER
echo -n "Username: "
read USERNAME
echo -n "Password: "
read PASSWORD
echo -n "Remote folder: "
read RFOLDER
# get current date to use as a name for the backup
DATE=$(/bin/date +%Y%m%d)
# folder on local machine where the backups should be put
FOLDER="/media/storage/#BACKUPS/${SERVER}/"
# append the date to the folder name
BACKUP=${FOLDER}${DATE}
# create the backup folder in case it doesn't exist
mkdir $FOLDER
# create the current backup folder
mkdir $BACKUP
# we use the yafc ftp client to download the files from the remote server recursively
yafc <<**
open ftp://$USERNAME:$PASSWORD@$SERVER/
cd $RFOLDER
get -f -r -o $BACKUP *
close
**
# if you wish to backup a remote database too and put it in the current backup folder, uncomment the next line and replace {dbUser},{password},{dbName},{host} with your database information
#mysqldump -u {dbUser} -p{password} -h {host} {dbName} > ${BACKUP}/${DATE}.sql
# next we archive the current backup folder to take less space
tar -czf ${FOLDER}${DATE}.tgz $BACKUP
# we delete the backup folder since we have the archive now
rm -rf $BACKUP
# ====== END ======
