Skip to main content

Shell script to backup web sites, MySQL, and PostgreSQL databases.

# #############################################################################
#
# Linux shell script to backup websites, MySQL, and PostgreSQL databases
#
# How to make a Linux shell script that backs up your websites and databases.
#
# http://lassebunk.dk/2011/03/11/linux-backup-script/
#
# - Backup websites
# - Backup MySQL databases
# - Backup PostgreSQL databases
# - Delete old backups to preserve disk space
#
#   Arguments:
# -----------------------------------------------
#     -d to specify destination
#     -s to specify website to backup
#     -m to specify MySQL database to backup
#     -p to specify PostgreSQL database to backup
#
# #############################################################################

##
# get arguments
##
while [ "$1" != "" ]; do
    case $1 in
        -d | --destination )    shift
                                dest=$1
                                ;;
        -s | --site )           shift
                                site=$1
                                ;;
        -m | --mysql )          shift
                                mysqldb=$1
                                ;;
        -p | --postgres )       shift
                                postgresdb=$1
                                ;;
        * )                     echo "Unknown argument $1."
                                exit 1
    esac
    shift
done

##
# complain if no destination specified
##
if [ "$dest" == "" ]; then
  echo "Missing destination."
  exit 1
fi

##
# set and create backup folder
##
folder="$dest/`date +%Y%m%d%H%M%S`"
mkdir -p $folder

##
# backup website
##
if [ "$site" != "" ]; then
  echo "Backing up site $site to $folder/www.tar.gz..."
  cd /var/www
  tar -czf $folder/www.tar.gz $site
fi

##
# backup mysql database if specified
#
# assumes database has a user with the same name, and logs in with this user.
##
if [ "$mysqldb" != "" ]; then
  echo "Backing up database $mysqldb to $folder/db.sql.gz..."
  mysqldump -u $mysqldb $mysqldb | gzip > $folder/db.sql.gz
fi

##
# backup postgres database
#
# assumes database has a user with the same name, and logs in with this user.
##
if [ "$postgresdb" != "" ]; then
  echo "Backing up database $postgresdb to $folder/db.sql.gz..."
  pg_dump -U $postgresdb $postgresdb | gzip > $folder/db.sql.gz
fi

##
# delete old backups keeping last 5
##
echo "Deleting old backups..."
cd $dest
ls -t | sed 1,5d | while read folder; do rm -r $folder; done

##
# done
##
echo "Done!"

# My crontab -e is then:

# m h  dom mon dow   command

# lassebunk
0 2 * * * /var/scripts/backup.sh -d /var/backups/sites/lassebunk/daily -s lassebunk -m lassebunk
0 2 1 * * /var/scripts/backup.sh -d /var/backups/sites/lassebunk/monthly -s lassebunk -m lassebunk

# othersite
0 2 * * * /var/scripts/backup.sh -d /var/backups/sites/othersite/daily -s othersite -p othersite
0 2 1 * * /var/scripts/backup.sh -d /var/backups/sites/othersite/monthly -s othersite -p othersite