Skip to content

Instantly share code, notes, and snippets.

@dickolsson
Last active July 14, 2025 22:10
Show Gist options
  • Save dickolsson/d86d1058f67da7fcd9c8 to your computer and use it in GitHub Desktop.
Save dickolsson/d86d1058f67da7fcd9c8 to your computer and use it in GitHub Desktop.
Simple backup script that will backup package list, files and databases.
#!/bin/sh
#
# Simple backup script that will backup package list, files and databases.
#
# For example, run daily keeping backups for a week on external mount:
#
# $ crontab -e
# 0 0 * * * /usr/local/bin/backup.sh /mnt/backups 7 > /dev/null 2>&1
BACKUPDIR=${1:-/var/backups}
DAYSTOKEEP=${2:-3}
INCLUDEFILE=${3:-/usr/local/etc/backup-include.txt}
DATE=$(date +%s)
# Create the backup directory and file list.
if [ ! -d $BACKUPDIR ]; then
mkdir -p $BACKUPDIR
chmod 711 $BACKUPDIR
fi
if [ ! -f $INCLUDEFILE ]; then
printf "/etc/\n/home/\n/root/\n/srv/\n/usr/local/\n/usr/share/webapps/\n/var/log/\n" > $INCLUDEFILE
fi
# Backup package list, files and databases.
umask 077;
pacman -Qqe | grep -v "$(pacman -Qqm)" | gzip > $BACKUPDIR/pkg-$DATE.txt.gz
tar --files-from=$INCLUDEFILE -czpvf $BACKUPDIR/fs-$DATE.tar.gz
if command -v mysqldump >/dev/null 2>&1; then
mysqldump --all-databases | gzip > $BACKUPDIR/db-$DATE.sql.gz
fi
# Remove old backups.
find $BACKUPDIR/* -mtime +$DAYSTOKEEP -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment