Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 )
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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 )
share|improve this answer
    
Thanks - your script was what got me onto the right track. –  Frederik Nielsen Oct 7 '12 at 17:53

As simple as:

DATE=`date +%d-%m-%Y-%H-%M`
tar cfz ${DATE}.tar.gz /root/tekkit/world

Or, with less typing:

tar cfz $(date +%d-%m-%Y-%H-%M).tar.gz /root/tekkit/world

What happens?

$ DATE=`date +%d-%m-%Y-%H-%M`
$ echo $DATE
07-10-2012-13-15
share|improve this answer
    
Good answer, but misses the FTP upload part. –  Frederik Nielsen Oct 7 '12 at 14:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.