#!/bin/bash
DATE=`date +%F`
git_name=`ls -l /opt/gitlab_bak/*.tar -tr | tail -1 | awk -F"/" '{print $NF}'`

gitlab-rake gitlab:backup:create
if [ $? = 0 ];then
  echo "$DATE:$git_name:successful" >> /opt/gitlab_bak/log/backup.log
else 
  echo "!!$DATE:Backup failed,please check!!" >> /opt/gitlab_bak/log/backup.log
  exit 99
fi

git_dir_1=`ls -l /opt/gitlab_bak/*.tar | awk '{print $NF}' | head -1`
git_dir=`ls -l /opt/gitlab_bak/*.tar | awk '{print $NF}' | wc -l`
if [ "$git_dir" -gt 5 ];then
  rm -rf $git_dir_1
fi
#find /opt/gitlab_bak/ -type f -mtime +5 | xargs rm -rf
