diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd5193c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +*.sublime-project +*.sublime-workspace \ No newline at end of file diff --git a/README.md b/README.md index 7233d15..f11b082 100644 --- a/README.md +++ b/README.md @@ -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 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 `/var/backups` + +## Usage +`./magento-backup.sh -t -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 + + + diff --git a/magento_backup.sh b/magento_backup.sh new file mode 100755 index 0000000..9aa4ada --- /dev/null +++ b/magento_backup.sh @@ -0,0 +1,145 @@ +#!/bin/bash +#@version 0.3.0 +#@author Holger Lösken + +# 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 -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 < $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