1

I have a very small, non-essential database on my NAS that will be backed up weekly by a small Bash-script that is a modified version of this link's one:

outpath='/media/backupfiles/mysql_data'
dumpdate=$(date +"%Y-%m-%d")

mysqldump --user=xxx --password=xxx --events --all-databases | gzip -7 > ${outpath}/mysqldump_${dumpdate}.sql.gz

This script works flawlessly. However, I do not have much proficiency with Bash, while I love to code around with PowerShell. Naturally, I wanted to create an analog PowerShell-script:

param(
    [string]$OutPath = "/media/backupfiles/mysql_data",
    [string]$DumpBaseName = "mysqldump_$(Get-Date -Format "yyyy-MM-dd").sql"
)

mysqldump --user=xxx --password=xxx --events --all-databases | gzip -7 > $($OutPath)/$($DumpBaseName).gz

To start the script, I first started the console as root, then started powershell and then started the script /home/mysqldump.ps1.

Unfortunately, the above script does not work:

out-file : Access to the path '/media/backupfiles/mysql_data' is denied.

When I enter

mysqldump --user=xxx --password=xxx --events --all-databases | gzip -7 > /media/backupfiles/mysql_data/test.sql.gz

in the console manually, the file will get produced, but it is too big (about twice the size of the Bash-version), has no user-privileges by itself (meaning I have to run chown before I can access it), and also, when I then try to open it (with 7-zip or with gzip -d), both tell me that the file can't be opened and that it is not in gzip format.

I then tried:

Start-Process mysqldump -ArgumentList " --user=xxx --password=xxx --events --all-databases | gzip -7 > /media/backupfiles/mysql_data/test.sql.gz"

This fails, too:

mysqldump: unknown option '-7'

To me, it seems like the pipe/redirection operator is the problem. Unfortunately, I do not have any more ideas as to how to make this script work (without intermediate-files or -scripts). The Internet did not prove very helpfull this time, however, that might be caused by me searching for the wrong things.

Is there anything that I am missing? Or am I simply trying to get a functionality that is not (yet) implemented in PowerShell for Linux?

Used software:

  • OS: openmediavault 3.0.88 (which is Debian 4.9.30-2+deb9u2~bpo8+1 2017-06-27) x86_64 GNU/Linux)
  • PowerShell: v6.0.0-beta.6
  • gzip: 1.6
  • mysqldump: Ver 10.13 Distrib 5.5.57

1 Answer 1

2

The > redirection operator in PowerShell does the same as | Out-File. The default encoding for Out-File is Unicode (UTF-16 LE to be precise), which cannot be changed for the redirection operator.

Replace

... > $($OutPath)/$($DumpBaseName).gz

with either

... | Out-File "${OutPath}/${DumpBaseName}.gz" -Encoding Ascii

or

... | Set-Content "${OutPath}/${DumpBaseName}.gz"

The Set-Content cmdlet defaults to ASCII output.

Personally, I'd stick with bash for a task like this, though. You're using external commands to write a text dump to a (compressed) output file, so you're not taking advantage of PowerShell features anywhere. The only difference between your bash and PowerShell code is that the PowerShell code has parameters with default values. You can do something similar in bash via parameter expansion:

outpath="${1:-'/media/backupfiles/mysql_data'}"
dumpdate="${2:-$(date +"%Y-%m-%d")}"

The above assigns the first and second positional parameter (or the specified default value if the parameter is missing) to the variables $outpath and $dumpdate respectively. Order matters here, though. If you want to be able to specify the parameters independently from each other (e.g. pass the date as a parameter, but not the path) you need code like this:

while getopts ':do' opt; do
  case "$opt" in
    d) dumpdate="$OPTARG";;
    o) outpath="$OPTARG";;
    \?) echo "Invalid option: -${OPTARG}"; exit 1;;  
  esac
done
shift $((OPTIND-1))

outpath="${outpath:-'/media/backupfiles/mysql_data'}"
dumpdate="${dumpdate:-$(date +"%Y-%m-%d")}"
2
  • I understand that there is no advantage in my PowerShell-"implementation"; it's just for learning how things work. Thank you for both the explanation about parameters in Bash and the explanation about > in PowerShell; however, both your suggestions for replacing > fail. Out-File .. -Encoding ascii will return a file of decent size, but it still is not readable (gzip -d / 7zip GUI). Set-Content will create a .gz that is again twice the size of the working Bash-script's (though not the same size as the one when using >), but it still is not readable.
    – flolilo
    Commented Sep 11, 2017 at 14:41
  • I have no idea what's happening there, but it looks like a bug to me. Commented Sep 11, 2017 at 20:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.