scripts/ftp-backup.sh

78 lines
1.8 KiB
Bash
Raw Normal View History

2006-07-27 11:37:56 +02:00
#!/bin/bash
FTP_CREDS="/root/backup/ftpserver.login"
BACKUP_SYSTEM="bin boot dev lib mnt opt sbin usr"
BACKUP_CONFIG="etc"
BACKUP_USER="home root"
BACKUP_VAR="var"
# ------------------
umask 0077
TODAY=`date +%Y-%m-%d`
OLDDAY=`date +%Y-%m-%d -d '-6 months'`
TMPDIR=`mktemp -d`
# ------------------
function create_backup {
echo "## "`date`
target=$1
shift
rm -rf $target
tar --totals -czpf $target -C / $@ 2>&1
/usr/bin/md5sum -b $target
echo
}
function ftp_push_delete {
echo "## "`/bin/date`
target=$1
shift
# workaround -X %s bug in ncftpput (do not use absolute local paths)
pushd . &> /dev/null
cd $TMPDIR
for file in $@; do
files="$files `/usr/bin/basename $file`"
done
/usr/bin/ncftpput -V -X "SITE CHMOD 600 %s" -f $FTP_CREDS $target $files
popd &> /dev/null
# end workaround
echo
}
function ftp_list {
echo "## "`date`
ftp_host=`/usr/bin/awk '/host/{ print $2 }' $FTP_CREDS`
/usr/bin/ncftpls -l -f $FTP_CREDS ftp://${ftp_host}/
echo
}
function ftp_delete {
echo "delete $1"
ftp_host=`/usr/bin/awk '/host/{ print $2 }' $FTP_CREDS`
/usr/bin/ncftpls -l -f $FTP_CREDS -X "DELE $1" ftp://${ftp_host}/ &> /dev/null
}
# ------------------
create_backup $TMPDIR/backup-${TODAY}-system.tar.gz $BACKUP_SYSTEM
create_backup $TMPDIR/backup-${TODAY}-config.tar.gz $BACKUP_CONFIG
create_backup $TMPDIR/backup-${TODAY}-user.tar.gz $BACKUP_USER
create_backup $TMPDIR/backup-${TODAY}-var.tar.gz $BACKUP_VAR
ftp_push_delete / $TMPDIR/backup-${TODAY}-*.tar.gz
ftp_delete backup-${OLDDAY}-system.tar.gz
ftp_delete backup-${OLDDAY}-config.tar.gz
ftp_delete backup-${OLDDAY}-user.tar.gz
ftp_delete backup-${OLDDAY}-var.tar.gz
ftp_list
echo "FTP backup completed."
rm -rf $TMPDIR