Skip to content

Instantly share code, notes, and snippets.

@pacmandv
Forked from alfredfox/cleanup.sh
Created June 7, 2017 14:48
Show Gist options
  • Save pacmandv/fd5d334a535699c659363e8568d96978 to your computer and use it in GitHub Desktop.
Save pacmandv/fd5d334a535699c659363e8568d96978 to your computer and use it in GitHub Desktop.
PHP script to backup DB, zip web directory and backup .htaccess file. 4 complete backups
#! /bin/bash
# Enter directory
cd "backups/"
# Count Files in dir
FILES=$(ls | wc -l)
# If FILES are more than 12 remove sort by oldest
while [ $FILES -gt 12 ]; do
ls -tr | head -n 1 | sed "s/ /\\ /g" | xargs rm -f
FILES=$(ls | wc -l)
done
<?
////////////////////////////////////////
//
// Let's make some automatic backups yo!
// Courtesy of Alfred Fox
// Woot!
//
////////////////////////////////////////
// Setup Database connection information
$username="********";
$password="********";
$database="********";
mysql_connect(localhost,$username,$password);
// Connect to Database
@mysql_select_db($database) or die("Unable to select database");
// Setup Database Dump
$backupFile = 'backups/'.$database . date("Y-m-d-H-i-s") . '.sql';
$mysqlDump = "mysqldump --opt -h localhost -u $username -p$password $database > $backupFile";
system($mysqlDump);
// Close Database Connection
mysql_close();
// Create zip file and move it to the backup directory then run cleanup.sh which keeps the number of backups at 12 (4 complete backups)
$zip = "cd ../public_html/ && zip -r backup_".date("Y-m-d-H-i-s").".zip ./* -x '/home/mattwolf/public_html/backup/*' && mv backup_*.zip ../backup/backups/ && cp .htaccess ../backup/backups/htaccess_".date("Y-m-d-H-i-s")." && cd /home/mattwolf/backup/ && ./cleanup.sh";
system($zip);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment