#!/bin/bash BASEDIR=/home/git/gitlab DESTDIR=/home/silence/backups/gitlab SRCDIR=$BASEDIR/tmp/backups LOGFILE=$DESTDIR/gitback.log #####begin backups##### ts_format=`date +%Y-%m-%d %H:%M:%S` timestamp=`date +%s` test -f $LOGFILE || touch $LOGFILE echo "" >> $LOGFILE echo "" >> $LOGFILE echo "BEGIN BACKUP @ $ts_format" >> $LOGFILE ######backup git####### cd $BASEDIR && sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production > /dev/null 2>&1 if [ $? -eq 0 ];then echo "backup successfully" >> $LOGFILE else echo "backup failed. Errno: $?" >> $LOGFILE fi ######clean git diskspace,keep 5days data###### file_counts=`ls $SRCDIR |wc -w` if [ $file_counts -gt 5 ]; then rmlist=`ls -ltr $SRCDIR |grep -v ^total |head -n -5 |awk '{print $9}'` for rfile in $rmlist;do echo "remove $SRCDIR/$rfile to clean five days ago backupfiles" >> $LOGFILE rm $SRCDIR/$rfile done fi ######clean store dir space ,keep 3days data###### file_counts=`ls $DESTDIR |grep .tar |wc -w` if [ $file_counts -gt 2 ]; then rmlist=`ls -ltr $DESTDIR |grep .tar |head -n -2 |awk '{print $9}'` for rfile in $rmlist;do echo "remove $DESTDIR/$rfile to clean 3 days ago backupfiles" >> $LOGFILE rm $DESTDIR/$rfile done fi ######cp the file ####### targetfile=$SRCDIR/`ls -ltr $SRCDIR | tail -n 1|awk '{print $9}'` cp -af $targetfile $DESTDIR && echo "copy the file to destination directory successfully!" >> $LOGFILE exit 0