Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- archiveLocation=/backups
- archiveTempPath=/backups/tmp_backup
- archiveName=$(date '+%d-%m-%Y_%H:%M');
- archiveFullName=$archiveName.tar.gz
- archivePath=$archiveLocation/$archiveFullName
- deleteLocalArchive=false
- backupPath=/var/www
- backupToRemoteServer=false
- remoteServerProtocol=sftp
- remoteServerPort=22
- remoteServerHost=
- remoteServerUsername=
- remoteServerPassword=
- remoteServerPath=/
- backupMysql=false
- backupMysqlUsername=
- backupMysqlPassword=
- backupMysqlDatabase=
- # Create temporary folder
- echo 'Creating temporary folders';
- mkdir $archiveTempPath
- mkdir $archiveTempPath/files
- mkdir $archiveTempPath/databases
- # Set permissions
- echo 'Setting file permissions';
- chmod -R 777 $archiveTempPath
- # Copy all the files
- echo 'Copying all files to temporary location';
- rsync -avz $backupPath $archiveTempPath/files -q
- # Export MySQL database
- if [ $backupMysql = true ]
- then
- echo "Exporting database: $backupMysqlDatabase";
- mysqldump -u$backupMysqlUsername -p$backupMysqlPassword $backupMysqlDatabase > $archiveTempPath/databases/$backupMysqlDatabase.sql 2>&1
- fi
- # Tar all files
- cd $archiveTempPath
- tar -czf "$archivePath" *
- cd $archiveLocation
- # Upload archive to external server
- if [ $backupToRemoteServer = true ]
- then
- echo "Uploading archive ($remoteServerProtocol)";
- if [ $remoteServerProtocol = 'ftp' ]
- then
- curl -T $archivePath ftp://$remoteServerHost:$remoteServerPort/$remoteServerPath/ --user $remoteServerUsername:$remoteServerPassword -s
- elif [ $remoteServerProtocol == 'sftp' ]
- then
- sshpass -p $remoteServerPassword scp -P $remoteServerPort $archivePath $remoteServerUsername@$remoteServerHost:$remoteServerPath/$archiveFullname
- fi
- fi
- # Remove all the temporary files
- echo 'Removing temporary files';
- rm -rf $archiveTempPath
- # Remove local archive if necessary
- if [ $deleteLocalArchive = true ]
- then
- echo 'Removing local archive';
- rm -rf $archivePath;
- fi
- # Done!
- echo "Backup $archiveFullName was completed";
Add Comment
Please, Sign In to add comment