Smart backup script for linux servers based on rsync, mysqldump and tar
- Rsyncing data to the given backup directory as backup-YYYY-MM-DD folders. Unchanged files between days are hardlinked, so only changed and new files will consume space from disk.
- Creating last_backup link for easy access to the last backup.
- Mysqldumping databases to last_backup/mysql_bkp/ folder as databaseName.sql files.
Edit backup-daily.sh:
Set backup directory:
bkpPath=/backup
Set how long we gonna keep backups, default 30 days:
oldFiles=30
List directories to backup:
bkpData=( "/data" "/var" "/etc" "/home" "/root" )
Create read only backup user for databases and set them:
user=bkpuser
pass=bkpPassword
List databases to backup:
databases=( "firstDatabase" "secondDatabase" "thirdDatabase" )
Set cron to run this backup every day at 1:30 and write success and error logs:
30 1 * * * /root/backup-daily.sh > /root/backup-daily.log 2> /root/backup-daily.err
Forget everything.
- Removing last backup file.
- Creating compressed tar fullserver-YYYY-MM-DD.tar.gz file from almost everything on the server.
- Mounting remote storage.
- Mysqldumping database to remote storage as databaseName_YYYY-MM-DD.sql files.
- Removing oldest backup files from remote storage.
- Copying backup tar file to remote storage.
- Unmounting remote storage.
Edit backup-weekly.sh:
Set backup directory:
bkpPath=/backup
Set how long we gonna keep backups, default 30 days:
oldFiles=60
Create read only backup user for databases and set them:
user=bkpuser
pass=bkpPassword
List databases to backup:
databases=( "firstDatabase" "secondDatabase" "thirdDatabase" )
Add more excludings if needed to tar:
--exclude=/path \
Se your remote mount using method suitable for you:
sshfs [email protected]:/backup $bkpPath/remote-backup
Create read only backup user for databases and set them:
user=bkpuser
pass=bkpPassword
List databases to backup:
databases=( "firstDatabase" "secondDatabase" "thirdDatabase" )
Set cron to run this backup every sunday at 23:30 and write success and error logs:
30 23 * * 7 /root/backup-weekly.sh > /root/backup-weekly.log 2> /root/backup-weekly.err
Forget everything. Again.