Here’s a cool cron script I wrote to backup your Gerrit instance to a shared folder:
Some requirements and assumptions:
- You’ll need 7z available on the path.
- Edit the share location and the username and password.
- Keeps the past 30 days of all back-ups on the server.
- Keeps the past 14 days of MySQL backups on the share. Edit the corresponding value in the script to change as you need it.
- Keeps the past 7 days of Gerrit backups on the share. Edit the corresponding value in the script to change as you need it.
- MySQL is used for Gerrit’s datastore You can remove those sections if you’re not using it. Just make sure you’re backing up the datastore!
#!/bin/bash
# Backup the mysql databases
mysqldump-secure --cron
# Remove old backups
find /var/backups/mysql/* -mtime +30 -exec rm {} \;
#Gerrit Home Directory Filename
filename="gerrit-home-$(date '+%F-%H%M').tar.7z"
echo "Starting backup of gerrit home directory..."
#Perform Backup of the gerrit home directory.
tar -cf - /home/gerrit | 7z a -si "/var/backups/gerrit/$filename"
echo "Backup of home directory completed!"
echo "Removing old backups..."
#Remove old backups
find /var/backups/gerrit/* -mtime +30 -exec rm {} \;
echo "Completed removing old backups."
#make the share folder incase it's gone.
if [ ! -d "/mnt/backup-share" ]; then
mkdir /mnt/backup-share
fi
#mount the CIFS share for the backup location
mount -t cifs -o username=rodsqladmin,password=*r6fE?eW //GVLVIDFSCS02P/MGardner$ /mnt/backup-share/
#make the share folder incase it's gone.
if [ ! -d "/tmp/tobackup" ]; then
mkdir /tmp/tobackup
fi
if [ ! -d "/tmp/tobackup/mysql" ]; then
mkdir /tmp/tobackup/mysql/
fi
if [ ! -d "/tmp/tobackup/gerrit" ]; then
mkdir /tmp/tobackup/gerrit/
fi
echo "Preparing backups for copying..."
find /var/backups/mysql -type f -mtime -14 > /tmp/tobackup/mysql
find /var/backups/gerrit -type f -mtime -7 > /tmp/tobackup/gerrit
echo "Copying backups..."
#Rsync the backups
rsync -a /tmp/tobackup/mysql/ /mnt/backup-share/mysql
rsync -a /tmp/tobackup/gerrit/ /mnt/backup-share/gerrit
echo "Copying complete!"
echo "Performing final cleanup..."
find /mnt/backup-share/mysql/* -mtime +14 -exec rm {} \;
find /mnt/backup-share/gerrit/* -mtime +7 -exec rm {} \;
rm -rf /tmp/tobackup
echo "Cleanup complete!"
echo "Unmounting share and wrapping up!"
#unmount the share
umount /mnt/backup-share
echo "Backup complete!"
I’ve placed this script in my /etc/cron.daily folder to have it run once a day.