Backup Script to FTP with Timed Subfolders – Bash Scripting

backupbashftpscripting

I want to make a backup script, that makes a .tar.gz of a folder I define, say fx /root/tekkit/world

This .tar.gz file should then be uploaded to a FTP server, named by the time it was uploaded, for example: 07-10-2012-13-00.tar.gz

How should such backup script be written?

I already figured out the .tar.gz part – just need the naming and the uploading to FTP.

I know that FTP is not the most secure way to do it, but as it is non-sensitive data, and FTP is the only option I have, it will do.

Edit:

I ended up with this script:

#!/bin/bash

# have some path predefined for backup unless one is provided as first argument
BACKUP_DIR="/root/tekkit/world/"
TMP_DIR="/tmp/tekkitbackup/"
FINISH_DIR="/tmp/tekkitfinished/"
# construct name for our archive
TIME=$(date +%d-%m-%Y-%H-%M)

if [ $1 ]; then
    BACKUP_DIR="$1"
fi

echo "Backing up dir ... $BACKUP_DIR"
mkdir $TMP_DIR
cp -R $BACKUP_DIR $TMP_DIR
cd $FINISH_DIR
tar czvfp tekkit-$TIME.tar.gz -C $TMP_DIR .

# create upload script for lftp
cat <<EOF> lftp.upload.script
open server
user user password
lcd $FINISH_DIR
mput tekkit-$TIME.tar.gz
exit
EOF

# start backup using lftp and script we created; if all went well print simple message and clean up 
lftp -f lftp.upload.script && ( echo Upload successfull ; rm lftp.upload.script )

Best Answer

First you create cron job that is invoked at time you want to have backup created after backup is created script will generate lftp script to upload file via FTP.

#!/bin/bash

# have some path predefined for backup unless one is provided as first argument
BACKUP_DIR="/tmp/test"
# construct name for our archive
TIME=$(date +%d-%m-%Y-%H-%M)

if [ $1 ]; then
    BACKUP_DIR="$1"
fi



echo "Backing up dir ... $BACKUP_DIR"
tar czvfp $TIME.tar.gz $BACKUP_DIR

# create upload script for lftp
cat <<EOF> lftp.upload.script
open ftp.server.tld
user username password
cd /target/directory
lcd /local/dir/where/tar/gz/archive/is
mput $TIME.tar.gz
exit
EOF

# start backup using lftp and script we created; if all went well print simple message and clean up 
lftp -f lftp.upload.script && ( echo Upload successfull ; rm lftp.upload.script )