Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
codedge committed Jan 8, 2016
1 parent 0777097 commit d85dbd0
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.sublime-project
*.sublime-workspace
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# magento-backup
Shell script for creating Magento backups
# Magento backup
With this shell script you can create backups of your Magento installation, either database or files.
The script is based on [MagePsychos](http://www.blog.magepsycho.com/backup-magento-project-files-db-using-bash-script/) script.


## Requirements / Prerequisites
- bash shell
- installed xmllint >=20901 (http://xmlsoft.org/xmllint.html)
- installed `tar` package
- installed `ftp` package
- make the script executable, i. e. via `chmod +x magento_backup.sh`
- place the script in your <Magento root> folder

## Notes
- Please specify a project name in the _configuration_ section of the [script](magento_backup.sh), see `projectName` variable
- The backup folder will be created in `<Magento root>/var/backups`

## Usage
`./magento-backup.sh -t <database|files|basesystem> -m <1|0> -b <1|0>`

You've got three options how to run the script:
* `-t`: type of backup
* `database`: Only the datase will be backuped as a .sql.gz archive
* `files`: Only Magento files will be backuped as a .tar.gz archive.
The folders var/ and includes/ as well as the .htaccess file are completly ignored and _will not_ be saved. Please see the parameter `-m` for more options.
* `basesystem`: For both files and database a backup will be created and optinally transferred to an ftp server.
* `-m`: skip media files
* `0`: Include all files in the media/ folder, product and category images, picture cache,...
* `1`: Exlude the entire media/ folder from being backuped
* `-b`: backup on external FTP server
* `0`: The backup file will not be transferred to any external ftp server
* `1`: The backup file will be transferred to an ftp server specified in the _configuration_ section, see `ftp_backup_host` variable. Furthermore you need to have a `.netrc` with valid credentials for the ftp server.
If you are unfamiliar with `.netrc` please see http://www.mavetju.org/unix/netrc.php for how to set up.

## Contact
* If you encounter any problems or bugs, please [create an issue](https://github.com/codedge/magento-backup/issues/new) on Github
* I am happy to merge in any improvements - just send me a pull request
* For any other things, send me a mail to holger.loesken@codedge.de



145 changes: 145 additions & 0 deletions magento_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/bin/bash
#@version 0.3.0
#@author Holger Lösken <holger.loesken@codedge.de>

# Configuration
projectName=MyProject
currentDir="$(pwd)"
backupDir="$currentDir/var/backups"

# The user and pass are stored in ~/.netrc on the web server
ftp_backup_host="11.22.33.44"
ftp_backup_dir="backups" # Put a "." to stay in the current folder

# Edit below only if you know what you are doing!
dbXmlPath="app/etc/local.xml"
{
host="$(xmllint --xpath 'string(//default_setup/connection/host)' $dbXmlPath)"
username="$(xmllint --xpath 'string(//default_setup/connection/username)' $dbXmlPath)"
password="$(xmllint --xpath 'string(//default_setup/connection/password)' $dbXmlPath)"
dbName="$(xmllint --xpath 'string(//default_setup/connection/dbname)' $dbXmlPath)"
}


usage()
{
echo "
Usage: $0 -t <database|files|basesystem> -m <1|0> -b <1|0>
-t: Backup type, either database only or files
-m: If enabled you can skip media files (media directory) from being backuped.
Files in includes/ and /var as well as .htaccess will always be excluded.
-b: If enabled the backup files will be moved to an external ftp server.
This can only be used with a .netrc file with username and password of the server.
" 1>&2;
exit 1;
}


ftp_backup()
{
f=$1

ftp $ftp_backup_host <<ENDFTP
cd $ftp_backup_dir
put $f
quit
ENDFTP
}


while getopts ":t:m:b:" o; do
case "${o}" in
t)
t=${OPTARG}
((t == database || t == files || t == basesystem)) || usage
;;
m)
m=${OPTARG}
((m == 1 || m == 0)) || usage
;;
b)
b=${OPTARG}
((b == 1 || b == 0)) || usage
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))

backupType=$t
skipMedia=$m
makeFtpBackup=$b

fileName=$projectName-$(date +"%Y-%m-%d")


if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"
echo "Dumping MySQL to $fileName.sql.gz..."
mysqldump -h$host -u$username -p$password $dbName | gzip > $fileName.sql.gz
echo "Done!"
fi

if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"
echo "Archiving Files to $fileName.tar.gz..."

if [ $skipMedia == 1 ]; then
echo " -> Skipping media files (media directory)"
tar -zcf $fileName.tar.gz --exclude=var --exclude=includes --exclude=media * .htaccess
else
echo " -> Include media files (media directory)"
tar -zcf $fileName.tar.gz --exclude=var --exclude=includes * .htaccess
fi
echo "Done!"
echo "----------------------------------------------------"
fi

if [ "$backupType" == "database" ] || [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo "----------------------------------------------------"

if [ ! -d $backupDir ]; then
echo "$backupDir does not exist! Creating..."
mkdir -p $backupDir;
echo "Done!"
echo "----------------------------------------------------"
fi

echo "Moving file to backup dir $backupDir..."
if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.sql.gz"
mv $fileName.sql.gz $backupDir
fi

if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.tar.gz"
mv $fileName.tar.gz $backupDir
fi
echo "Done!"
echo "----------------------------------------------------"
else
usage
fi

if [ "$makeFtpBackup" == "1" ]; then
echo "Copying files to ftp backup server ($ftp_backup_host)..."

if [ "$backupType" == "database" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.sql.gz"
cd $backupDir
ftp_backup "$fileName.sql.gz"
cd -
fi

if [ "$backupType" == "files" ] || [ "$backupType" == "basesystem" ]; then
echo " -> $fileName.tar.gz"
cd $backupDir
ftp_backup "$fileName.tar.gz"
cd -
fi

echo "Done!"
echo "----------------------------------------------------"
fi

0 comments on commit d85dbd0

Please sign in to comment.